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/rest/FavoritesRest.java b/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java new file mode 100644 index 0000000..8cf4caa --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/rest/FavoritesRest.java @@ -0,0 +1,4 @@ +package com.example.citrusclient.rest; + +public interface FavoritesRest { +} diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/ScheduleViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/ScheduleViewModel.java index 7c49525..700826f 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/ScheduleViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/ScheduleViewModel.java @@ -182,7 +182,8 @@ if(response.isSuccessful()){ schedulesByMonth.setValue(response.body()); } else { - errorLiveData.setValue(response.message()); + schedulesByMonth.setValue(new HashMap<>()); + errorLiveData.setValue(response.message()); } } @Override diff --git a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java index cd80709..92d4908 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java @@ -21,6 +21,7 @@ import com.example.citrusclient.Citrus; import com.example.citrusclient.R; import com.example.citrusclient.models.Book; +import com.example.citrusclient.models.Todo; import com.example.citrusclient.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.ScheduleViewModel; import com.example.citrusclient.viewmodels.TodosViewModel; @@ -41,6 +42,8 @@ private TodosViewModel todosViewModel; private BooksViewModel booksViewModel; + private Todo todo; + // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; @@ -57,8 +60,32 @@ private String token; private int selectBookNum; - public CreateTodoFragment() { - // Required empty public constructor + //編集用 + private boolean isEdit; + private Todo editTodo; + private int viwYear; + private int viwMonth; + private int viwDay; + private String viwTitle; + + //追加用コンストラクタ + public CreateTodoFragment(int viwYear, int viwMonth, int viwDay) { + this.isEdit = false; + this.viwYear = viwYear; + this.viwMonth = viwMonth; + this.viwDay = viwDay; + this.viwTitle = ""; + } + + //編集用コンストラクタ + public CreateTodoFragment(Todo editTodo) { + this.editTodo = editTodo; + this.isEdit = true; + this.viwYear = editTodo.getYear(); + this.viwMonth = editTodo.getMonth(); + this.viwDay = editTodo.getDay(); + this.viwTitle = editTodo.getTitle(); + } /** @@ -71,7 +98,7 @@ */ // TODO: Rename and change types and number of parameters public static CreateTodoFragment newInstance(String param1, String param2) { - CreateTodoFragment fragment = new CreateTodoFragment(); + CreateTodoFragment fragment = new CreateTodoFragment(0,0,0); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); @@ -111,17 +138,17 @@ NumberPicker numberPickerYear = view.findViewById(R.id.numberPickerYear); numberPickerYear.setMinValue(citrus.getCurYear()); numberPickerYear.setMaxValue(citrus.getCurYear() + 30); - numberPickerYear.setValue(citrus.getCurYear()); // 年の初期値を設定 + numberPickerYear.setValue(viwYear); // 年の初期値を設定 NumberPicker numberPickerMonth = view.findViewById(R.id.numberPickerMonth); numberPickerMonth.setMinValue(1); numberPickerMonth.setMaxValue(12); - numberPickerMonth.setValue(citrus.getCurMonth()); // 月の初期値を設定 + numberPickerMonth.setValue(viwMonth); // 月の初期値を設定 NumberPicker numberPickerDay = view.findViewById(R.id.numberPickerDay); numberPickerDay.setMinValue(1); numberPickerDay.setMaxValue(getDaysInMonth(citrus.getCurYear(), citrus.getCurMonth())); // 日の初期値を設定 - numberPickerDay.setValue(citrus.getCurDay()); // 日の初期値を設定 + numberPickerDay.setValue(viwDay); // 日の初期値を設定 // 月が変更されたとき numberPickerMonth.setOnValueChangedListener((picker, oldVal, newVal) -> { @@ -159,12 +186,31 @@ adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item); spinner.setAdapter(adapter); + //キャンセルボタン処理 Button cancelButton = view.findViewById(R.id.todo_cancel_button); cancelButton.setOnClickListener(v->{ - ((MainActivity)getActivity()).showFragment(new HomeFragment()); + ((MainActivity)getActivity()).backFragment(); }); + //削除ボタン処理 + Button deleteButton = view.findViewById(R.id.todo_delete_button); + if (isEdit) { + deleteButton.setVisibility(View.VISIBLE); // 編集時ボタンを表示 + } else { + deleteButton.setVisibility(View.INVISIBLE); // 追加時ボタンを非表示 + } + deleteButton.setOnClickListener(v->{ + //ここにdelete処理か確認ダイアログ(いるかな) + }); + + //追加ボタン処理 Button createButton = view.findViewById(R.id.todo_create_button); + if (isEdit) { + createButton.setText("完了"); //編集時 + } else { + createButton.setText("追加"); //追加時 + } + createButton.setOnClickListener(v->{ int year = numberPickerYear.getValue(); // 設定年を取得 int month = numberPickerMonth.getValue(); // 設定月を取得 @@ -184,7 +230,7 @@ return; } todosViewModel.createTodo(accountId,spinner.getSelectedItemPosition(),year,month,day,title,token); - ((MainActivity)getActivity()).showFragment(new HomeFragment()); + ((MainActivity)getActivity()).backFragment(); }); } 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..73ddb7d 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,26 +136,21 @@ 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); todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); - MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, citrus); + MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, citrus, getActivity()); todoRecyclerView.setAdapter(todoAdapter); RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_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(year, month, day)); + }); + + 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 { @@ -226,8 +280,11 @@ private Citrus citrus; private String accountId; private String token; + private Context context; - MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Citrus citrus) { + + + MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Citrus citrus, Context context) { this.citrus = citrus; this.token = citrus.getToken(); this.accountId = citrus.getAccountId(); @@ -238,6 +295,7 @@ } else { this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 } + this.context = context; } public void setTodos(List todos ,HashMap idBookHashMap) { @@ -271,6 +329,11 @@ holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); } + holder.todoButton.setOnClickListener(v -> { + Activity activity = (Activity) context; + ((MainActivity) activity).showFragment(new CreateTodoFragment(todoData)); + }); + holder.todoCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> { // チェック状態が変更された時の処理を記述 if (isChecked) { @@ -289,6 +352,10 @@ return todoList.size(); } + public void sortTodosById(Todo todo) { + notifyDataSetChanged(); + } + static class MyTodoViewHolder extends RecyclerView.ViewHolder { Button todoButton; CheckBox todoCheckBox; diff --git a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java index 47eb235..4b492b3 100644 --- a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java +++ b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java @@ -79,17 +79,19 @@ System.out.println("テスト入力完了"); //入力した文字に空白が含まれている場合 - if(id.contains(" ") || pw.contains(" ")){ + //半角->全角 全てのスペースをなくす + if(id.contains(" ") ||id.contains(" ") || pw.contains(" ") || pw.contains(" ")){ System.out.println("不適切入力"); ((TextView)findViewById(R.id.textView_respons)).setText("空白を含まないように入力してください"); } //入力欄が空欄の時 + //半角->全角 if(id.isEmpty() || pw.isEmpty() || id.trim().isEmpty() || pw.trim().isEmpty() ){ System.out.println("不適切入力"); ((TextView)findViewById(R.id.textView_respons)).setText("適切に入力してください"); - }else if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() && !id.contains(" ") && !pw.contains(" ") ){ + }else if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() && !id.contains(" ") && !id.contains(" ") && !pw.contains(" ") && !pw.contains(" ")){ //入力欄が全て適切に入力された場合の処理 Call call = accountsRest.signup(id,pw); call.enqueue(new Callback() { diff --git a/app/src/main/res/layout/fragment_create_todo.xml b/app/src/main/res/layout/fragment_create_todo.xml index 605204b..d994fc7 100644 --- a/app/src/main/res/layout/fragment_create_todo.xml +++ b/app/src/main/res/layout/fragment_create_todo.xml @@ -161,7 +161,8 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.424" /> + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.396" /> + +