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 10b2987..aa273f6 100644 --- a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java @@ -7,12 +7,16 @@ 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.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.ViewTreeObserver; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; @@ -20,8 +24,11 @@ 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.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.ScheduleViewModel; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -31,6 +38,7 @@ import java.time.YearMonth; import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; import java.util.List; import android.view.ViewGroup.MarginLayoutParams; @@ -86,7 +94,8 @@ mParam2 = getArguments().getString(ARG_PARAM2); } - + scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); + booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); } @Override @@ -96,11 +105,16 @@ return inflater.inflate(R.layout.fragment_calendar, container, false); } + + BooksViewModel booksViewModel; private List scheduleList; ScheduleViewModel scheduleViewModel; + private HashMap integerBookHashMap; + int year; int month; + int day; public TextView[][] calendar(int firstDayOfWeek, int prevMonthDay, int lastDay) { int curDay = 1; @@ -131,10 +145,35 @@ 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); + tableLayout = view.findViewById(R.id.calendarlayout); Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); //現在の年 @@ -159,7 +198,7 @@ Button prevMonth = view.findViewById(R.id.prevMonth); FloatingActionButton createBook = view.findViewById(R.id.floatingActionButton); TextView curMonth = view.findViewById(R.id.month); - curMonth.setText(year + "年" + "" + month + "月"); + curMonth.setText(year + "年" + month + "月"); createBook.setOnClickListener(new View.OnClickListener() { @Override @@ -176,7 +215,7 @@ month = 1; year++; } - curMonth.setText(year + "年" + "" + month + "月"); + curMonth.setText(year + "年" + month + "月"); //指定した年月の一日の曜日を取得 LocalDate firstDay = LocalDate.of(year, month, 1); DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); @@ -190,38 +229,21 @@ LocalDate CurLastDay = yearMonth.atEndOfMonth(); int lastDay = CurLastDay.getDayOfMonth(); - TextView[][] days = calendar(firstDayOfWeek, prevMonthDay, lastDay); - tableLayout.removeAllViews(); - for (int i = 0; i < 6; i++) { - tableRows[i] = new TableRow(requireContext()); - tableLayout.addView(tableRows[i]); - } + 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 = new LinearLayout(requireContext()); - layout.setOrientation(LinearLayout.VERTICAL); - RecyclerView recyclerView = new RecyclerView(requireContext()); - recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); - recyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); + 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)); - recyclerView.setAdapter(new MyScheduleAdapter(schedules)); - LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) recyclerView.getLayoutParams(); - p.weight = 1; - p.height = -1; - tableRows[i].setLayoutParams(p); - - layout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT)); - TableRow.LayoutParams p2 = (TableRow.LayoutParams) layout.getLayoutParams(); - p2.weight = 1; - layout.setLayoutParams(p2); - - - layout.addView(days[i][j]); - layout.addView(recyclerView); - tableRows[i].addView(layout); + 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 + ""); } } } @@ -235,7 +257,7 @@ month = 12; year--; } - curMonth.setText(year + "年" + "" + month + "月"); + curMonth.setText(year + "年" + month + "月"); //指定した年月の一日の曜日を取得 LocalDate firstDay = LocalDate.of(year, month, 1); DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); @@ -249,69 +271,57 @@ LocalDate CurLastDay = yearMonth.atEndOfMonth(); int lastDay = CurLastDay.getDayOfMonth(); - TextView[][] days = calendar(firstDayOfWeek, prevMonthDay, lastDay); - tableLayout.removeAllViews(); - for (int i = 0; i < 6; i++) { - tableRows[i] = new TableRow(requireContext()); - tableLayout.addView(tableRows[i]); - } +// 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 = new LinearLayout(requireContext()); - layout.setOrientation(LinearLayout.VERTICAL); - RecyclerView recyclerView = new RecyclerView(requireContext()); - recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); - recyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); + 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)); - recyclerView.setAdapter(new MyScheduleAdapter(schedules)); - LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) recyclerView.getLayoutParams(); - p.weight = 1; - p.height = -1; - tableRows[i].setLayoutParams(p); - - layout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT)); - TableRow.LayoutParams p2 = (TableRow.LayoutParams) layout.getLayoutParams(); - p2.weight = 1; - layout.setLayoutParams(p2); - - - layout.addView(days[i][j]); - layout.addView(recyclerView); - tableRows[i].addView(layout); + ((MyScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, integerBookHashMap); + ((TextView) layout.getChildAt(0)).setText(days[i][j]); } } } }); + TableLayout tableLayout = view.findViewById(R.id.calendarlayout); TextView[][] days = calendar(firstDayOfWeek, prevMonthDay, lastDay); - for(int i = 0; i < 6; i++) { - tableRows[i] = (TableRow) tableLayout.getChildAt(i); - + TableRow tableRow = (TableRow) tableLayout.getChildAt(i); for(int j = 0; j < 7; j++) { LinearLayout layout = new LinearLayout(requireContext()); layout.setOrientation(LinearLayout.VERTICAL); - - - RecyclerView recyclerView = new RecyclerView(requireContext()); - recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); - recyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); - 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)); - LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) recyclerView.getLayoutParams(); - p.weight = 1; - layout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT)); TableRow.LayoutParams p2 = (TableRow.LayoutParams) layout.getLayoutParams(); p2.weight = 1; + tableRow.addView(layout); layout.addView(days[i][j]); + RecyclerView recyclerView = new RecyclerView(requireContext()); + RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(requireContext()); + 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); - tableRows[i].addView(layout); + 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); + } + }); + } } } @@ -320,11 +330,24 @@ class MyScheduleAdapter extends RecyclerView.Adapter { private List scheduleList; + private HashMap integerBookHashMap; - MyScheduleAdapter(List schedules) { this.scheduleList = schedules; } + MyScheduleAdapter(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) { + public void setSchedules(List schedules, HashMap integerBookHashMap) { scheduleList = schedules; + if(integerBookHashMap != null) { + this.integerBookHashMap = new HashMap<>(integerBookHashMap); + } else { + this.integerBookHashMap = new HashMap<>(); + } notifyDataSetChanged(); } @NonNull @@ -336,8 +359,19 @@ @Override public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { + int red = 169; + int green = 169; + int blue = 169; Schedule 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