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 420da7f..09d7db5 100644 --- a/app/src/main/java/com/example/citrusclient/rest/BooksRest.java +++ b/app/src/main/java/com/example/citrusclient/rest/BooksRest.java @@ -94,4 +94,16 @@ @Field("color") String color, @Field("token") String token ); + + @PUT("/{account_id}/books/{book_id}/favoriteCount") + Call registerFavoriteCount( + @Path("account_id") String account_id, + @Path("book_id") Integer book_id + ); + + @PUT("/{account_id}/books/{book_id}/favoriteCount") + Call unregisterFavoriteCount( + @Path("account_id") String account_id, + @Path("book_id") Integer book_id + ); } 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..30b2273 100644 --- a/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java +++ b/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java @@ -1,7 +1,9 @@ package com.example.citrusclient.rest; +import com.example.citrusclient.models.Book; + import java.util.HashMap; -import java.util.HashSet; +import java.util.List; import retrofit2.Call; import retrofit2.http.DELETE; @@ -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 ); @@ -24,7 +26,7 @@ //主{account_id}がいいねした誰か{other_account_id}の本の一覧 @FormUrlEncoded @GET("accounts/{account_id}/favorites/{other_account_id}") - Call> getFavoritesBooksById( + Call> getFavoritesBooksById( @Path("account_id") String accountId, @Path("other_account_id") String otherAccountId, @Query("token") String token @@ -33,7 +35,7 @@ //主{account_id}のある本{book_id}をいいねした人の一覧 @FormUrlEncoded @GET("accounts/{account_id}/books/{book_id}/favorited") - Call> getFavoritedAccount( + Call> getFavoritedAccount( @Path("account_id") String accountId, @Path("book_id") Integer bookId, @Query("token") String token 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 e96c043..4c309ba 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java @@ -192,6 +192,46 @@ }); } + private void registerFavoriteCount(String accountId, Integer bookId){ + Call call = booksRest.registerFavoriteCount(accountId, bookId); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("Success registerFavorited"); + } else { + System.out.println("response error"); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("NetWorkError" + t); + } + }); + } + + private void unregisterFavoriteCount(String accountId, Integer bookId){ + Call call = booksRest.unregisterFavoriteCount(accountId, bookId); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("Success unresterFavorited"); + } else { + System.out.println("response error"); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("NetWorkError" + t); + } + }); + } + private String parseStatusCode(Integer code) { switch (code) { case 404: 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 c51f485..b4c3842 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java @@ -1,9 +1,10 @@ package com.example.citrusclient.viewmodels; +import com.example.citrusclient.models.Book; import com.example.citrusclient.rest.FavoritesRest; import java.util.HashMap; -import java.util.HashSet; +import java.util.List; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; @@ -17,9 +18,9 @@ private final Retrofit retrofit; private final FavoritesRest favoritesRest; - private final MutableLiveData>> favoritesBooksLiveData; - private final MutableLiveData> favoritesBookIdLiveData; - private final MutableLiveData> favoritedAccountsLiveData; + private final MutableLiveData>> favoritesBooksLiveData; + private final MutableLiveData> favoritesBookIdLiveData; + private final MutableLiveData> favoritedAccountsLiveData; public FavoritesViewModel(){ this.retrofit = new Retrofit.Builder() @@ -31,16 +32,16 @@ this.favoritesBookIdLiveData = new MutableLiveData<>(); this.favoritedAccountsLiveData = new MutableLiveData<>(); } - public MutableLiveData>> getFavoritesBooksLiveData(){return favoritesBooksLiveData;} - public MutableLiveData> getFavoritesBookIdLiveData(){return favoritesBookIdLiveData;} - public MutableLiveData> getFavoritedAccountsLiveData(){return favoritedAccountsLiveData;} + 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()); @@ -48,17 +49,17 @@ } @Override - public void onFailure(Call>> call, Throwable t) { + public void onFailure(Call>> call, Throwable t) { System.out.println("NetworkError : getFavoritesBooks" + t); } }); } public void loadFavoritesBookId(String accountId, String otherAccountId, String token){ - Call> call = favoritesRest.getFavoritesBooksById(accountId,otherAccountId,token); - call.enqueue(new Callback>() { + Call> call = favoritesRest.getFavoritesBooksById(accountId,otherAccountId,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 : getFavoritesBooksById"); favoritesBookIdLiveData.setValue(response.body()); @@ -66,17 +67,17 @@ } @Override - public void onFailure(Call> call, Throwable t) { + public void onFailure(Call> call, Throwable t) { System.out.println("NetworkError : getFavoritesBooksById" + t); } }); } public void loadFavoritedAccounts(String accountId, Integer bookId, String token){ - Call> call = favoritesRest.getFavoritedAccount(accountId,bookId,token); - call.enqueue(new Callback>() { + Call> call = favoritesRest.getFavoritedAccount(accountId,bookId,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 : getFavoritedAccount"); favoritedAccountsLiveData.setValue(response.body()); @@ -84,7 +85,7 @@ } @Override - public void onFailure(Call> call, Throwable t) { + public void onFailure(Call> call, Throwable t) { System.out.println("NetworkError : getFavoritedAccount" + 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/HomeFragment.java b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java index 8c23744..cd900b8 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -98,6 +98,8 @@ month = citrus.getCurMonth(); day = citrus.getCurDay(); + modo = "todoSchedule"; + todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); @@ -126,6 +128,7 @@ int month; int day; + String modo; public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -139,29 +142,96 @@ todoList = new ArrayList<>(); scheduleList = new ArrayList<>(); + int todoWidth = 140; + int scheduleWidth = 170; + int modoButtonWidth = 180; + int wholeButtonWidth = 370; + Button curDateButton = view.findViewById(R.id.date_button); curDateButton.setText(year + "年" + month + "月" + day + "日"); - + //TodoAdapter設定 RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list); todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, getActivity()); todoRecyclerView.setAdapter(todoAdapter); - + todoAdapter.setItemWidth(dpToPx(todoWidth)); + //ScheduleAdapter設定 RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list); scheduleRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext()); scheduleRecyclerView.setLayoutManager(scheduleLayoutManager); MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap, curDate, getActivity()); scheduleRecyclerView.setAdapter(scheduleAdapter); + scheduleAdapter.setItemWidth(dpToPx(scheduleWidth)); + + //ボタン処理 Button dateButton = view.findViewById(R.id.date_button); dateButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new CalendarFragment(year, month)); }); + Button modoTodoButton = view.findViewById(R.id.modo_todo_button); + Button modoScheduleButton = view.findViewById(R.id.modo_schedule_button); + View verticalLine = view.findViewById(R.id.vertical_line); // IDを指定して取得 + + //上のTodoButtonを押したときTodoだけを表示 + modoTodoButton.setOnClickListener(v -> { + ViewGroup.LayoutParams todoButtonParams = modoTodoButton.getLayoutParams(); + ViewGroup.LayoutParams todoRecyclerViewLayoutParams = todoRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoScheduleButton.setVisibility(View.GONE); + scheduleRecyclerView.setVisibility(View.GONE); + todoButtonParams.width = dpToPx(wholeButtonWidth); // 幅を全体に + modoTodoButton.setLayoutParams(todoButtonParams); + todoRecyclerViewLayoutParams.width = dpToPx(wholeButtonWidth); + todoRecyclerView.setLayoutParams(todoRecyclerViewLayoutParams); + todoAdapter.setItemWidth(dpToPx(330)); + verticalLine.setVisibility(View.GONE); + modo = "todo"; + } else if(modo == "todo"){ + todoButtonParams.width = dpToPx(modoButtonWidth); // 幅を全体に + modoTodoButton.setLayoutParams(todoButtonParams); + todoRecyclerViewLayoutParams.width = dpToPx(modoButtonWidth); + todoRecyclerView.setLayoutParams(todoRecyclerViewLayoutParams); + modoScheduleButton.setVisibility(View.VISIBLE); + scheduleRecyclerView.setVisibility(View.VISIBLE); + todoAdapter.setItemWidth(dpToPx(todoWidth)); + verticalLine.setVisibility(View.VISIBLE); + modo = "todoSchedule"; + } + }); + + //上のScheduleButtonを押したときScheduleだけを表示 + modoScheduleButton.setOnClickListener(v -> { + ViewGroup.LayoutParams scheuleButtonParams = modoScheduleButton.getLayoutParams(); + ViewGroup.LayoutParams scheduleRecyclerViewLayoutParams = scheduleRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoTodoButton.setVisibility(View.GONE); + todoRecyclerView.setVisibility(View.GONE); + scheuleButtonParams.width = dpToPx(wholeButtonWidth); // 幅を全体に + modoScheduleButton.setLayoutParams(scheuleButtonParams); + scheduleRecyclerViewLayoutParams.width = dpToPx(wholeButtonWidth); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + scheduleAdapter.setItemWidth(dpToPx(360)); + verticalLine.setVisibility(View.GONE); + modo = "schedule"; + } else if(modo == "schedule"){ + scheuleButtonParams.width = dpToPx(modoButtonWidth); // 幅を全体に + modoScheduleButton.setLayoutParams(scheuleButtonParams); + scheduleRecyclerViewLayoutParams.width = dpToPx(modoButtonWidth); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + modoTodoButton.setVisibility(View.VISIBLE); + todoRecyclerView.setVisibility(View.VISIBLE); + scheduleAdapter.setItemWidth(dpToPx(scheduleWidth)); + verticalLine.setVisibility(View.VISIBLE); + modo = "todoSchedule"; + } + }); + FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button); todoAddButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new CreateTodoFragment(year, month, day)); @@ -172,7 +242,7 @@ ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment(year, month, day)); }); - ImageButton prevBotton = view.findViewById(R.id.prev_day_botton); + ImageButton prevBotton = view.findViewById(R.id.prev_day_button); prevBotton.setOnClickListener(v -> { curDate = curDate.minusDays(1); year = curDate.getYear(); @@ -184,7 +254,7 @@ updateTodoSchedule(integerBookHashMap); }); - ImageButton nextBotton = view.findViewById(R.id.next_day_botton); + ImageButton nextBotton = view.findViewById(R.id.next_day_button); nextBotton.setOnClickListener(v -> { curDate = curDate.plusDays(1); year = curDate.getYear(); @@ -196,10 +266,9 @@ updateTodoSchedule(integerBookHashMap); }); + booksViewModel.loadBooks(accountId, token);//ViewModel呼び出し - booksViewModel.loadBooks(accountId, token); - System.out.println(token); - + //ViewModel.observe(onChanged) booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(HashMap idBookHashMap) { @@ -270,7 +339,10 @@ scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); } - + // dp を px に変換するためのメソッド + private int dpToPx(int dp) { + return Math.round(dp * getResources().getDisplayMetrics().density); + } @Override public void onStop() { @@ -286,6 +358,7 @@ private HashMap idBookHashMap; private TodosViewModel todosViewModel; private Context context; + private int itemWidth; // アイテムの幅を保持 MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Context context) { @@ -325,6 +398,11 @@ String token = citrus.getToken(); String accountId = citrus.getAccountId(); + // アイテムの幅を設定 + ViewGroup.LayoutParams params = holder.todoButton.getLayoutParams(); + params.width = itemWidth; // 動的に設定した幅を使用 + holder.todoButton.setLayoutParams(params); + Todo todoData = this.todoList.get(position); holder.todoButton.setText(todoData.getTitle()); holder.todoCheckBox.setChecked(todoData.getCheck()); @@ -372,6 +450,11 @@ }); } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } + static class MyTodoViewHolder extends RecyclerView.ViewHolder { Button todoButton; @@ -390,6 +473,7 @@ private HashMap idBookHashMap; private Context context; private LocalDate curDate; + private int itemWidth; // アイテムの幅を保持 MyScheduleshelfAdapter(List schedules, HashMap idBookHashMap, LocalDate curDate, Context context) { this.scheduleList = schedules; @@ -424,6 +508,11 @@ public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { Activity activity = (Activity) context; + // アイテムの幅を設定 + ViewGroup.LayoutParams params = holder.scheduleButton.getLayoutParams(); + params.width = itemWidth; // 動的に設定した幅を使用 + holder.scheduleButton.setLayoutParams(params); + int red = 169; int green = 169; int blue = 169; @@ -511,6 +600,10 @@ notifyDataSetChanged(); // 必要に応じてアダプターをリフレッシュ } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } @Override public int getItemCount() { return scheduleList.size(); 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..4001914 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; @@ -67,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); @@ -77,6 +83,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 +98,7 @@ } - + BooksViewModel booksViewModel; private List scheduleList; ScheduleViewModel scheduleViewModel; TodosViewModel todosViewModel; @@ -101,9 +109,9 @@ int day; HashMap books; - + private Book book; HashMap> schedules; - HashMap> todos; + HashMap> todos; public TextView[][] calendar(int firstDayOfWeek, int prevMonthDay, int lastDay) { int curDay = 1; @@ -179,7 +187,22 @@ } }); - + 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); + } + } + }); todosViewModel.getTodosByMonthLiveData().observe(getViewLifecycleOwner(), new Observer>>() { @Override @@ -187,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)); } } } @@ -207,6 +228,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); //現在の月の最後の日付を取得 @@ -239,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); } }); @@ -253,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); } }); @@ -323,7 +351,7 @@ } citrus.setCurMonth(month + 1); } - ((MainActivity) getActivity()).showFragment(new HomeFragment()); + ((MainActivity) getActivity()).showFragment(new OtherHomeFragment()); } }); RecyclerView recyclerView = new RecyclerView(requireContext()); @@ -332,7 +360,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 @@ -386,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); } } @@ -398,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); } } @@ -410,36 +438,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 +485,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 5eed4ef..fb5656a 100644 --- a/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java @@ -91,6 +91,8 @@ month = citrus.getCurMonth(); day = citrus.getCurDay(); + modo = "todoSchedule"; + todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); @@ -128,6 +130,7 @@ int month; int day; + String modo; public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -144,30 +147,94 @@ todoList = new ArrayList<>(); scheduleList = new ArrayList<>(); + int todoWidth = 140; + int scheduleWidth = 170; + int modoButtonWidth = 180; + int wholeButtonWidth = 370; + Button curDateButton = view.findViewById(R.id.date_button); curDateButton.setText(year + "年" + month + "月" + day + "日"); - + //TodoAdapter設定 RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list); todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); MyOtherTodoshelfAdapter todoAdapter = new MyOtherTodoshelfAdapter(todoList, book, todosViewModel, getActivity()); todoRecyclerView.setAdapter(todoAdapter); - + todoAdapter.setItemWidth(dpToPx(todoWidth)); + //ScheduleAdapter設定 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, book, curDate, getActivity()); scheduleRecyclerView.setAdapter(scheduleAdapter); + scheduleAdapter.setItemWidth(dpToPx(scheduleWidth)); Button dateButton = view.findViewById(R.id.date_button); dateButton.setOnClickListener(v -> { - ((MainActivity) getActivity()).showFragment(new OtherCalendarFragment()); + ((MainActivity) getActivity()).showFragment(new OtherCalendarFragment(year, month)); }); - ImageButton prevBotton = view.findViewById(R.id.prev_day_botton); + Button modoTodoButton = view.findViewById(R.id.modo_todo_button); + Button modoScheduleButton = view.findViewById(R.id.modo_schedule_button); + View verticalLine = view.findViewById(R.id.vertical_line); // IDを指定して取得 + + //上のTodoButtonを押したときTodoだけを表示 + modoTodoButton.setOnClickListener(v -> { + ViewGroup.LayoutParams todoButtonParams = modoTodoButton.getLayoutParams(); + ViewGroup.LayoutParams todoRecyclerViewLayoutParams = todoRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoScheduleButton.setVisibility(View.GONE); + scheduleRecyclerView.setVisibility(View.GONE); + todoButtonParams.width = dpToPx(wholeButtonWidth); // 幅を全体に + modoTodoButton.setLayoutParams(todoButtonParams); + todoRecyclerViewLayoutParams.width = dpToPx(wholeButtonWidth); + todoRecyclerView.setLayoutParams(todoRecyclerViewLayoutParams); + todoAdapter.setItemWidth(dpToPx(330)); + verticalLine.setVisibility(View.GONE); + modo = "todo"; + } else if(modo == "todo"){ + todoButtonParams.width = dpToPx(modoButtonWidth); // 幅を全体に + modoTodoButton.setLayoutParams(todoButtonParams); + todoRecyclerViewLayoutParams.width = dpToPx(modoButtonWidth); + todoRecyclerView.setLayoutParams(todoRecyclerViewLayoutParams); + modoScheduleButton.setVisibility(View.VISIBLE); + scheduleRecyclerView.setVisibility(View.VISIBLE); + todoAdapter.setItemWidth(dpToPx(todoWidth)); + verticalLine.setVisibility(View.VISIBLE); + modo = "todoSchedule"; + } + }); + + //上のScheduleButtonを押したときScheduleだけを表示 + modoScheduleButton.setOnClickListener(v -> { + ViewGroup.LayoutParams scheuleButtonParams = modoScheduleButton.getLayoutParams(); + ViewGroup.LayoutParams scheduleRecyclerViewLayoutParams = scheduleRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoTodoButton.setVisibility(View.GONE); + todoRecyclerView.setVisibility(View.GONE); + scheuleButtonParams.width = dpToPx(wholeButtonWidth); // 幅を全体に + modoScheduleButton.setLayoutParams(scheuleButtonParams); + scheduleRecyclerViewLayoutParams.width = dpToPx(wholeButtonWidth); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + scheduleAdapter.setItemWidth(dpToPx(360)); + verticalLine.setVisibility(View.GONE); + modo = "schedule"; + } else if(modo == "schedule"){ + scheuleButtonParams.width = dpToPx(modoButtonWidth); // 幅を全体に + modoScheduleButton.setLayoutParams(scheuleButtonParams); + scheduleRecyclerViewLayoutParams.width = dpToPx(modoButtonWidth); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + modoTodoButton.setVisibility(View.VISIBLE); + todoRecyclerView.setVisibility(View.VISIBLE); + scheduleAdapter.setItemWidth(dpToPx(scheduleWidth)); + verticalLine.setVisibility(View.VISIBLE); + modo = "todoSchedule"; + } + }); + ImageButton prevBotton = view.findViewById(R.id.prev_day_button); prevBotton.setOnClickListener(v -> { curDate = curDate.minusDays(1); year = curDate.getYear(); @@ -181,7 +248,7 @@ updateTodoSchedule(book); }); - ImageButton nextBotton = view.findViewById(R.id.next_day_botton); + ImageButton nextBotton = view.findViewById(R.id.next_day_button); nextBotton.setOnClickListener(v -> { curDate = curDate.plusDays(1); year = curDate.getYear(); @@ -195,10 +262,14 @@ }); + ImageButton backButton = view.findViewById(R.id.back_button); + backButton.setOnClickListener(v -> { + ((MainActivity) getActivity()).backFragment(); + }); + 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); +// todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); +// scheduleViewModel.updateSchedulesByDayAndBookId(accountId, year, month, day, token, curBookId); booksViewModel.getBook().observe(getViewLifecycleOwner(), new Observer() { @Override @@ -232,8 +303,6 @@ } } }); - - } @@ -247,7 +316,7 @@ this.book = book; } todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); - scheduleViewModel.updateSchedulesByDayAndBookId(accountId, year, month, day, token, curBookId); + scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); } private void clearTodoSchedule(MyOtherTodoshelfAdapter todoAdapter, MyOtherScheduleshelfAdapter scheduleAdapter) { @@ -257,7 +326,10 @@ scheduleAdapter.setSchedules(scheduleList, book); } - + // dp を px に変換するためのメソッド + private int dpToPx(int dp) { + return Math.round(dp * getResources().getDisplayMetrics().density); + } @Override public void onStop() { @@ -273,6 +345,7 @@ private Book book; private TodosViewModel todosViewModel; private Context context; + private int itemWidth; // アイテムの幅を保持 MyOtherTodoshelfAdapter(List todos, Book book, TodosViewModel todosViewModel, Context context) { @@ -304,17 +377,22 @@ // String token = citrus.getToken(); // String accountId = citrus.getCurLookingAccountId(); + // アイテムの幅を設定 + ViewGroup.LayoutParams params = holder.todoButton.getLayoutParams(); + params.width = itemWidth; // 動的に設定した幅を使用 + holder.todoButton.setLayoutParams(params); + Todo todoData = this.todoList.get(position); holder.todoButton.setText(todoData.getTitle()); holder.todoCheckBox.setChecked(todoData.getCheck()); 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); - int blue = Integer.parseInt(book.getColor().substring(5, 7), 16); - holder.todoButton.setBackgroundColor(Color.rgb(red, green, blue)); - holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); - } + + + int red = Integer.parseInt(book.getColor().substring(1, 3), 16); + int green = Integer.parseInt(book.getColor().substring(3, 5), 16); + int blue = Integer.parseInt(book.getColor().substring(5, 7), 16); + holder.todoButton.setBackgroundColor(Color.rgb(red, green, blue)); + holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); // holder.todoButton.setOnClickListener(v -> { // ((MainActivity) activity).showFragment(new CreateTodoFragment(todoData, book)); @@ -333,11 +411,6 @@ } - @Override - public int getItemCount() { - return todoList.size(); - } - private void sortTodosById() { Collections.sort(todoList, new Comparator() { @Override @@ -351,6 +424,15 @@ }); } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } + + @Override + public int getItemCount() { + return todoList.size(); + } static class MyTodoViewHolder extends RecyclerView.ViewHolder { Button todoButton; @@ -369,6 +451,7 @@ Book book; private Context context; private LocalDate curDate; + private int itemWidth; // アイテムの幅を保持 MyOtherScheduleshelfAdapter(List schedules, Book book, LocalDate curDate, Context context) { this.scheduleList = schedules; @@ -395,13 +478,18 @@ public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { // Activity activity = (Activity) context; + // アイテムの幅を設定 + ViewGroup.LayoutParams params = holder.scheduleButton.getLayoutParams(); + params.width = itemWidth; // 動的に設定した幅を使用 + holder.scheduleButton.setLayoutParams(params); + int red = 169; int green = 169; int blue = 169; 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)); - if(book != null) { + if(book.getBookId() == scheduleData.getBookId()) { red = Integer.parseInt(book.getColor().substring(1, 3), 16); green = Integer.parseInt(book.getColor().substring(3, 5), 16); blue = Integer.parseInt(book.getColor().substring(5, 7), 16); @@ -481,6 +569,11 @@ notifyDataSetChanged(); // 必要に応じてアダプターをリフレッシュ } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } + @Override public int getItemCount() { return scheduleList.size(); 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 != ""){ diff --git a/app/src/main/res/drawable/baseline_arrow_back_24.xml b/app/src/main/res/drawable/baseline_arrow_back_24.xml new file mode 100644 index 0000000..075e95d --- /dev/null +++ b/app/src/main/res/drawable/baseline_arrow_back_24.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/vertical_dotted_line.xml b/app/src/main/res/drawable/vertical_dotted_line.xml new file mode 100644 index 0000000..1f09ce1 --- /dev/null +++ b/app/src/main/res/drawable/vertical_dotted_line.xml @@ -0,0 +1,10 @@ + + + + + + \ 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 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">