diff --git a/app/src/main/java/com/example/citrusclient/rest/BooksRest.java b/app/src/main/java/com/example/citrusclient/rest/BooksRest.java index 0119071..b4500c9 100644 --- a/app/src/main/java/com/example/citrusclient/rest/BooksRest.java +++ b/app/src/main/java/com/example/citrusclient/rest/BooksRest.java @@ -95,13 +95,13 @@ @Field("token") String token ); - @PUT("/{account_id}/books/{book_id}/registerFavoriteCount") + @PUT("accounts/{account_id}/books/{book_id}/registerFavoriteCount") Call registerFavoriteCount( @Path("account_id") String account_id, @Path("book_id") Integer book_id ); - @PUT("/{account_id}/books/{book_id}/unregisterFavoriteCount") + @PUT("accounts/{account_id}/books/{book_id}/unregisterFavoriteCount") Call unregisterFavoriteCount( @Path("account_id") String account_id, @Path("book_id") Integer book_id diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java index ea47647..8a16215 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java @@ -6,6 +6,7 @@ import com.example.citrusclient.models.Book; import com.example.citrusclient.rest.BooksRest; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -201,7 +202,7 @@ if (response.isSuccessful()){ System.out.println("Success registerFavorited"); } else { - System.out.println("response error"); + System.out.println(response.code()); } } @@ -221,7 +222,7 @@ if (response.isSuccessful()){ System.out.println("Success unresterFavorited"); } else { - System.out.println("response error"); + System.out.println("response errora"); } } 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 bc701ca..e5a7f6d 100644 --- a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java @@ -236,7 +236,10 @@ todos.put(day, new ArrayList<>()); } for (int todoId : integerHashMapHashMap.get(day).keySet()) { - todos.get(day).add(integerHashMapHashMap.get(day).get(todoId)); + Todo todo = integerHashMapHashMap.get(day).get(todoId); + if(!todo.containsTodo(todos.get(day))){ + todos.get(day).add(todo); + } } } } diff --git a/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java b/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java index 160d860..c9157e3 100644 --- a/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java @@ -182,6 +182,7 @@ 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; @@ -194,7 +195,7 @@ //ここにアイコンの色 int other_iconColor = Color.parseColor(books.get(position).getAccountColor()); holder.other_icon.setBackgroundTintList(ColorStateList.valueOf(other_iconColor)); - + holder.favorites_book.setTextColor(Color.BLACK); } diff --git a/app/src/main/java/com/example/citrusclient/views/MyAdapter.java b/app/src/main/java/com/example/citrusclient/views/MyAdapter.java index ae0198f..425c268 100644 --- a/app/src/main/java/com/example/citrusclient/views/MyAdapter.java +++ b/app/src/main/java/com/example/citrusclient/views/MyAdapter.java @@ -14,6 +14,7 @@ import com.example.citrusclient.Citrus; import com.example.citrusclient.R; import com.example.citrusclient.models.Book; +import com.example.citrusclient.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.FavoritesViewModel; import java.util.ArrayList; @@ -21,6 +22,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.ResourceBundle; import androidx.annotation.NonNull; import androidx.fragment.app.FragmentActivity; @@ -31,6 +33,7 @@ private List originalList; // 元のデータリスト private List filteredList; // フィルタリングされたリスト private HashMap> favoritesMap = new HashMap<>(); + private HashSet favoritedMap; public MyAdapter(List originalList) { @@ -52,6 +55,7 @@ Context context = holder.itemView.getContext(); Activity activity = (Activity) context; Citrus citrus = (Citrus) activity.getApplication(); + BooksViewModel booksViewModel = new ViewModelProvider((FragmentActivity)context).get(BooksViewModel.class); FavoritesViewModel favoritesViewModel = new ViewModelProvider((FragmentActivity)context).get(FavoritesViewModel.class); // 背景色の設定 @@ -74,34 +78,49 @@ citrus.setCurDay(calendar.get(Calendar.DATE)); ((MainActivity) activity).showFragment(new OtherHomeFragment()); }); -// if (favoritesMap != null && favoritesMap.containsKey(currentBook.getAccountId())) { -// if (favoritesMap.get(currentBook.getAccountId()).contains(currentBook.getBookId())) { -// holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_24); -// holder.LikeTextView.setTag("liked"); -// } else { -// holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_border_24); -// holder.LikeTextView.setTag("unliked"); -// } -// } else { -// holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_border_24); -// holder.LikeTextView.setTag("unliked"); -// } + + if (favoritesMap != null && favoritesMap.containsKey(currentBook.getAccountId())) { + if (favoritesMap.get(currentBook.getAccountId()).contains(currentBook.getBookId())) { + holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_24); + holder.LikeTextView.setTag("liked"); + } else { + holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_border_24); + holder.LikeTextView.setTag("unliked"); + } + } else { + holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_border_24); + holder.LikeTextView.setTag("unliked"); + } holder.LikeTextView.setOnClickListener(view -> { System.out.println(holder.LikeTextView.getTag()); if(holder.LikeTextView.getTag().equals("unliked")){ System.out.println(citrus.getAccountId()+"が"+currentBook.getAccountId()+"の"+currentBook.getBookId()+"を登録しました"); + + booksViewModel.registerFavoriteCount(currentBook.getAccountId(), currentBook.getBookId()); + holder.LikedCountTextView.setText(context.getString(R.string.liked_count_format, currentBook.getFavoritedCount())); + favoritesViewModel.setFavorite(currentBook.getAccountId(), currentBook.getBookId(), citrus.getAccountId(), citrus.getToken()); holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_24); Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(),R.anim.touch); view.startAnimation(animation); holder.LikeTextView.setTag("liked"); + if(!favoritesMap.containsKey(currentBook.getAccountId())) { + favoritesMap.put(currentBook.getAccountId(), new HashSet<>()); + } + favoritesMap.get(currentBook.getAccountId()).add(currentBook.getBookId()); + currentBook.setFavoritedCount(currentBook.getFavoritedCount()+1); }else if(holder.LikeTextView.getTag().equals("liked")){ System.out.println(citrus.getAccountId()+"が"+currentBook.getAccountId()+"の"+currentBook.getBookId()+"を解除しました"); + booksViewModel.unregisterFavoriteCount(currentBook.getAccountId(), currentBook.getBookId()); + holder.LikedCountTextView.setText(context.getString(R.string.liked_count_format, currentBook.getFavoritedCount())); favoritesViewModel.deletefavorite(currentBook.getAccountId(), currentBook.getBookId(), citrus.getAccountId(), citrus.getToken()); holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_border_24); holder.LikeTextView.setTag("unliked"); + favoritesMap.get(currentBook.getAccountId()).remove(currentBook.getBookId()); + currentBook.setFavoritedCount(currentBook.getFavoritedCount()-1); } + holder.LikedCountTextView.setText(context.getString(R.string.liked_count_format, currentBook.getFavoritedCount())); }); } @@ -139,10 +158,16 @@ notifyDataSetChanged(); } + public void updateFavorited(HashSet likedBooksCount) { + this.favoritedMap = likedBooksCount; + notifyDataSetChanged(); // アダプターを更新 + } + static class ViewHolder extends RecyclerView.ViewHolder { TextView titleTextView; TextView authorTextView; ImageButton LikeTextView; + TextView LikedCountTextView; ViewHolder(View itemView) { @@ -150,12 +175,14 @@ titleTextView = itemView.findViewById(R.id.public_button); authorTextView = itemView.findViewById(R.id.public_id); LikeTextView = itemView.findViewById(R.id.public_favorite); - + LikedCountTextView = itemView.findViewById(R.id.liketed_count); } void bind(Book book) { titleTextView.setText(book.getTitle()); authorTextView.setText(book.getAccountId()); + Context context = itemView.getContext(); + LikedCountTextView.setText(context.getString(R.string.liked_count_format, book.getFavoritedCount())); } } } diff --git a/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java b/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java index fb5656a..85227c8 100644 --- a/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java @@ -160,7 +160,7 @@ todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); - MyOtherTodoshelfAdapter todoAdapter = new MyOtherTodoshelfAdapter(todoList, book, todosViewModel, getActivity()); + MyOtherTodoshelfAdapter todoAdapter = new MyOtherTodoshelfAdapter(todoList, book); todoRecyclerView.setAdapter(todoAdapter); todoAdapter.setItemWidth(dpToPx(todoWidth)); //ScheduleAdapter設定 @@ -168,7 +168,7 @@ scheduleRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext()); scheduleRecyclerView.setLayoutManager(scheduleLayoutManager); - MyOtherScheduleshelfAdapter scheduleAdapter = new MyOtherScheduleshelfAdapter(scheduleList, book, curDate, getActivity()); + MyOtherScheduleshelfAdapter scheduleAdapter = new MyOtherScheduleshelfAdapter(scheduleList, book, curDate); scheduleRecyclerView.setAdapter(scheduleAdapter); scheduleAdapter.setItemWidth(dpToPx(scheduleWidth)); @@ -316,7 +316,7 @@ this.book = book; } todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); - scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); + scheduleViewModel.updateSchedulesByDayAndBookId(accountId, year, month, day, token, curBookId); } private void clearTodoSchedule(MyOtherTodoshelfAdapter todoAdapter, MyOtherScheduleshelfAdapter scheduleAdapter) { @@ -343,16 +343,16 @@ class MyOtherTodoshelfAdapter extends RecyclerView.Adapter { private List todoList; private Book book; - private TodosViewModel todosViewModel; - private Context context; +// private TodosViewModel todosViewModel; +// private Context context; private int itemWidth; // アイテムの幅を保持 - MyOtherTodoshelfAdapter(List todos, Book book, TodosViewModel todosViewModel, Context context) { + MyOtherTodoshelfAdapter(List todos, Book book) { this.todoList = todos; - this.todosViewModel = todosViewModel; this.book = book; - this.context = context; +// this.todosViewModel = todosViewModel; +// this.context = context; } public void setTodos(List todos ,Book book) { @@ -449,15 +449,15 @@ private List scheduleList; Book book; - private Context context; +// private Context context; private LocalDate curDate; private int itemWidth; // アイテムの幅を保持 - MyOtherScheduleshelfAdapter(List schedules, Book book, LocalDate curDate, Context context) { + MyOtherScheduleshelfAdapter(List schedules, Book book, LocalDate curDate) { this.scheduleList = schedules; this.book = book; this.curDate = curDate; - this.context = context; +// this.context = context; } public void setSchedules(List schedules, Book book) { diff --git a/app/src/main/java/com/example/citrusclient/views/SearchFragment.java b/app/src/main/java/com/example/citrusclient/views/SearchFragment.java index 35095a3..88385ce 100644 --- a/app/src/main/java/com/example/citrusclient/views/SearchFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/SearchFragment.java @@ -25,12 +25,14 @@ import com.example.citrusclient.Citrus; import com.example.citrusclient.R; import com.example.citrusclient.models.Book; +import com.example.citrusclient.models.BookModel; import com.example.citrusclient.viewmodels.PublicBooksViewModel; import com.example.citrusclient.viewmodels.FavoritesViewModel; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; /** @@ -152,20 +154,40 @@ adapter.setBooks(new ArrayList<>(books)); // アダプターに新しいリストを渡す } }); -// favoritesViewModel.getFavoritesBooksLiveData().observe(getViewLifecycleOwner(), new Observer>>() { + favoritesViewModel.getFavoritesBooksLiveData().observe(getViewLifecycleOwner(), (Observer>) new Observer>() { + @Override + public void onChanged(List stringHashSetHashMap) { + if (stringHashSetHashMap != null) { + HashMap> newMap = new HashMap<>(); + + // 変換処理 + for (BookModel bookModel : stringHashSetHashMap) { + if(!newMap.containsKey(bookModel.getAccountId())){ + newMap.put(bookModel.getAccountId(), new HashSet<>()); + } + newMap.get(bookModel.getAccountId()).add(bookModel.getBookId()); + } + System.out.println("aaaaaaaaa"); + adapter.updateFavorites(newMap); + } else { + adapter.updateFavorites(new HashMap<>()); + } + } + }); + +// favoritesViewModel.getFavoritedAccountsLiveData().observe(getViewLifecycleOwner(), new Observer>() { // @Override -// public void onChanged(HashMap> stringHashSetHashMap) { -// if (stringHashSetHashMap != null) { -// adapter.updateFavorites(stringHashSetHashMap); -// } else { -// adapter.updateFavorites(new HashMap<>()); +// public void onChanged(HashMap likedBooksCount) { +// if (likedBooksCount != null) { +// adapter.updateFavorited(likedBooksCount); // HashMapを渡す // } // } // }); // Booksを読み込む publicBooksViewModel.loadAllBooks(); -// favoritesViewModel.loadFavoritesBooks(citrus.getAccountId(), citrus.getToken()); + favoritesViewModel.loadFavoritesBooks(citrus.getAccountId(), citrus.getToken()); + //favoritesViewModel.loadFavoritedAccounts(citrus.getCurLookingAccountId(), citrus.getCurLookingBookId(), citrus.getToken()); // SearchViewの設定 SearchView searchView = view.findViewById(R.id.search_word); @@ -174,6 +196,8 @@ public boolean onQueryTextSubmit(String query) { searchWord = query; loadSearchResults(searchWord, sortBy); + favoritesViewModel.loadFavoritesBooks(citrus.getAccountId(), citrus.getToken()); + favoritesViewModel.loadFavoritedAccounts(citrus.getCurLookingAccountId(), citrus.getCurLookingBookId(), citrus.getToken()); Log.d(TAG, "Search query: " + query); return true; } @@ -183,6 +207,8 @@ // adapter.filter(newText); // フィルタリングを実行 searchWord = newText; loadSearchResults(searchWord, sortBy); + favoritesViewModel.loadFavoritesBooks(citrus.getAccountId(), citrus.getToken()); + favoritesViewModel.loadFavoritedAccounts(citrus.getCurLookingAccountId(), citrus.getCurLookingBookId(), citrus.getToken()); Log.d(TAG, "Search query: " + newText); return true; } @@ -200,14 +226,17 @@ if(sort != null) sortBy = sort;//ソートはまだ Log.d(TAG, "Search query: " + title + accountId); - if(title != ""|| accountId != ""){ - if(sort != null) { + if(title != ""|| accountId != "" || sort != null){ +// if(sort != null) { publicBooksViewModel.loadSearchBooks(title, accountId, sortBy);//title,accountidでの検索 System.out.println("title: "+title+" accountId: "+accountId+" sortBy: "+sortBy); - } +// } }else { publicBooksViewModel.loadAllBooks(); // titleとaccountIdを使って検索 } } -} \ No newline at end of file +} +/* +155-173にかけてHashMapでやり取りしてたのがListに変わったので,その中にあるaccountIdとbookidを取り出してHashMap(String, HashSet)におさめる + */ \ No newline at end of file diff --git a/app/src/main/res/layout/a_favorites_book.xml b/app/src/main/res/layout/a_favorites_book.xml index b036845..118f0a8 100644 --- a/app/src/main/res/layout/a_favorites_book.xml +++ b/app/src/main/res/layout/a_favorites_book.xml @@ -41,7 +41,7 @@