diff --git a/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java b/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java index 4848018..cffd5df 100644 --- a/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java +++ b/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java @@ -1,5 +1,7 @@ package com.example.citrusclient.rest; +import com.example.citrusclient.models.Book; + import java.util.HashMap; import java.util.HashSet; @@ -14,9 +16,9 @@ public interface FavoritesRest { //主{account_id}がいいねした本の一覧 - @FormUrlEncoded + @GET("accounts/{account_id}/favorites") - Call>> getFavoritesBooks( + Call>> getFavoritesBooks( @Path("account_id") String accountId, @Query("token") String token ); diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java index 5887c1f..0ba119b 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java @@ -1,5 +1,6 @@ package com.example.citrusclient.viewmodels; +import com.example.citrusclient.models.Book; import com.example.citrusclient.rest.FavoritesRest; import java.util.HashMap; @@ -17,7 +18,7 @@ private final Retrofit retrofit; private final FavoritesRest favoritesRest; - private final MutableLiveData>> favoritesBooksLiveData; + private final MutableLiveData>> favoritesBooksLiveData; private final MutableLiveData> favoritesBookIdLiveData; private final MutableLiveData> favoritedAccountsLiveData; @@ -31,15 +32,15 @@ this.favoritesBookIdLiveData = new MutableLiveData<>(); this.favoritedAccountsLiveData = new MutableLiveData<>(); } - public MutableLiveData>> getFavoritesBooksLiveData(){return favoritesBooksLiveData;} + public MutableLiveData>> getFavoritesBooksLiveData(){return favoritesBooksLiveData;} public MutableLiveData> getFavoritesBookIdLiveData(){return favoritesBookIdLiveData;} public MutableLiveData> getFavoritedAccountsLiveData(){return favoritedAccountsLiveData;} public void loadFavoritesBooks(String accountId, String token){ - Call>> call = favoritesRest.getFavoritesBooks(accountId,token); - call.enqueue(new Callback>>() { + Call>> call = favoritesRest.getFavoritesBooks(accountId,token); + call.enqueue(new Callback>>() { @Override - public void onResponse(Call>> call, Response>> response) { + public void onResponse(Call>> call, Response>> response) { if(response.isSuccessful()){ System.out.println("success : getFavoritesBooks"); favoritesBooksLiveData.setValue(response.body()); @@ -47,7 +48,7 @@ } @Override - public void onFailure(Call>> call, Throwable t) { + public void onFailure(Call>> call, Throwable t) { System.out.println("NetworkError : getFavoritesBooks" + t); } }); diff --git a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java index 5b0d307..bc701ca 100644 --- a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java @@ -18,6 +18,7 @@ import android.view.ViewTreeObserver; import android.widget.Button; import android.widget.LinearLayout; +import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TableLayout; import android.widget.TableRow; @@ -129,7 +130,7 @@ HashMap books; HashMap> schedules; - HashMap> todos; + HashMap> todos; public TextView[][] calendar(int firstDayOfWeek, int prevMonthDay, int lastDay) { int curDay = 1; @@ -232,10 +233,10 @@ if(integerHashMapHashMap != null && !integerHashMapHashMap.isEmpty()) { for (int day : integerHashMapHashMap.keySet()) { if (todos.get(day) == null) { - todos.put(day, new HashMap<>()); + todos.put(day, new ArrayList<>()); } for (int todoId : integerHashMapHashMap.get(day).keySet()) { - todos.get(day).put(todoId, integerHashMapHashMap.get(day).get(todoId)); + todos.get(day).add(integerHashMapHashMap.get(day).get(todoId)); } } } @@ -276,7 +277,16 @@ createBook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment(year, month, day)); + RadioButton rbSchedule = getActivity().findViewById(R.id.rbSchedule); + RadioButton rbTodo = getActivity().findViewById(R.id.rbTodo); + RadioButton rbBoth = getActivity().findViewById(R.id.rbBoth); + if(rbSchedule != null && rbSchedule.isChecked()) { + ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment(year, month, day)); + } else if(rbTodo != null && rbTodo.isChecked()) { + ((MainActivity) getActivity()).showFragment(new CreateTodoFragment(year, month, day)); + } else if(rbBoth != null && rbBoth.isChecked()) { + ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment(year, month, day)); + } } }); @@ -289,7 +299,10 @@ month = 1; year++; } + todos.clear(); scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); + booksViewModel.loadBooks(accountId, token); + updateCalendar(curMonth); } }); @@ -302,7 +315,10 @@ month = 12; year--; } + todos.clear(); scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); + booksViewModel.loadBooks(accountId, token); + updateCalendar(curMonth); } }); @@ -428,7 +444,7 @@ if(select != -1){ if(select == R.id.rbTodo){ if (this.todos.get(Integer.parseInt(days[i][j])) != null) { - for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j])).values()) { + for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j]))) { schedules.add(schedule); } } @@ -440,7 +456,7 @@ } } else { if (this.todos.get(Integer.parseInt(days[i][j])) != null) { - for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j])).values()) { + for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j]))) { schedules.add(schedule); } } diff --git a/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java b/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java new file mode 100644 index 0000000..31261d8 --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java @@ -0,0 +1,200 @@ +package com.example.citrusclient.views; + +import android.app.Activity; +import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.Color; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import com.example.citrusclient.Citrus; +import com.example.citrusclient.R; +import com.example.citrusclient.models.Book; +import com.example.citrusclient.viewmodels.FavoritesViewModel; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link FavoritesBooksFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class FavoritesBooksFragment extends Fragment { + + // TODO: Rename parameter arguments, choose names that match + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + + // TODO: Rename and change types of parameters + private String mParam1; + private String mParam2; + + public FavoritesBooksFragment() { + // Required empty public constructor + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @param param1 Parameter 1. + * @param param2 Parameter 2. + * @return A new instance of fragment FavoritesBooksFragment. + */ + // TODO: Rename and change types and number of parameters + public static FavoritesBooksFragment newInstance(String param1, String param2) { + FavoritesBooksFragment fragment = new FavoritesBooksFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + + favoritesViewModel = new ViewModelProvider(this).get(FavoritesViewModel.class); + + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_favorites_books, container, false); + } + + + private HashMap> bookList; + HashMap books; + HashMap integerBookHashMap; + HashMap> data; + + FavoritesViewModel favoritesViewModel; + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + Citrus citrus = (Citrus) (getActivity().getApplication()); + bookList = new HashMap<>(); + + // いいねした本の一覧を返すのに必要 + RecyclerView recyclerView = view.findViewById(R.id.my_favorites_list); //いいねした本の一覧 + recyclerView.setHasFixedSize(true); //サイズの固定 + RecyclerView.LayoutManager layoutManager = new GridLayoutManager(view.getContext(), 3); //何列にするか + recyclerView.setLayoutManager(layoutManager); + FavoritesBooksAdapter favoritesBooksAdapter = new FavoritesBooksAdapter(bookList); + recyclerView.setAdapter(favoritesBooksAdapter); + + favoritesViewModel.getFavoritesBooksLiveData().observe(getViewLifecycleOwner(), favoritesBooks -> { + bookList = favoritesBooks; // アダプターに新しいリストを渡す + ((FavoritesBooksAdapter)recyclerView.getAdapter()).setFavoritesBooks(favoritesBooks); + + }); + favoritesViewModel.loadFavoritesBooks(citrus.getAccountId(), citrus.getToken()); + + } +} + + +class FavoritesBooksAdapter extends RecyclerView.Adapter { + + private List books = new ArrayList<>(); + //private HashMap integerBookHashMap; + //private HashMap> data; + + FavoritesBooksAdapter(HashMap> favoritesBooksList) { //初期化 + + for (String account: favoritesBooksList.keySet()){ + for (int number: favoritesBooksList.get(account).keySet()){ + books.add(favoritesBooksList.get(account).get(number)); + } + } + // this.data = data; + // this.integerBookHashMap = new HashMap<>(); + } + + public void setFavoritesBooks(HashMap> favoritesBooksList) { + for (String account: favoritesBooksList.keySet()){ + for (int number: favoritesBooksList.get(account).keySet()){ + books.add(favoritesBooksList.get(account).get(number)); + } + } + notifyDataSetChanged(); + } + + + @NonNull + @Override + public FavoritesBooksViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_favorites_book, parent, false); + return new FavoritesBooksViewHolder(view); + } + @Override + public void onBindViewHolder(@NonNull FavoritesBooksViewHolder holder, int position) { //表示に関係する +// book = favoritesBooksList.get(position); +// holder.favorites_book.setText(book.getTitle()); +// +// TextView other = (TextView) view.findViewById(R.id.other_id); +// +// holder.favorites_book.setText(book.getTitle()); + +// int count = 0; +// for (String otherAccountId: favoritesBooksList.keySet()) { +// HashMap bookMap = favoritesBooksList.get(otherAccountId); +// for (Integer bookId : bookMap.keySet()) { +// Book book = bookMap.get(bookId); +// if (count == position){ + holder.other_id.setText(books.get(position).getAccountId()); + holder.favorites_book.setText(books.get(position).getTitle()); + int parsedColor = Color.parseColor(books.get(position).getColor()); + holder.favorites_book.setBackgroundTintList(ColorStateList.valueOf(parsedColor)); + Context context = holder.itemView.getContext(); + holder.favorites_book.setOnClickListener(v -> { + Activity activity = (Activity) context; + Citrus citrus = (Citrus) activity.getApplication(); + citrus.setCurLookingAccountId(books.get(position).getAccountId()); + citrus.setCurLookingBookId(books.get(position).getBookId()); + ((MainActivity) activity).showFragment(new OtherHomeFragment()); + }); + + } + + + static class FavoritesBooksViewHolder extends RecyclerView.ViewHolder { + TextView other_id; + Button favorites_book; + + public FavoritesBooksViewHolder(@NonNull View itemView) { + super(itemView); + favorites_book = itemView.findViewById(R.id.favorites_book); + other_id = itemView.findViewById(R.id.other_id); + } + } + + @Override + public int getItemCount() { + return books.size(); + } // これがないとclassにエラー + +} diff --git a/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java b/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java index 0161adb..4001914 100644 --- a/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java @@ -68,6 +68,11 @@ month = calendar.get(Calendar.MONTH) + 1; //現在の月 } + public OtherCalendarFragment(int year, int month) { + this.year = year; + this.month = month; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -106,7 +111,7 @@ HashMap books; private Book book; HashMap> schedules; - HashMap> todos; + HashMap> todos; public TextView[][] calendar(int firstDayOfWeek, int prevMonthDay, int lastDay) { int curDay = 1; @@ -195,8 +200,6 @@ } todosViewModel.loadTodosByMonth(accountId, book.getBookId(), year, month, token); updateCalendar(curMonth); - Log.d("Debug", "Book: " + book); - Log.d("Debug", "Color: " + (book != null ? book.getColor() : "No color")); } } }); @@ -207,11 +210,9 @@ TextView curMonth = view.findViewById(R.id.otherMonth); if(integerHashMapHashMap != null && !integerHashMapHashMap.isEmpty()) { for (int day : integerHashMapHashMap.keySet()) { - if (todos.get(day) == null) { - todos.put(day, new HashMap<>()); - } + todos.put(day, new ArrayList<>()); for (int todoId : integerHashMapHashMap.get(day).keySet()) { - todos.get(day).put(todoId, integerHashMapHashMap.get(day).get(todoId)); + todos.get(day).add(integerHashMapHashMap.get(day).get(todoId)); } } } @@ -260,8 +261,11 @@ month = 1; year++; } + todos.clear(); scheduleViewModel.updateSchedulesByMonthAndBookId(accountId, year, month, token, bookId); + booksViewModel.loadBook(accountId, token, bookId); todosViewModel.loadTodosByMonth(accountId, bookId, year, month, token); + updateCalendar(curMonth); } }); @@ -274,8 +278,11 @@ month = 12; year--; } + todos.clear(); scheduleViewModel.updateSchedulesByMonthAndBookId(accountId, year, month, token, bookId); + booksViewModel.loadBook(accountId, token, bookId); todosViewModel.loadTodosByMonth(accountId, bookId, year, month, token); + updateCalendar(curMonth); } }); @@ -407,7 +414,7 @@ if(select != -1){ if(select == R.id.otherRbTodo){ if (this.todos.get(Integer.parseInt(days[i][j])) != null) { - for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j])).values()) { + for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j]))) { schedules.add(schedule); } } @@ -419,7 +426,7 @@ } } else { if (this.todos.get(Integer.parseInt(days[i][j])) != null) { - for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j])).values()) { + for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j]))) { schedules.add(schedule); } } diff --git a/app/src/main/res/layout/a_favorites_book.xml b/app/src/main/res/layout/a_favorites_book.xml index f00a3b4..89d0cc1 100644 --- a/app/src/main/res/layout/a_favorites_book.xml +++ b/app/src/main/res/layout/a_favorites_book.xml @@ -1,22 +1,34 @@ - + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_margin="12dp" + tools:ignore="ExtraText">