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 6a8a305..38654e6 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -139,7 +139,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - Citrus citrus = (Citrus)(getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getAccountId(); @@ -157,27 +156,29 @@ todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); - MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, citrus, getActivity()); + MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, 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); - MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap); + MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap, curDate, getActivity()); scheduleRecyclerView.setAdapter(scheduleAdapter); + Button dateButton = view.findViewById(R.id.date_button); + dateButton.setOnClickListener(v -> { + ((MainActivity) getActivity()).showFragment(new CalendarFragment()); + }); FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button); todoAddButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new CreateTodoFragment(year, month, day)); - todoAdapter.notifyDataSetChanged(); }); FloatingActionButton scheduleAddButton = view.findViewById(R.id.schedule_add_button); scheduleAddButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment()); - scheduleAdapter.notifyDataSetChanged(); }); ImageButton prevBotton = view.findViewById(R.id.prev_day_botton); @@ -189,8 +190,6 @@ curDateButton.setText(year + "年" + month + "月" + day + "日"); updateTodoSchedule(integerBookHashMap); - todoAdapter.notifyDataSetChanged(); - scheduleAdapter.notifyDataSetChanged(); }); ImageButton nextBotton = view.findViewById(R.id.next_day_botton); @@ -201,8 +200,6 @@ day = curDate.getDayOfMonth(); curDateButton.setText(year + "年" + month + "月" + day + "日"); updateTodoSchedule(integerBookHashMap); - todoAdapter.notifyDataSetChanged(); - scheduleAdapter.notifyDataSetChanged(); }); @@ -215,8 +212,6 @@ @Override public void onChanged(HashMap idBookHashMap) { updateTodoSchedule(idBookHashMap); - todoAdapter.notifyDataSetChanged(); - scheduleAdapter.notifyDataSetChanged(); } }); @@ -227,14 +222,12 @@ for(Todo todo : idTodoHashMap.values()){ if(!todo.containsTodo(todoList)){ todoList.add(todo); - todoAdapter.setTodos(todoList, integerBookHashMap); } } todoAdapter.setTodos(todoList, integerBookHashMap); } else { todoAdapter.setTodos(todoList, integerBookHashMap); } - todoAdapter.notifyDataSetChanged(); } }); @@ -248,7 +241,6 @@ scheduleList = new ArrayList<>(); scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); } - scheduleAdapter.notifyDataSetChanged(); } }); @@ -296,17 +288,10 @@ private List todoList; private HashMap idBookHashMap; private TodosViewModel todosViewModel; - private Citrus citrus; - private String accountId; - private String token; private Context context; - - MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Citrus citrus, Context context) { - this.citrus = citrus; - this.token = citrus.getToken(); - this.accountId = citrus.getAccountId(); + MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Context context) { this.todoList = todos; this.todosViewModel = todosViewModel; if (idBookHashMap != null) { @@ -315,7 +300,6 @@ this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 } this.context = context; - notifyDataSetChanged(); } public void setTodos(List todos ,HashMap idBookHashMap) { @@ -339,6 +323,11 @@ @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.getAccountId(); + Todo todoData = this.todoList.get(position); holder.todoButton.setText(todoData.getTitle()); holder.todoCheckBox.setChecked(todoData.getCheck()); @@ -352,9 +341,7 @@ } holder.todoButton.setOnClickListener(v -> { - Activity activity = (Activity) context; ((MainActivity) activity).showFragment(new CreateTodoFragment(todoData, book)); - notifyDataSetChanged(); }); holder.todoCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> { @@ -386,7 +373,6 @@ return Integer.compare(todo1.getTodoId(), todo2.getTodoId()); // 同じbookIdの場合、todoIdで比較 } }); - notifyDataSetChanged(); // データ変更をアダプタに通知 } @@ -405,14 +391,19 @@ private List scheduleList; private HashMap idBookHashMap; + private Context context; + private LocalDate curDate; - MyScheduleshelfAdapter(List schedules, HashMap idBookHashMap) { + MyScheduleshelfAdapter(List schedules, HashMap idBookHashMap, LocalDate curDate, Context context) { this.scheduleList = schedules; if (idBookHashMap != null) { this.idBookHashMap = new HashMap<>(idBookHashMap); } else { this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } } + } + this.curDate = curDate; + this.context = context; + } public void setSchedules(List schedules, HashMap idBookHashMap) { scheduleList = schedules; @@ -434,12 +425,14 @@ @Override public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { + Activity activity = (Activity) context; + 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()) + " ~ " + extractionTime(scheduleData.getEndTime())); + 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); @@ -449,8 +442,13 @@ 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()); + }); } + + private void sortScheduleByTime() { Collections.sort(scheduleList, new Comparator() { @Override @@ -470,7 +468,6 @@ // StringをLocalDateTimeに変換 LocalDateTime dateTime = LocalDateTime.parse(scheduleTime, formatter); - System.out.println(dateTime); // 年、月、日、時、分を抽出 long year = dateTime.getYear(); @@ -478,11 +475,9 @@ long day = dateTime.getDayOfMonth(); long hour = dateTime.getHour(); long minute = dateTime.getMinute(); - System.out.println(year); // 必要に応じて整数を組み合わせる(例: YYYYMMDDHHMM) long result = year *100000000 +month * 1000000 + day * 10000 + hour * 100 + minute; - System.out.println(result); return result; } catch (DateTimeParseException e) { @@ -491,10 +486,25 @@ } } - private static String extractionTime(String scheduleTime) { + private static String extractionTime(String scheduleTime, LocalDate curDate) { + int year = curDate.getYear(); + int month = curDate.getMonthValue(); + int day = curDate.getDayOfMonth(); + long yearMonth = year * 10000 + month * 100 + day; + long scheduleYearMonth = extractUpperDigits(convertTimeInteger(scheduleTime), 8); +// System.out.println(yearMonth); +// System.out.println(scheduleYearMonth); return scheduleTime.substring(scheduleTime.length() - 5); } + public static long extractUpperDigits(long number, int digitCount) { + // 10の累乗を計算 + long divisor = (long) Math.pow(10, String.valueOf(number).length() - digitCount); +// System.out.println(divisor); +// System.out.println(number / divisor); + return number / divisor; // 上位の桁を抽出 + } + @Override public int getItemCount() { return scheduleList.size();