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 cc44f19..454fa78 100644 --- a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java @@ -1,5 +1,7 @@ package com.example.citrusclient.views; +import android.content.Intent; +import android.graphics.Color; import android.os.Bundle; import androidx.annotation.NonNull; @@ -12,6 +14,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TableLayout; import android.widget.TableRow; @@ -20,12 +23,19 @@ import com.example.citrusclient.R; import com.example.citrusclient.models.Schedule; 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; import java.util.ArrayList; import java.util.Calendar; 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 @@ -89,23 +99,75 @@ private List scheduleList; ScheduleViewModel scheduleViewModel; + int year; int month; + 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; + } + @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); tableLayout = view.findViewById(R.id.calendarlayout); Calendar calendar = Calendar.getInstance(); - month = calendar.get(Calendar.MONTH) + 1; - int date = calendar.get(Calendar.DATE); - int lastdate = calendar.getActualMaximum(Calendar.DATE); - int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + year = calendar.get(Calendar.YEAR); //現在の年 + month = calendar.get(Calendar.MONTH) + 1; //現在の月 + + //現在の月の最後の日付を取得 + 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.nextMonth); Button prevMonth = view.findViewById(R.id.prevMonth); + FloatingActionButton createBook = view.findViewById(R.id.floatingActionButton); TextView curMonth = view.findViewById(R.id.month); curMonth.setText("" + 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) { @@ -114,6 +176,53 @@ month = 1; } curMonth.setText("" + 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); + tableLayout.removeAllViews(); + for (int i = 0; i < 6; i++) { + tableRows[i] = new TableRow(requireContext()); + tableLayout.addView(tableRows[i]); + } + for(int i = 0; i < 6; 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)); + if(i==2&&j==1)schedules.add(new Schedule("aho", "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); + } + } } }); @@ -125,18 +234,65 @@ month = 12; } curMonth.setText("" + 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); + tableLayout.removeAllViews(); + for (int i = 0; i < 6; i++) { + tableRows[i] = new TableRow(requireContext()); + tableLayout.addView(tableRows[i]); + } + for(int i = 0; i < 6; 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)); + if(i==2&&j==1)schedules.add(new Schedule("aho", "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); + } + } } }); + TextView[][] days = calendar(firstDayOfWeek, prevMonthDay, lastDay); for(int i = 0; i < 6; i++) { tableRows[i] = (TableRow) tableLayout.getChildAt(i); + for(int j = 0; j < 7; j++) { LinearLayout layout = new LinearLayout(requireContext()); layout.setOrientation(LinearLayout.VERTICAL); - TextView textView = new TextView(requireContext()); - textView.setText("" + i); RecyclerView recyclerView = new RecyclerView(requireContext()); recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); @@ -151,7 +307,7 @@ layout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT)); TableRow.LayoutParams p2 = (TableRow.LayoutParams) layout.getLayoutParams(); p2.weight = 1; - layout.addView(textView); + layout.addView(days[i][j]); layout.addView(recyclerView); tableRows[i].addView(layout); } diff --git a/app/src/main/java/com/example/citrusclient/views/MainActivity.java b/app/src/main/java/com/example/citrusclient/views/MainActivity.java index 0e92ec5..371c2d0 100644 --- a/app/src/main/java/com/example/citrusclient/views/MainActivity.java +++ b/app/src/main/java/com/example/citrusclient/views/MainActivity.java @@ -76,7 +76,7 @@ if(itemId == R.id.book){// 本棚 showFragment(new MyBookshelfFragment()); } else if(itemId == R.id.search){//検索 - + showFragment(new SearchFragment()); } else if(itemId == R.id.home){//ホーム showFragment(new HomeFragment()); }else if(itemId == R.id.calendar){ //カレンダ diff --git a/app/src/main/java/com/example/citrusclient/views/SearchFragment.java b/app/src/main/java/com/example/citrusclient/views/SearchFragment.java new file mode 100644 index 0000000..8c49eec --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/views/SearchFragment.java @@ -0,0 +1,211 @@ +package com.example.citrusclient.views; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Color; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +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.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.example.citrusclient.Citrus; +import com.example.citrusclient.R; +import com.example.citrusclient.models.Book; +import com.example.citrusclient.viewmodels.PublicBooksViewModel; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link SearchFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class SearchFragment 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; + + public SearchFragment() { + // 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 SearchFragment. + */ + // TODO: Rename and change types and number of parameters + public static SearchFragment newInstance(String param1, String param2) { + SearchFragment fragment = new SearchFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + private ArrayList publicList; + private ArrayList name; + PublicBooksViewModel publicBooksViewModel; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + + publicBooksViewModel = new ViewModelProvider(this).get(PublicBooksViewModel.class); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_search, container, false); + } + + public void onViewCreated(@NonNull View view, @NonNull Bundle saveInstanceState){ + super.onViewCreated(view, saveInstanceState); +// LinearLayout layout = new LinearLayout(requireContext()); + +// Citrus citrus = (Citrus)(getActivity().getApplication()); + publicList = new ArrayList<>(); + name = new ArrayList<>(); + + RecyclerView recyclerView = view.findViewById(R.id.public_list); + recyclerView.setHasFixedSize(true); + recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); + recyclerView.setLayoutManager(layoutManager); + PublicAdapter publicAdapter = new PublicAdapter(name, publicList, getActivity()); + recyclerView.setAdapter(publicAdapter); + publicBooksViewModel.getAllBooksLiveData().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(ArrayList books) { + if(books != null){ + for(int i = 0; i < books.size(); i++){ + name.add(books.get(i).getAccountId()); + } + publicList = new ArrayList<>(books); + }else{ + publicList = new ArrayList<>(); + } + publicAdapter.setBooks(publicList); + System.out.println(name); + } + }); + publicBooksViewModel.loadAllBooks(); + } +} + +class PublicAdapter extends RecyclerView.Adapter{ + + private List publicList; + private List accountIdList; + private Context context; + + PublicAdapter(List id, List publics, Context context){ + this.accountIdList = id; + this.publicList = publics; + this.context = context; + } + + public void setBooks(List publics){ + publicList = publics; + notifyDataSetChanged(); + } + + @NonNull + @Override + public PublicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_public_book, parent, false); + return new PublicViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull PublicAdapter.PublicViewHolder holder, int position) { + holder.idText.setText(accountIdList.get(position).toString()); + holder.idText.setHeight(30); + holder.idText.setWidth(100); + Book publicData = this.publicList.get(position); + holder.publicButton.setText(publicData.getTitle()); + int red; + int green; + int blue; + if (publicData.getColor() == null) { + red = 255; + green = 255; + blue = 255; + } else if (publicData.getColor().length() < 7) { + red = 255; + green = 255; + blue = 255; + } else { + red = Integer.parseInt(publicData.getColor().substring(1, 3), 16); + green = Integer.parseInt(publicData.getColor().substring(3, 5), 16); + blue = Integer.parseInt(publicData.getColor().substring(5, 7), 16); + } + + holder.publicButton.setBackgroundColor(Color.rgb(red, green, blue)); + holder.publicButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); + holder.publicButton.setOnClickListener(v -> { + Calendar c = Calendar.getInstance(); + Activity activity = (Activity) context; + Citrus citrus = (Citrus) activity.getApplication(); + citrus.setCurYear(c.get(Calendar.YEAR)); + citrus.setCurMonth(c.get(Calendar.MONTH) + 1); + citrus.setCurDay(c.get(Calendar.DATE)); + citrus.setCurBookId(publicData.getBookId()); + ((MainActivity) activity).showFragment(new HomeFragment()); + + }); + + } + + @Override + public int getItemCount() { + return publicList.size(); + } + + static class PublicViewHolder extends RecyclerView.ViewHolder{ + Button publicButton; + TextView idText; + public PublicViewHolder(@NonNull View itemView){ + + super(itemView); + publicButton = itemView.findViewById(R.id.public_button); + idText = itemView.findViewById(R.id.public_id); + } + } +} + + +/*recyclerviewの中にrecyclerviewを入れよう(縦スクに横スクをいれる感じ) +linearlayoutを縦にいれる + + */ \ No newline at end of file diff --git a/app/src/main/res/layout/a_public_book.xml b/app/src/main/res/layout/a_public_book.xml new file mode 100644 index 0000000..513c9af --- /dev/null +++ b/app/src/main/res/layout/a_public_book.xml @@ -0,0 +1,31 @@ + + + +