diff --git a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java index 0b808fe..8c23744 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -105,15 +105,6 @@ } @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt("year", year); - outState.putInt("month", month); - outState.putInt("day", day); - } - - - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment @@ -188,9 +179,8 @@ month = curDate.getMonthValue(); day = curDate.getDayOfMonth(); curDateButton.setText(year + "年" + month + "月" + day + "日"); - scheduleList.clear(); - scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); scheduleAdapter.updateCurDate(curDate); + clearTodoSchedule(todoAdapter, scheduleAdapter); updateTodoSchedule(integerBookHashMap); }); @@ -201,11 +191,9 @@ month = curDate.getMonthValue(); day = curDate.getDayOfMonth(); curDateButton.setText(year + "年" + month + "月" + day + "日"); - scheduleList.clear(); - scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); scheduleAdapter.updateCurDate(curDate); + clearTodoSchedule(todoAdapter, scheduleAdapter); updateTodoSchedule(integerBookHashMap); - }); @@ -215,6 +203,7 @@ booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(HashMap idBookHashMap) { + clearTodoSchedule(todoAdapter, scheduleAdapter); updateTodoSchedule(idBookHashMap); } }); @@ -259,7 +248,6 @@ int curBookId = citrus.getCurBookId(); if (idBookHashMap != null) { - todoList.clear(); if(curBookId != -1) { integerBookHashMap = new HashMap<>(); Book book = idBookHashMap.get(curBookId); @@ -275,6 +263,13 @@ scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); } + private void clearTodoSchedule(MyTodoshelfAdapter todoAdapter, MyScheduleshelfAdapter scheduleAdapter) { + todoList.clear(); + todoAdapter.setTodos(todoList, integerBookHashMap); + scheduleList.clear(); + scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); + } + @Override diff --git a/app/src/main/java/com/example/citrusclient/views/MainActivity.java b/app/src/main/java/com/example/citrusclient/views/MainActivity.java index ae85791..455cb29 100644 --- a/app/src/main/java/com/example/citrusclient/views/MainActivity.java +++ b/app/src/main/java/com/example/citrusclient/views/MainActivity.java @@ -78,6 +78,11 @@ } else if(itemId == R.id.search){//検索 showFragment(new SearchFragment()); } else if(itemId == R.id.home){//ホーム + citrus = (Citrus) getApplication(); + Calendar calendar = Calendar.getInstance(); + citrus.setCurYear(calendar.get(Calendar.YEAR)); + citrus.setCurMonth(calendar.get(Calendar.MONTH) + 1); + citrus.setCurDay(calendar.get(Calendar.DATE)); citrus.setCurBookId(-1); showFragment(new HomeFragment()); }else if(itemId == R.id.calendar){ //カレンダ 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/OtherHomeFragment.java b/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java index 4279181..5eed4ef 100644 --- a/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java @@ -111,16 +111,16 @@ Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_home, container, false); + return inflater.inflate(R.layout.fragment_other_home, container, false); } - private List todoList; + BooksViewModel booksViewModel; TodosViewModel todosViewModel; - private List scheduleList; ScheduleViewModel scheduleViewModel; - private HashMap integerBookHashMap; - BooksViewModel booksViewModel; + private Book book; + private List todoList; + private List scheduleList; LocalDate curDate; @@ -135,6 +135,9 @@ Citrus citrus = (Citrus)(getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getCurLookingAccountId(); + + int curBookId = citrus.getCurLookingBookId(); + System.out.println(token); curDate = LocalDate.of(year, month, day); @@ -149,14 +152,14 @@ todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); - MyOtherTodoshelfAdapter todoAdapter = new MyOtherTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, getActivity()); + MyOtherTodoshelfAdapter todoAdapter = new MyOtherTodoshelfAdapter(todoList, book, todosViewModel, getActivity()); todoRecyclerView.setAdapter(todoAdapter); RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list); scheduleRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext()); scheduleRecyclerView.setLayoutManager(scheduleLayoutManager); - MyOtherScheduleshelfAdapter scheduleAdapter = new MyOtherScheduleshelfAdapter(scheduleList, integerBookHashMap, curDate, getActivity()); + MyOtherScheduleshelfAdapter scheduleAdapter = new MyOtherScheduleshelfAdapter(scheduleList, book, curDate, getActivity()); scheduleRecyclerView.setAdapter(scheduleAdapter); Button dateButton = view.findViewById(R.id.date_button); @@ -171,9 +174,11 @@ month = curDate.getMonthValue(); day = curDate.getDayOfMonth(); curDateButton.setText(year + "年" + month + "月" + day + "日"); - + scheduleList.clear(); scheduleAdapter.updateCurDate(curDate); - updateTodoSchedule(integerBookHashMap); + + clearTodoSchedule(todoAdapter, scheduleAdapter); + updateTodoSchedule(book); }); ImageButton nextBotton = view.findViewById(R.id.next_day_botton); @@ -183,21 +188,23 @@ month = curDate.getMonthValue(); day = curDate.getDayOfMonth(); curDateButton.setText(year + "年" + month + "月" + day + "日"); - scheduleAdapter.updateCurDate(curDate); - updateTodoSchedule(integerBookHashMap); + + clearTodoSchedule(todoAdapter, scheduleAdapter); + updateTodoSchedule(book); }); - - booksViewModel.loadBooks(accountId, token); - scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); + booksViewModel.loadBook(accountId, token, curBookId); + todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); + scheduleViewModel.updateSchedulesByDayAndBookId(accountId, year, month, day, token, curBookId); System.out.println(token); - booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { + booksViewModel.getBook().observe(getViewLifecycleOwner(), new Observer() { @Override - public void onChanged(HashMap idBookHashMap) { - updateTodoSchedule(idBookHashMap); + public void onChanged(Book book) { + clearTodoSchedule(todoAdapter, scheduleAdapter); + updateTodoSchedule(book); } }); @@ -205,14 +212,10 @@ @Override public void onChanged(HashMap idTodoHashMap) { if(idTodoHashMap != null) { - for(Todo todo : idTodoHashMap.values()){ - if(!todo.containsTodo(todoList)){ - todoList.add(todo); - } - } - todoAdapter.setTodos(todoList, integerBookHashMap); + todoList = new ArrayList<>(idTodoHashMap.values()); + todoAdapter.setTodos(todoList, book); } else { - todoAdapter.setTodos(todoList, integerBookHashMap); + todoAdapter.setTodos(todoList, book); } } }); @@ -222,10 +225,10 @@ public void onChanged(HashMap idScheduleHashMap) { if(idScheduleHashMap != null) { scheduleList = new ArrayList<>(idScheduleHashMap.values()); - scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); + scheduleAdapter.setSchedules(scheduleList, book); } else { scheduleList = new ArrayList<>(); - scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); + scheduleAdapter.setSchedules(scheduleList, book); } } }); @@ -234,28 +237,24 @@ } - private void updateTodoSchedule(HashMap idBookHashMap) { + private void updateTodoSchedule(Book book) { Citrus citrus = (Citrus)(getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getCurLookingAccountId(); int curBookId = citrus.getCurLookingBookId(); - if (idBookHashMap != null) { - if(curBookId != -1) { - todoList = new ArrayList<>(); - integerBookHashMap = new HashMap<>(); - Book book = idBookHashMap.get(curBookId); - integerBookHashMap.put(curBookId, book); - todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); - } else { - todoList = new ArrayList<>(); - integerBookHashMap = new HashMap<>(idBookHashMap); - for (Book book : integerBookHashMap.values()) { - todosViewModel.loadTodosByDay(accountId, book.getBookId(), year, month, day, token); - } - } + if (book != null) { + this.book = book; } - scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); + todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); + scheduleViewModel.updateSchedulesByDayAndBookId(accountId, year, month, day, token, curBookId); + } + + private void clearTodoSchedule(MyOtherTodoshelfAdapter todoAdapter, MyOtherScheduleshelfAdapter scheduleAdapter) { + todoList.clear(); + todoAdapter.setTodos(todoList, book); + scheduleList.clear(); + scheduleAdapter.setSchedules(scheduleList, book); } @@ -271,29 +270,21 @@ class MyOtherTodoshelfAdapter extends RecyclerView.Adapter { private List todoList; - private HashMap idBookHashMap; + private Book book; private TodosViewModel todosViewModel; private Context context; - MyOtherTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Context context) { + MyOtherTodoshelfAdapter(List todos, Book book, TodosViewModel todosViewModel, Context context) { this.todoList = todos; this.todosViewModel = todosViewModel; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + this.book = book; this.context = context; } - public void setTodos(List todos ,HashMap idBookHashMap) { - todoList = todos; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + public void setTodos(List todos ,Book book) { + this.todoList = todos; + this.book = book; sortTodosById(); notifyDataSetChanged(); @@ -308,15 +299,15 @@ @Override public void onBindViewHolder(@NonNull MyTodoViewHolder holder, int position) { - Activity activity = (Activity) context; - Citrus citrus = (Citrus)(activity.getApplication()); - String token = citrus.getToken(); - String accountId = citrus.getCurLookingAccountId(); +// Activity activity = (Activity) context; +// Citrus citrus = (Citrus)(activity.getApplication()); +// String token = citrus.getToken(); +// String accountId = citrus.getCurLookingAccountId(); Todo todoData = this.todoList.get(position); holder.todoButton.setText(todoData.getTitle()); holder.todoCheckBox.setChecked(todoData.getCheck()); - Book book = idBookHashMap.get(todoData.getBookId()); + holder.todoCheckBox.setEnabled(false); if(book != null) { int red = Integer.parseInt(book.getColor().substring(1, 3), 16); int green = Integer.parseInt(book.getColor().substring(3, 5), 16); @@ -325,20 +316,20 @@ holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); } - holder.todoButton.setOnClickListener(v -> { - ((MainActivity) activity).showFragment(new CreateTodoFragment(todoData, book)); - }); - - holder.todoCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> { - // チェック状態が変更された時の処理を記述 - if (isChecked) { - // チェックされた時の処理 - todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), true, token); - } else { - // チェックが外された時の処理 - todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), false, token); - } - }); +// holder.todoButton.setOnClickListener(v -> { +// ((MainActivity) activity).showFragment(new CreateTodoFragment(todoData, book)); +// }); +// +// holder.todoCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> { +// // チェック状態が変更された時の処理を記述 +// if (isChecked) { +// // チェックされた時の処理 +// todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), true, token); +// } else { +// // チェックが外された時の処理 +// todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), false, token); +// } +// }); } @@ -375,28 +366,20 @@ class MyOtherScheduleshelfAdapter extends RecyclerView.Adapter { private List scheduleList; - private HashMap idBookHashMap; + Book book; private Context context; private LocalDate curDate; - MyOtherScheduleshelfAdapter(List schedules, HashMap idBookHashMap, LocalDate curDate, Context context) { + MyOtherScheduleshelfAdapter(List schedules, Book book, LocalDate curDate, Context context) { this.scheduleList = schedules; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + this.book = book; this.curDate = curDate; this.context = context; } - public void setSchedules(List schedules, HashMap idBookHashMap) { + public void setSchedules(List schedules, Book book) { scheduleList = schedules; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + this.book = book; sortScheduleByTime(); notifyDataSetChanged(); } @@ -410,7 +393,7 @@ @Override public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { - Activity activity = (Activity) context; +// Activity activity = (Activity) context; int red = 169; int green = 169; @@ -418,7 +401,6 @@ Schedule scheduleData = this.scheduleList.get(position); holder.scheduleButton.setText(scheduleData.getTitle()); holder.scheduleButton.setText(scheduleData.getTitle() + "\n" + extractionTime(scheduleData.getStartTime(), curDate) + " ~ " + extractionTime(scheduleData.getEndTime(), curDate)); - Book book = idBookHashMap.get(scheduleData.getBookId()); if(book != null) { red = Integer.parseInt(book.getColor().substring(1, 3), 16); green = Integer.parseInt(book.getColor().substring(3, 5), 16); @@ -427,9 +409,9 @@ holder.scheduleButton.setBackgroundColor(Color.rgb(red, green, blue)); holder.scheduleButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); - holder.scheduleButton.setOnClickListener(v -> { - ((MainActivity) activity).showFragment(new CreateScheduleFragment(scheduleData, book)); - }); +// holder.scheduleButton.setOnClickListener(v -> { +// ((MainActivity) activity).showFragment(new CreateScheduleFragment(scheduleData, 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 928bd04..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; /** @@ -53,7 +57,7 @@ private final SearchFragment self = this; private SearchView searchView; - private String searchWord; + private String searchWord=""; private MyAdapter adapter; private RecyclerView recyclerView; private RecyclerView recyclerView1; // sort_by用のRecyclerView @@ -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); @@ -130,12 +135,14 @@ public void onItemSelected(AdapterView parent, View view, int position, long id) { // positionに基づいてソートの処理を行う sortBy = position; // 例えば、0が"Sort by Title"、1が"Sort by Author"など + System.out.println(position); loadSearchResults(searchWord, sortBy); } @Override public void onNothingSelected(AdapterView parent) { // 何も選択されていない場合の処理(必要に応じて) + loadSearchResults(searchWord, sortBy); } }); @@ -145,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); @@ -162,7 +180,10 @@ @Override public boolean onQueryTextChange(String newText) { - adapter.filter(newText); // フィルタリングを実行 +// adapter.filter(newText); // フィルタリングを実行 + searchWord = newText; + loadSearchResults(searchWord, sortBy); + Log.d(TAG, "Search query: " + newText); return true; } }); @@ -176,16 +197,16 @@ 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 != "") { - if (accountId != "") { + if(title != ""|| accountId != ""){ + if(sort != null) { publicBooksViewModel.loadSearchBooks(title, accountId, sortBy);//title,accountidでの検索 System.out.println("title: "+title+" accountId: "+accountId+" sortBy: "+sortBy); - }else { - publicBooksViewModel.loadAllBooks(); } + }else { + publicBooksViewModel.loadAllBooks(); // titleとaccountIdを使って検索 } }