package com.example.citrusclient.views; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageButton; 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.BooksViewModel; import com.example.citrusclient.viewmodels.FavoritesViewModel; import java.util.ArrayList; import java.util.Calendar; 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; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<Book> originalList; // 元のデータリスト private List<Book> filteredList; // フィルタリングされたリスト private HashMap<String, HashSet<Integer>> favoritesMap = new HashMap<>(); private HashSet<String> favoritedMap; public MyAdapter(List<Book> originalList) { this.originalList = originalList; this.filteredList = new ArrayList<>(originalList); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_public_book, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Book currentBook = filteredList.get(position); holder.bind(currentBook); // bindメソッドで設定 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); // 背景色の設定 int red, green, blue; if (currentBook.getColor() == null || currentBook.getColor().length() < 7) { red = 255; green = 255; blue = 255; } else { red = Integer.parseInt(currentBook.getColor().substring(1, 3), 16); green = Integer.parseInt(currentBook.getColor().substring(3, 5), 16); blue = Integer.parseInt(currentBook.getColor().substring(5, 7), 16); } holder.titleTextView.setBackgroundColor(Color.rgb(red, green, blue)); holder.titleTextView.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); holder.titleTextView.setOnClickListener(v -> { citrus.setCurLookingAccountId(currentBook.getAccountId()); citrus.setCurLookingBookId(currentBook.getBookId()); Calendar calendar = Calendar.getInstance(); citrus.setCurYear(calendar.get(Calendar.YEAR)); citrus.setCurMonth(calendar.get(Calendar.MONTH) + 1); 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"); } 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())); }); } @Override public int getItemCount() { return filteredList != null ? filteredList.size() : 0; // filteredListがnullの場合は0を返す } // 新しいデータを設定するメソッド public void setBooks(List<Book> books) { originalList.clear(); originalList.addAll(books); filteredList.clear(); // フィルタリングされたリストもクリア filteredList.addAll(originalList); // 元のリストの内容を追加 notifyDataSetChanged(); // アダプターを更新 } // フィルタリングメソッド public void filter(String query) { filteredList.clear(); if (query.isEmpty()) { filteredList.addAll(originalList); } else { String lowerCaseQuery = query.toLowerCase().trim(); for (Book item : originalList) { if (item.getAccountId().toLowerCase().contains(lowerCaseQuery) || item.getTitle().toLowerCase().contains(lowerCaseQuery)) { filteredList.add(item); } } } notifyDataSetChanged(); } public void updateFavorites(HashMap<String, HashSet<Integer>> newFavorites){ this.favoritesMap = newFavorites; notifyDataSetChanged(); } public void updateFavorited(HashSet<String> likedBooksCount) { this.favoritedMap = likedBooksCount; notifyDataSetChanged(); // アダプターを更新 } static class ViewHolder extends RecyclerView.ViewHolder { TextView titleTextView; TextView authorTextView; ImageButton LikeTextView; TextView LikedCountTextView; ViewHolder(View itemView) { super(itemView); 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())); } } }