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 6b72b78..ae0198f 100644 --- a/app/src/main/java/com/example/citrusclient/views/MyAdapter.java +++ b/app/src/main/java/com/example/citrusclient/views/MyAdapter.java @@ -17,6 +17,9 @@ 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 androidx.annotation.NonNull; @@ -27,6 +30,7 @@ public class MyAdapter extends RecyclerView.Adapter { private List originalList; // 元のデータリスト private List filteredList; // フィルタリングされたリスト + private HashMap> favoritesMap = new HashMap<>(); public MyAdapter(List originalList) { @@ -46,6 +50,9 @@ Book currentBook = filteredList.get(position); holder.bind(currentBook); // bindメソッドで設定 Context context = holder.itemView.getContext(); + Activity activity = (Activity) context; + Citrus citrus = (Citrus) activity.getApplication(); + FavoritesViewModel favoritesViewModel = new ViewModelProvider((FragmentActivity)context).get(FavoritesViewModel.class); // 背景色の設定 int red, green, blue; @@ -59,16 +66,28 @@ holder.titleTextView.setBackgroundColor(Color.rgb(red, green, blue)); holder.titleTextView.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); holder.titleTextView.setOnClickListener(v -> { - Activity activity = (Activity) context; - Citrus citrus = (Citrus) activity.getApplication(); 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 -> { - Activity activity = (Activity) context; - Citrus citrus = (Citrus) activity.getApplication(); - FavoritesViewModel favoritesViewModel = new ViewModelProvider((FragmentActivity)context).get(FavoritesViewModel.class); System.out.println(holder.LikeTextView.getTag()); if(holder.LikeTextView.getTag().equals("unliked")){ System.out.println(citrus.getAccountId()+"が"+currentBook.getAccountId()+"の"+currentBook.getBookId()+"を登録しました"); @@ -115,6 +134,10 @@ } notifyDataSetChanged(); } + public void updateFavorites(HashMap> newFavorites){ + this.favoritesMap = newFavorites; + notifyDataSetChanged(); + } static class ViewHolder extends RecyclerView.ViewHolder { TextView titleTextView; 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 84c4e88..0161adb 100644 --- a/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java @@ -11,6 +11,7 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -77,6 +78,8 @@ scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); + booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); + todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); schedules = new HashMap<>(); @@ -90,7 +93,7 @@ } - + BooksViewModel booksViewModel; private List scheduleList; ScheduleViewModel scheduleViewModel; TodosViewModel todosViewModel; @@ -101,7 +104,7 @@ int day; HashMap books; - + private Book book; HashMap> schedules; HashMap> todos; @@ -179,7 +182,24 @@ } }); - + booksViewModel.getBook().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(Book updateBook) { + book = updateBook; + if (book != null) { + TextView curMonth = view.findViewById(R.id.otherMonth); + if (todos != null) { + todos.clear(); + } else { + todos = new HashMap<>(); + } + 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")); + } + } + }); todosViewModel.getTodosByMonthLiveData().observe(getViewLifecycleOwner(), new Observer>>() { @Override @@ -207,6 +227,7 @@ // month = calendar.get(Calendar.MONTH) + 1; //現在の月 scheduleViewModel.updateSchedulesByMonthAndBookId(accountId, year, month, token, bookId); + booksViewModel.loadBook(accountId, token, bookId); todosViewModel.loadTodosByMonth(accountId, bookId, year, month, token); //現在の月の最後の日付を取得 @@ -323,7 +344,7 @@ } citrus.setCurMonth(month + 1); } - ((MainActivity) getActivity()).showFragment(new HomeFragment()); + ((MainActivity) getActivity()).showFragment(new OtherHomeFragment()); } }); RecyclerView recyclerView = new RecyclerView(requireContext()); @@ -332,7 +353,7 @@ List schedules = new ArrayList<>(); - recyclerView.setAdapter(new OtherScheduleAdapter(schedules, books)); + recyclerView.setAdapter(new OtherScheduleAdapter(schedules, book)); layout.addView(recyclerView); layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override @@ -410,36 +431,39 @@ } } } - ((OtherScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, books); + ((OtherScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, book); ((TextView) layout.getChildAt(0)).setText(days[i][j]); } } } } + + class OtherScheduleAdapter extends RecyclerView.Adapter { private List scheduleList; - private HashMap integerBookHashMap; + private Book book; - OtherScheduleAdapter(List schedules, HashMap integerBookHashMap) { + OtherScheduleAdapter(List schedules, Book book) { this.scheduleList = schedules; - if(integerBookHashMap != null) { - this.integerBookHashMap = new HashMap<>(integerBookHashMap); + if(book != null) { + this.book = book; } else { - this.integerBookHashMap = new HashMap<>(); + this.book = new Book(); } } - public void setSchedules(List schedules, HashMap integerBookHashMap) { + public void setSchedules(List schedules, Book book) { scheduleList = schedules; - if(integerBookHashMap != null) { - this.integerBookHashMap = new HashMap<>(integerBookHashMap); + if(book != null) { + this.book = book; } else { - this.integerBookHashMap = new HashMap<>(); + this.book = new Book(); } notifyDataSetChanged(); } + @NonNull @Override public OtherScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { @@ -454,7 +478,6 @@ int blue = 169; Task scheduleData = this.scheduleList.get(position); holder.scheduleText.setText(scheduleData.getTitle()); - Book book = integerBookHashMap.get(scheduleData.getBookId()); if(book != null) { red = Integer.parseInt(book.getColor().substring(1, 3), 16); green = Integer.parseInt(book.getColor().substring(3, 5), 16); 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 556f4c7..35095a3 100644 --- a/app/src/main/java/com/example/citrusclient/views/SearchFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/SearchFragment.java @@ -1,11 +1,13 @@ package com.example.citrusclient.views; import android.annotation.SuppressLint; +import android.app.Activity; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -20,13 +22,15 @@ import android.widget.SearchView; import android.widget.Spinner; +import com.example.citrusclient.Citrus; import com.example.citrusclient.R; +import com.example.citrusclient.models.Book; import com.example.citrusclient.viewmodels.PublicBooksViewModel; import com.example.citrusclient.viewmodels.FavoritesViewModel; import java.util.ArrayList; - - +import java.util.HashMap; +import java.util.HashSet; /** @@ -111,6 +115,7 @@ @SuppressLint("WrongViewCast") public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { adapter = new MyAdapter(new ArrayList<>()); + Citrus citrus = (Citrus)(getActivity().getApplication()); RecyclerView recyclerView = view.findViewById(R.id.public_list); recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); recyclerView.setAdapter(adapter); @@ -147,9 +152,20 @@ adapter.setBooks(new ArrayList<>(books)); // アダプターに新しいリストを渡す } }); +// favoritesViewModel.getFavoritesBooksLiveData().observe(getViewLifecycleOwner(), new Observer>>() { +// @Override +// public void onChanged(HashMap> stringHashSetHashMap) { +// if (stringHashSetHashMap != null) { +// adapter.updateFavorites(stringHashSetHashMap); +// } else { +// adapter.updateFavorites(new HashMap<>()); +// } +// } +// }); // Booksを読み込む publicBooksViewModel.loadAllBooks(); +// favoritesViewModel.loadFavoritesBooks(citrus.getAccountId(), citrus.getToken()); // SearchViewの設定 SearchView searchView = view.findViewById(R.id.search_word); @@ -181,7 +197,7 @@ title = parts.length > 0 ? parts[0] : ""; // 最初の部分をtitleに accountId = parts.length > 1 ? parts[1] : ""; // 2番目の部分をaccountIdに } - if(sortBy != null) sortBy = sort;//ソートはまだ + if(sort != null) sortBy = sort;//ソートはまだ Log.d(TAG, "Search query: " + title + accountId); if(title != ""|| accountId != ""){