diff --git a/app/src/main/java/com/example/citrusclient/models/Todo.java b/app/src/main/java/com/example/citrusclient/models/Todo.java index 10a9733..8458c6b 100644 --- a/app/src/main/java/com/example/citrusclient/models/Todo.java +++ b/app/src/main/java/com/example/citrusclient/models/Todo.java @@ -1,5 +1,7 @@ package com.example.citrusclient.models; +import java.util.List; + public class Todo { String title; @@ -50,5 +52,12 @@ } public Integer getBookId(){return bookId;} - + public boolean containsTodo(List todoList){ + for(Todo todo : todoList) { + if(todo.bookId == this.bookId && todo.year == this.year && todo.month == this.month && todo.day == this.day && todo.todoId == this.todoId) { + return true; + } + } + return false; + } } 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 a73523a..1006c55 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -86,12 +86,24 @@ mParam2 = getArguments().getString(ARG_PARAM2); } + Citrus citrus = (Citrus)(getActivity().getApplication()); + year = citrus.getCurYear(); + month = citrus.getCurMonth(); + day = citrus.getCurDay(); + todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); } + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putInt("year", year); + outState.putInt("month", month); + outState.putInt("day", day); + } @Override @@ -110,11 +122,13 @@ private HashMap integerBookHashMap; BooksViewModel booksViewModel; - private LocalDate openDate; - int year; + LocalDate curDate; + + int year ; int month; int day; + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -122,19 +136,14 @@ Citrus citrus = (Citrus)(getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getAccountId(); - year = citrus.getCurYear(); - month = citrus.getCurMonth(); - day = citrus.getCurDay(); - int curBookId = citrus.getCurBookId(); - - openDate = LocalDate.of(year, month, day); + curDate = LocalDate.of(year, month, day); todoList = new ArrayList<>(); scheduleList = new ArrayList<>(); - TextView curDate = view.findViewById(R.id.year_month_day); - curDate.setText(year + "年" + month + "月" + day + "日"); + TextView curDateText = view.findViewById(R.id.year_month_day); + curDateText.setText(year + "年" + month + "月" + day + "日"); RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list); @@ -152,28 +161,46 @@ scheduleRecyclerView.setAdapter(scheduleAdapter); + FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button); + todoAddButton.setOnClickListener(v -> { + ((MainActivity) getActivity()).showFragment(new CreateTodoFragment()); + }); + + FloatingActionButton scheduleAddButton = view.findViewById(R.id.schedule_add_button); + scheduleAddButton.setOnClickListener(v -> { + ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment()); + }); + + FloatingActionButton prevBotton = view.findViewById(R.id.prev_day_botton); + prevBotton.setOnClickListener(v -> { + curDate = curDate.minusDays(1); + year = curDate.getYear(); + month = curDate.getMonthValue(); + day = curDate.getDayOfMonth(); + curDateText.setText(year + "年" + month + "月" + day + "日"); + + updateTodoSchedule(integerBookHashMap); + }); + + FloatingActionButton nextBotton = view.findViewById(R.id.next_day_botton); + nextBotton.setOnClickListener(v -> { + curDate = curDate.plusDays(1); + year = curDate.getYear(); + month = curDate.getMonthValue(); + day = curDate.getDayOfMonth(); + curDateText.setText(year + "年" + month + "月" + day + "日"); + updateTodoSchedule(integerBookHashMap); + }); + + booksViewModel.loadBooks(accountId, token); + scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); System.out.println(token); booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(HashMap idBookHashMap) { - if (idBookHashMap != null) { - if(curBookId != -1) { - integerBookHashMap = new HashMap<>(); - Book book = idBookHashMap.get(curBookId); - integerBookHashMap.put(curBookId, book); - todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); - citrus.setCurBookId(-1); - } else { - todoList = new ArrayList<>(); - integerBookHashMap = new HashMap<>(idBookHashMap); - for (Book book : integerBookHashMap.values()) { - todosViewModel.loadTodosByDay(accountId, book.getBookId(), year, month, day, token); - } - } - scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); - } + updateTodoSchedule(idBookHashMap); } }); @@ -181,7 +208,13 @@ @Override public void onChanged(HashMap idTodoHashMap) { if(idTodoHashMap != null) { - todoList.addAll(idTodoHashMap.values()); + for(Todo todo : idTodoHashMap.values()){ + if(!todo.containsTodo(todoList)){ + todoList.add(todo); + todoAdapter.setTodos(todoList, integerBookHashMap); + } + } + } else { todoAdapter.setTodos(todoList, integerBookHashMap); } } @@ -193,30 +226,51 @@ if(idScheduleHashMap != null) { scheduleList = new ArrayList<>(idScheduleHashMap.values()); scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); + } else { + scheduleList = new ArrayList<>(); + scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); } } }); - FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button); - todoAddButton.setOnClickListener(v -> { - ((MainActivity) getActivity()).showFragment(new CreateTodoFragment()); - }); - - FloatingActionButton scheduleAddButton = view.findViewById(R.id.schedule_add_button); - scheduleAddButton.setOnClickListener(v -> { - ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment()); - }); - -// FloatingActionButton prevBotton = view.findViewById(R.id.prev_day_botton); -// prevBotton.setOnClickListener(v -> { -// openDate = openDate.minusDays(1); -// year = openDate.getYear(); -// month = openDate.getMonthValue(); -// day = openDate.getDayOfMonth(); -// }); } + + private void updateTodoSchedule(HashMap idBookHashMap) { + Citrus citrus = (Citrus)(getActivity().getApplication()); + String token = citrus.getToken(); + String accountId = citrus.getAccountId(); + int curBookId = citrus.getCurBookId(); + + 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); + citrus.setCurBookId(-1); + } else { + todoList = new ArrayList<>(); + integerBookHashMap = new HashMap<>(idBookHashMap); + for (Book book : integerBookHashMap.values()) { + todosViewModel.loadTodosByDay(accountId, book.getBookId(), year, month, day, token); + } + } + } + scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); + } + + + + @Override + public void onStop() { + super.onStop(); + booksViewModel.getBookLiveData().removeObservers(getViewLifecycleOwner()); + } + + } class MyTodoshelfAdapter extends RecyclerView.Adapter {