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 aa273f6..1de74fd 100644 --- a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java @@ -1,6 +1,5 @@ package com.example.citrusclient.views; -import android.content.Intent; import android.graphics.Color; import android.os.Bundle; @@ -18,7 +17,6 @@ import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.Button; -import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TableLayout; import android.widget.TableRow; @@ -32,7 +30,6 @@ import com.example.citrusclient.viewmodels.ScheduleViewModel; import com.google.android.material.floatingactionbutton.FloatingActionButton; -import java.io.ObjectInputStream; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.YearMonth; @@ -41,9 +38,6 @@ import java.util.HashMap; import java.util.List; -import android.view.ViewGroup.MarginLayoutParams; -import android.view.ViewGroup.LayoutParams; - /** * A simple {@link Fragment} subclass. * Use the {@link CalendarFragment#newInstance} factory method to @@ -96,6 +90,8 @@ scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); + + schedules = new HashMap<>(); } @Override @@ -109,13 +105,14 @@ BooksViewModel booksViewModel; private List scheduleList; ScheduleViewModel scheduleViewModel; - private HashMap integerBookHashMap; int year; int month; int day; + HashMap> schedules; + public TextView[][] calendar(int firstDayOfWeek, int prevMonthDay, int lastDay) { int curDay = 1; int d = 1; @@ -173,12 +170,29 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + //AdapterはそれぞれのRecyclerViewに入れる必要があるため、for文を用いて、それぞれにセットする。(後期の作業) + + Citrus citrus = (Citrus)(getActivity().getApplication()); + String token = citrus.getToken(); + String accountId = citrus.getAccountId(); + + scheduleViewModel.getSchedulesByMonth().observe(getViewLifecycleOwner(), new Observer>>() { + @Override + public void onChanged(HashMap> integerHashMapHashMap) { + TextView curMonth = view.findViewById(R.id.month); + schedules = integerHashMapHashMap; + updateCalendar(curMonth); + } + }); + tableLayout = view.findViewById(R.id.calendarlayout); Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); //現在の年 month = calendar.get(Calendar.MONTH) + 1; //現在の月 + scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); + //現在の月の最後の日付を取得 YearMonth yearMonth = YearMonth.of(year, month); LocalDate CurLastDay = yearMonth.atEndOfMonth(); @@ -207,6 +221,7 @@ } }); + //カレンダーの次の月を表示 nextMonth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -215,40 +230,12 @@ month = 1; year++; } - curMonth.setText(year + "年" + month + "月"); - //指定した年月の一日の曜日を取得 - LocalDate firstDay = LocalDate.of(year, month, 1); - DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); - int firstDayOfWeek = dayOfWeek.getValue(); - //前の月の最後の日付を取得 - LocalDate lastDate = LocalDate.of(year, month, 1); - LocalDate lastDayPrevMonth = lastDate.minusDays(1); - int prevMonthDay = lastDayPrevMonth.getDayOfMonth(); - //現在の月の最後の日付を取得 - YearMonth yearMonth = YearMonth.of(year, month); - LocalDate CurLastDay = yearMonth.atEndOfMonth(); - int lastDay = CurLastDay.getDayOfMonth(); + scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); - String[][] days = calendarString(firstDayOfWeek, prevMonthDay, lastDay); - for(int i = 0; i < 6; i++) { - TableRow tableRow = (TableRow) tableLayout.getChildAt(i); - for(int j = 0; j < 7; j++) { - LinearLayout layout = (LinearLayout) tableRow.getChildAt(j); - RecyclerView recyclerView = (RecyclerView) layout.getChildAt(1); - List schedules = new ArrayList<>(); - schedules.add(new Schedule("abc", "3232", "yyyy", 0, 1)); - schedules.add(new Schedule("123", "3232", "yyyy", 0, 2)); - if(i==2&&j==1)schedules.add(new Schedule("cat", "3232", "yyyy", 0, 5)); - if(i==2&&j==1)schedules.add(new Schedule("cat", "3232", "yyyy", 0, 5)); - if(i==2&&j==1)schedules.add(new Schedule("cat", "3232", "yyyy", 0, 5)); - ((MyScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, integerBookHashMap); - ((TextView) layout.getChildAt(0)).setText(days[i][j]); - if(i == 2 && j == 1) Log.i("kjda", layout.getLayoutParams().height + ""); - } - } } }); + //カレンダーの前の月を表示 prevMonth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -257,40 +244,14 @@ month = 12; year--; } - curMonth.setText(year + "年" + month + "月"); - //指定した年月の一日の曜日を取得 - LocalDate firstDay = LocalDate.of(year, month, 1); - DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); - int firstDayOfWeek = dayOfWeek.getValue(); - //前の月の最後の日付を取得 - LocalDate lastDate = LocalDate.of(year, month, 1); - LocalDate lastDayPrevMonth = lastDate.minusDays(1); - int prevMonthDay = lastDayPrevMonth.getDayOfMonth(); - //現在の月の最後の日付を取得 - YearMonth yearMonth = YearMonth.of(year, month); - LocalDate CurLastDay = yearMonth.atEndOfMonth(); - int lastDay = CurLastDay.getDayOfMonth(); - -// TextView[][] days = calendar(firstDayOfWeek, prevMonthDay, lastDay); - String[][] days = calendarString(firstDayOfWeek, prevMonthDay, lastDay); - for(int i = 0; i < 6; i++) { - TableRow tableRow = (TableRow) tableLayout.getChildAt(i); - for(int j = 0; j < 7; j++) { - LinearLayout layout = (LinearLayout) tableRow.getChildAt(j); - RecyclerView recyclerView = (RecyclerView) layout.getChildAt(1); - List schedules = new ArrayList<>(); - schedules.add(new Schedule("abc", "3232", "yyyy", 0, 1)); - schedules.add(new Schedule("123", "3232", "yyyy", 0, 2)); - if(i==2&&j==1)schedules.add(new Schedule("cat", "3232", "yyyy", 0, 5)); - ((MyScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, integerBookHashMap); - ((TextView) layout.getChildAt(0)).setText(days[i][j]); - } - } + scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); } }); + //カレンダーの初期表示 TableLayout tableLayout = view.findViewById(R.id.calendarlayout); TextView[][] days = calendar(firstDayOfWeek, prevMonthDay, lastDay); + String[][] daysString = calendarString(firstDayOfWeek, prevMonthDay, lastDay); for(int i = 0; i < 6; i++) { TableRow tableRow = (TableRow) tableLayout.getChildAt(i); for(int j = 0; j < 7; j++) { @@ -306,8 +267,7 @@ recyclerView.setLayoutManager(layoutmanager); List schedules = new ArrayList<>(); - schedules.add(new Schedule("abc", "3232", "yyyy", 0, 1)); - schedules.add(new Schedule("123", "3232", "yyyy", 0, 2)); + recyclerView.setAdapter(new MyScheduleAdapter(schedules, integerBookHashMap)); layout.addView(recyclerView); layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @@ -325,6 +285,45 @@ } } } + + //カレンダーの更新 + private void updateCalendar(TextView curMonth){ + curMonth.setText(year + "年" + month + "月"); + //指定した年月の一日の曜日を取得 + LocalDate firstDay = LocalDate.of(year, month, 1); + DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); + int firstDayOfWeek = dayOfWeek.getValue(); + //前の月の最後の日付を取得 + LocalDate lastDate = LocalDate.of(year, month, 1); + LocalDate lastDayPrevMonth = lastDate.minusDays(1); + int prevMonthDay = lastDayPrevMonth.getDayOfMonth(); + //現在の月の最後の日付を取得 + YearMonth yearMonth = YearMonth.of(year, month); + LocalDate CurLastDay = yearMonth.atEndOfMonth(); + int lastDay = CurLastDay.getDayOfMonth(); + + String[][] days = calendarString(firstDayOfWeek, prevMonthDay, lastDay); + + for(int i = 0; i < 6; i++) { + TableRow tableRow = (TableRow) tableLayout.getChildAt(i); + for(int j = 0; j < 7; j++) { + LinearLayout layout = (LinearLayout) tableRow.getChildAt(j); + RecyclerView recyclerView = (RecyclerView) layout.getChildAt(1); + List schedules = new ArrayList<>(); + if(this.schedules != null) { + if (this.schedules.get(Integer.parseInt(days[i][j])) != null) { + for (Schedule schedule : this.schedules.get(Integer.parseInt(days[i][j])).values()) { + schedules.add(schedule); + } + } + } + + ((MyScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, integerBookHashMap); + ((TextView) layout.getChildAt(0)).setText(days[i][j]); + //if(i == 2 && j == 1) Log.i("kjda", layout.getLayoutParams().height + ""); + } + } + } } class MyScheduleAdapter extends RecyclerView.Adapter {