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 47a0b82..3dba52e 100644 --- a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java @@ -11,7 +11,6 @@ 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; @@ -19,7 +18,6 @@ import android.widget.Button; import android.widget.LinearLayout; import android.widget.RadioGroup; -import android.widget.RatingBar; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; @@ -190,7 +188,7 @@ scheduleViewModel.getSchedulesByMonth().observe(getViewLifecycleOwner(), new Observer>>() { @Override public void onChanged(HashMap> integerHashMapHashMap) { - TextView curMonth = view.findViewById(R.id.month); + TextView curMonth = view.findViewById(R.id.Month); schedules = integerHashMapHashMap; updateCalendar(curMonth); } @@ -201,7 +199,7 @@ public void onChanged(HashMap integerBookHashMap) { books = integerBookHashMap; if(books != null) { - TextView curMonth = view.findViewById(R.id.month); + TextView curMonth = view.findViewById(R.id.Month); if(todos != null) { todos.clear(); } else { @@ -220,13 +218,15 @@ todosViewModel.getTodosByMonthLiveData().observe(getViewLifecycleOwner(), new Observer>>() { @Override public void onChanged(HashMap> integerHashMapHashMap) { - TextView curMonth = view.findViewById(R.id.month); - for (int day:integerHashMapHashMap.keySet()){ - if(todos.get(day) == null){ - todos.put(day, new HashMap<>()); - } - for(int todoId:integerHashMapHashMap.get(day).keySet()){ - todos.get(day).put(todoId, integerHashMapHashMap.get(day).get(todoId)); + TextView curMonth = view.findViewById(R.id.Month); + if(integerHashMapHashMap != null && !integerHashMapHashMap.isEmpty()) { + for (int day : integerHashMapHashMap.keySet()) { + if (todos.get(day) == null) { + todos.put(day, new HashMap<>()); + } + for (int todoId : integerHashMapHashMap.get(day).keySet()) { + todos.get(day).put(todoId, integerHashMapHashMap.get(day).get(todoId)); + } } } updateCalendar(curMonth); @@ -235,7 +235,6 @@ booksViewModel.loadBooks(accountId, token); - tableLayout = view.findViewById(R.id.calendarlayout); Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); //現在の年 @@ -258,10 +257,10 @@ DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); int firstDayOfWeek = dayOfWeek.getValue(); - Button nextMonth = view.findViewById(R.id.nextMonth); - Button prevMonth = view.findViewById(R.id.prevMonth); + Button nextMonth = view.findViewById(R.id.NextMonth); + Button prevMonth = view.findViewById(R.id.PrevMonth); FloatingActionButton createBook = view.findViewById(R.id.floatingActionButton); - TextView curMonth = view.findViewById(R.id.month); + TextView curMonth = view.findViewById(R.id.Month); curMonth.setText(year + "年" + month + "月"); createBook.setOnClickListener(new View.OnClickListener() { @@ -374,7 +373,7 @@ String[][] days = calendarString(firstDayOfWeek, prevMonthDay, lastDay); int one = 0; - RadioGroup selectButton = getView().findViewById(R.id.selectButton); + RadioGroup selectButton = getView().findViewById(R.id.SelectButton); for(int i = 0; i < 6; i++) { TableRow tableRow = (TableRow) tableLayout.getChildAt(i); diff --git a/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java b/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java new file mode 100644 index 0000000..e03be33 --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java @@ -0,0 +1,486 @@ +package com.example.citrusclient.views; + +import android.graphics.Color; +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; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewTreeObserver; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.RadioGroup; +import android.widget.TableLayout; +import android.widget.TableRow; +import android.widget.TextView; + +import com.example.citrusclient.Citrus; +import com.example.citrusclient.R; +import com.example.citrusclient.models.Book; +import com.example.citrusclient.models.Schedule; +import com.example.citrusclient.models.Task; +import com.example.citrusclient.models.Todo; +import com.example.citrusclient.viewmodels.BooksViewModel; +import com.example.citrusclient.viewmodels.ScheduleViewModel; +import com.example.citrusclient.viewmodels.TodosViewModel; +import com.google.android.material.floatingactionbutton.FloatingActionButton; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.YearMonth; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link CalendarFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class OtherCalendarFragment 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; + + + private TableLayout tableLayout; + private TableRow[] tableRows = new TableRow[6]; + + public OtherCalendarFragment() { + // 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 ScheduleFragment. + */ + // TODO: Rename and change types and number of parameters + public static CalendarFragment newInstance(String param1, String param2) { + CalendarFragment fragment = new CalendarFragment(); + 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); + } + + scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); + booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); + todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); + + schedules = new HashMap<>(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_calendar, container, false); + } + + + BooksViewModel booksViewModel; + private List scheduleList; + ScheduleViewModel scheduleViewModel; + TodosViewModel todosViewModel; + private HashMap integerBookHashMap; + + int year; + int month; + int day; + + HashMap books; + + HashMap> schedules; + HashMap> todos; + + public TextView[][] calendar(int firstDayOfWeek, int prevMonthDay, int lastDay) { + int curDay = 1; + int d = 1; + int prevDay = prevMonthDay - firstDayOfWeek; + TextView textViews[][] = new TextView[6][7]; + if(firstDayOfWeek == 7) firstDayOfWeek = 0; + for(int i = 0; i < 6; i++) { + for(int j = 0; j < 7; j++){ + if(firstDayOfWeek > 0) { + prevDay++; + textViews[i][j] = new TextView(requireContext()); + textViews[i][j].setTextColor(Color.GRAY); + textViews[i][j].setText("" + prevDay); + firstDayOfWeek--; + } else if(firstDayOfWeek <= 0 && curDay <= lastDay) { + textViews[i][j] = new TextView(requireContext()); + textViews[i][j].setText("" + curDay); + curDay++; + } else if (curDay > lastDay && d <= 43-curDay) { + textViews[i][j] = new TextView(requireContext()); + textViews[i][j].setTextColor(Color.GRAY); + textViews[i][j].setText("" + d); + d++; + } + } + } + return textViews; + } + + public String[][] calendarString(int firstDayOfWeek, int prevMonthDay, int lastDay) { + int curDay = 1; + int d = 1; + int prevDay = prevMonthDay - firstDayOfWeek; + String[][] textViews = new String[6][7]; + if(firstDayOfWeek == 7) firstDayOfWeek = 0; + for(int i = 0; i < 6; i++) { + for(int j = 0; j < 7; j++){ + if(firstDayOfWeek > 0) { + prevDay++; + textViews[i][j] = ("" + prevDay); + firstDayOfWeek--; + } else if(firstDayOfWeek <= 0 && curDay <= lastDay) { + textViews[i][j] = ("" + curDay); + curDay++; + } else if (curDay > lastDay && d <= 43-curDay) { + textViews[i][j] = ("" + d); + d++; + } + } + } + return textViews; + } + + @Override + 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(); + + todos = new HashMap<>(); + schedules = new HashMap<>(); + + scheduleViewModel.getSchedulesByMonth().observe(getViewLifecycleOwner(), new Observer>>() { + @Override + public void onChanged(HashMap> integerHashMapHashMap) { + TextView curMonth = view.findViewById(R.id.otherMonth); + schedules = integerHashMapHashMap; + updateCalendar(curMonth); + } + }); + + booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(HashMap integerBookHashMap) { + books = integerBookHashMap; + if(books != null) { + TextView curMonth = view.findViewById(R.id.otherMonth); + if(todos != null) { + todos.clear(); + } else { + todos = new HashMap<>(); + } + + for(int bookId:books.keySet()){ + todosViewModel.loadTodosByMonth(accountId, bookId, year, month, token); + } + + updateCalendar(curMonth); + } + } + }); + + todosViewModel.getTodosByMonthLiveData().observe(getViewLifecycleOwner(), new Observer>>() { + @Override + public void onChanged(HashMap> integerHashMapHashMap) { + TextView curMonth = view.findViewById(R.id.otherMonth); + for (int day:integerHashMapHashMap.keySet()){ + if(todos.get(day) == null){ + todos.put(day, new HashMap<>()); + } + for(int todoId:integerHashMapHashMap.get(day).keySet()){ + todos.get(day).put(todoId, integerHashMapHashMap.get(day).get(todoId)); + } + } + updateCalendar(curMonth); + } + }); + + booksViewModel.loadBooks(accountId, token); + + + 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(); + int lastDay = CurLastDay.getDayOfMonth(); + + //前の月の最後の日付を取得 + LocalDate lastDate = LocalDate.of(year, month, 1); + LocalDate lastDayPrevMonth = lastDate.minusDays(1); + int prevMonthDay = lastDayPrevMonth.getDayOfMonth(); + + //指定した年月の一日の曜日を取得 + LocalDate firstDay = LocalDate.of(year, month, 1); + DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); + int firstDayOfWeek = dayOfWeek.getValue(); + + Button nextMonth = view.findViewById(R.id.otherNextMonth); + Button prevMonth = view.findViewById(R.id.otherPrevMonth); + FloatingActionButton createBook = view.findViewById(R.id.floatingActionButton); + TextView curMonth = view.findViewById(R.id.otherMonth); + curMonth.setText(year + "年" + month + "月"); + + createBook.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment()); + } + }); + + //カレンダーの次の月を表示 + nextMonth.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + month++; + if(month > 12){ + month = 1; + year++; + } + scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); + } + }); + + //カレンダーの前の月を表示 + prevMonth.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + month--; + if(month < 1) { + month = 12; + year--; + } + scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); + } + }); + + view.findViewById(R.id.otherRbTodo).setOnClickListener(view1 -> { + updateCalendar(curMonth); + }); + view.findViewById(R.id.otherRbSchedule).setOnClickListener(view1 -> { + updateCalendar(curMonth); + }); + view.findViewById(R.id.otherRbBoth).setOnClickListener(view1 -> { + updateCalendar(curMonth); + }); + + //カレンダーの初期表示 + 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++) { + LinearLayout layout = new LinearLayout(requireContext()); + layout.setOrientation(LinearLayout.VERTICAL); + layout.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT)); + TableRow.LayoutParams p2 = (TableRow.LayoutParams) layout.getLayoutParams(); + p2.weight = 1; + tableRow.addView(layout); + layout.addView(days[i][j]); + final int ii = i; + final int jj = j; + layout.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + citrus.setCurMonth(month); + citrus.setCurYear(year); + citrus.setCurDay(Integer.parseInt(daysString[ii][jj])); + ((MainActivity) getActivity()).showFragment(new HomeFragment()); + } + }); + RecyclerView recyclerView = new RecyclerView(requireContext()); + RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(requireContext()); + recyclerView.setLayoutManager(layoutmanager); + + List schedules = new ArrayList<>(); + + recyclerView.setAdapter(new OtherScheduleAdapter(schedules, books)); + layout.addView(recyclerView); + layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + layout.getHeight() + ); + layout.getChildAt(1).setLayoutParams(layoutParams); + layout.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + }); + + } + } + } + + //カレンダーの更新 + 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); + int one = 0; + RadioGroup selectButton = getView().findViewById(R.id.otherSelectButton); + + 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(days[i][j].equals("1")){ + one++; + } + if(this.schedules != null && one == 1) { + + int select = selectButton.getCheckedRadioButtonId(); + 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()) { + schedules.add(schedule); + } + } + } else if(select == R.id.otherRbSchedule){ + 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); + } + } + } else { + if (this.todos.get(Integer.parseInt(days[i][j])) != null) { + for (Todo schedule : this.todos.get(Integer.parseInt(days[i][j])).values()) { + schedules.add(schedule); + } + } + 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); + } + } + } + + + } + } + + ((OtherScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, books); + ((TextView) layout.getChildAt(0)).setText(days[i][j]); + } + } + } +} + +class OtherScheduleAdapter extends RecyclerView.Adapter { + + private List scheduleList; + private HashMap integerBookHashMap; + + OtherScheduleAdapter(List schedules, HashMap integerBookHashMap) { + this.scheduleList = schedules; + if(integerBookHashMap != null) { + this.integerBookHashMap = new HashMap<>(integerBookHashMap); + } else { + this.integerBookHashMap = new HashMap<>(); + } + } + + public void setSchedules(List schedules, HashMap integerBookHashMap) { + scheduleList = schedules; + if(integerBookHashMap != null) { + this.integerBookHashMap = new HashMap<>(integerBookHashMap); + } else { + this.integerBookHashMap = new HashMap<>(); + } + notifyDataSetChanged(); + } + @NonNull + @Override + public OtherScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_calendar_schedule, parent, false); + return new OtherScheduleViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull OtherScheduleViewHolder holder, int position) { + int red = 169; + int green = 169; + 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); + blue = Integer.parseInt(book.getColor().substring(5, 7), 16); + } + holder.scheduleText.setBackgroundColor(Color.rgb(red, green, blue)); + holder.scheduleText.setTextColor(Color.rgb(255-red, 255-green, 255-blue)); + } + + @Override + public int getItemCount() { + return scheduleList.size(); + } + + static class OtherScheduleViewHolder extends RecyclerView.ViewHolder { + TextView scheduleText; + public OtherScheduleViewHolder(@NonNull View itemView) { + super(itemView); + scheduleText = itemView.findViewById(R.id.other_schedule_text); + } + } +} + diff --git a/app/src/main/res/layout/fragment_calendar.xml b/app/src/main/res/layout/fragment_calendar.xml index ac66c68..37e121d 100644 --- a/app/src/main/res/layout/fragment_calendar.xml +++ b/app/src/main/res/layout/fragment_calendar.xml @@ -9,7 +9,7 @@