TodoとScheduleの色の反映とデザインの修正
1 parent 7a4d089 commit 637f09bd73bf4718aa44bb039528c17e566da652
h-yamamoto authored on 11 Jul
Showing 5 changed files
View
29
app/src/main/java/com/example/citrusclient/models/Todo.java
int year;
int month;
int day;
Integer todoId;
Integer bookId;
 
//コンストラクタ
public Todo(String t, boolean c, int y, int m, int d, Integer tid) {
title = t;
check = c;
year = y;
month = m;
day = d;
public Todo(String title, boolean check, int year, int month, int day, Integer tid, Integer bid) {
this.title = title;
this.check = check;
this.year = year;
this.month = month;
this.day = day;
todoId = tid;
this.bookId = bid;
}
 
public Todo(){}
 
 
 
//セッター
public void setTitle(String t) {title = t;}
public void setCheck(boolean c) {check = c;}
public void setYear(int y) {year = y;}
public void setMonth(int m) {month = m;}
public void setDay(int d) {day = d;}
public void setTodoId(Integer t) {todoId = t;}
public void setTitle(String title) {this.title = title;}
public void setCheck(boolean check) {this.check = check;}
public void setYear(int year) {this.year = year;}
public void setMonth(int month) {this.month = month;}
public void setDay(int day) {this.day = day;}
public void setTodoId(Integer tid) {this.todoId = tid;}
public void setBookId(Integer bid) {this.bookId = bid; }
 
//ゲッター
public String getTitle() {return title;}
 
 
public Integer getTodoId() {
return todoId;
}
public Integer getBookId(){return bookId;}
 
 
}
View
136
app/src/main/java/com/example/citrusclient/views/HomeFragment.java
import com.example.citrusclient.R;
import com.example.citrusclient.models.Book;
import com.example.citrusclient.models.Schedule;
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.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
}
 
todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class);
scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class);
booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class);
 
}
 
 
TodosViewModel todosViewModel;
private List<Schedule> scheduleList;
ScheduleViewModel scheduleViewModel;
 
private HashMap<Integer, Book> integerBookHashMap;
BooksViewModel booksViewModel;
 
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();
int year = citrus.getCurYear();
int month = citrus.getCurMouth();
int day = citrus.getCurDay();
int curBookId = citrus.getCurBookId();
 
todoList = new ArrayList<>();
todoList.add(new Todo("a", true, 2024, 7, 2, 1));
 
scheduleList = new ArrayList<>();
scheduleList.add(new Schedule("a", "1", "1", 1, 1));
 
 
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);
MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap);
todoRecyclerView.setAdapter(todoAdapter);
 
RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list);
scheduleRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext());
scheduleRecyclerView.setLayoutManager(scheduleLayoutManager);
MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList);
MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap);
scheduleRecyclerView.setAdapter(scheduleAdapter);
 
 
booksViewModel.loadBooks(accountId, token);
System.out.println(token);
 
booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() {
@Override
public void onChanged(HashMap<Integer, Book> idBookHashMap) {
if(idBookHashMap != null) {
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);
}
}
});
 
todosViewModel.getTodosByDayLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Todo>>() {
@Override
public void onChanged(HashMap<Integer, Todo> idTodoHashMap) {
if(idTodoHashMap != null) {
todoList = new ArrayList<>(idTodoHashMap.values());
todoAdapter.setTodos(todoList);
todoList.addAll(idTodoHashMap.values());
todoAdapter.setTodos(todoList, integerBookHashMap);
}
}
});
 
@Override
public void onChanged(HashMap<Integer, Schedule> idScheduleHashMap) {
if(idScheduleHashMap != null) {
scheduleList = new ArrayList<>(idScheduleHashMap.values());
scheduleAdapter.setSchedules(scheduleList);
scheduleAdapter.setSchedules(scheduleList, integerBookHashMap);
}
}
});
scheduleViewModel.updateSchedulesByDay("bird", 2024, 5, 28, "xyz");
todosViewModel.loadTodosByDay("bird", 1, 2024, 7, 2, "xyz");
 
}
 
 
 
 
}
 
}
 
class MyTodoshelfAdapter extends RecyclerView.Adapter<MyTodoshelfAdapter.MyTodoViewHolder> {
 
private List<Todo> todoList;
 
MyTodoshelfAdapter(List<Todo> todos) {
private HashMap<Integer, Book> idBookHashMap;
MyTodoshelfAdapter(List<Todo> todos, HashMap<Integer, Book> idBookHashMap) {
this.todoList = todos;
}
 
public void setTodos(List<Todo> todos) {
if (idBookHashMap != null) {
this.idBookHashMap = new HashMap<>(idBookHashMap);
} else {
this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
} }
 
public void setTodos(List<Todo> todos ,HashMap<Integer, Book> idBookHashMap) {
todoList = todos;
if (idBookHashMap != null) {
this.idBookHashMap = new HashMap<>(idBookHashMap);
} else {
this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
}
notifyDataSetChanged();
}
 
@NonNull
public void onBindViewHolder(@NonNull MyTodoViewHolder holder, int position) {
Todo todoData = this.todoList.get(position);
holder.todoButton.setText(todoData.getTitle());
holder.todoCheckBox.setChecked(todoData.getCheck());
Book book = idBookHashMap.get(todoData.getBookId());
if(book != null) {
int red = Integer.parseInt(book.getColor().substring(1, 3), 16);
int green = Integer.parseInt(book.getColor().substring(3, 5), 16);
int blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
holder.todoButton.setBackgroundColor(Color.rgb(red, green, blue));
holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
}
 
}
 
@Override
public int getItemCount() {
 
class MyScheduleshelfAdapter extends RecyclerView.Adapter<MyScheduleshelfAdapter.MyScheduleViewHolder> {
 
private List<Schedule> scheduleList;
 
MyScheduleshelfAdapter(List<Schedule> schedules) {
private HashMap<Integer, Book> idBookHashMap;
 
MyScheduleshelfAdapter(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) {
this.scheduleList = schedules;
}
 
public void setSchedules(List<Schedule> schedules) {
if (idBookHashMap != null) {
this.idBookHashMap = new HashMap<>(idBookHashMap);
} else {
this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
} }
 
public void setSchedules(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) {
scheduleList = schedules;
notifyDataSetChanged();
if (idBookHashMap != null) {
this.idBookHashMap = new HashMap<>(idBookHashMap);
} else {
this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
} notifyDataSetChanged();
}
 
@NonNull
@Override
@Override
public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) {
Schedule scheduleData = this.scheduleList.get(position);
holder.scheduleButton.setText(scheduleData.getTitle());
}
Book book = idBookHashMap.get(scheduleData.getBookId());
if(book != null) {
int red = Integer.parseInt(book.getColor().substring(1, 3), 16);
int green = Integer.parseInt(book.getColor().substring(3, 5), 16);
int blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
holder.scheduleButton.setBackgroundColor(Color.rgb(red, green, blue));
holder.scheduleButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
}
 
}
 
 
@Override
public int getItemCount() {
return scheduleList.size();
View
2
■■■
app/src/main/res/layout/a_schedule.xml
android:layout_margin="5dp">
 
<Button
android:id="@+id/schedule_button"
android:layout_width="180dp"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#000000"
app:cornerRadius="10dp"
View
2
■■■
app/src/main/res/layout/a_todo.xml
app:layout_constraintVertical_bias="0.0" />
 
<Button
android:id="@+id/todo_button"
android:layout_width="120dp"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#000000"
app:cornerRadius="10dp"
View
app/src/main/res/layout/fragment_home.xml