Skip to content

Commit

Permalink
fix: 优化当前周数的显示
Browse files Browse the repository at this point in the history
  • Loading branch information
liewstar committed Dec 7, 2024
1 parent 8305e05 commit eeecaec
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 54 deletions.
58 changes: 22 additions & 36 deletions app/src/main/java/com/example/CQUPT/adapter/CourseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,16 @@

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.CourseViewHolder> {
private List<Course> courses;
private int currentWeek;

public CourseAdapter(List<Course> courses, int currentWeek) {
public CourseAdapter(List<Course> courses) {
this.courses = courses;
this.currentWeek = currentWeek;
}

public void setCourses(List<Course> courses) {
this.courses = courses;
notifyDataSetChanged();
}

public void setCurrentWeek(int currentWeek) {
this.currentWeek = currentWeek;
notifyDataSetChanged();
}

@NonNull
@Override
public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Expand All @@ -48,7 +41,7 @@ public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewTy
@Override
public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) {
Course course = courses.get(position);
holder.bind(course, currentWeek);
holder.bind(course);
}

@Override
Expand Down Expand Up @@ -76,20 +69,21 @@ public CourseViewHolder(@NonNull View itemView) {
currentWeekText = itemView.findViewById(R.id.currentWeekText);
}

public void bind(Course course, int currentWeek) {
public void bind(Course course) {
Context context = itemView.getContext();

// 设置基本信息
courseTimeText.setText(course.getTimeRange());
courseNameText.setText(course.getName());
locationText.setText(course.getLocation());
teacherText.setText(course.getTeacher());
weekRangeText.setText(course.getWeekRange());
currentWeekText.setText(String.format("第%d周", currentWeek));
currentWeekText.setText(String.format("第%d周", course.getCurrentWeek()));

// 判断课程是否在当前周
boolean isInCurrentWeek = currentWeek >= course.getStartWeek() && currentWeek <= course.getEndWeek();

boolean isInCurrentWeek = course.getCurrentWeek() >= course.getStartWeek()
&& course.getCurrentWeek() <= course.getEndWeek();

// 根据是否在当前周设置卡片样式
if (isInCurrentWeek) {
cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.purple_50));
Expand All @@ -100,33 +94,25 @@ public void bind(Course course, int currentWeek) {
cardView.setCardBackgroundColor(Color.WHITE);
cardView.setStrokeColor(Color.TRANSPARENT);
cardView.setStrokeWidth(0);
courseNameText.setTextColor(ContextCompat.getColor(context, android.R.color.black));
courseNameText.setTextColor(Color.BLACK);
}

// 设置点击事件显示详情
cardView.setOnClickListener(v -> showCourseDetail(context, course, currentWeek));
// 设置点击事件显示课程详情
cardView.setOnClickListener(v -> showCourseDetailDialog(context, course));
}

private void showCourseDetail(Context context, Course course, int currentWeek) {
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_course_detail, null);

// 设置详情对话框的内容
TextView courseNameTitle = dialogView.findViewById(R.id.courseNameTitle);
TextView timeText = dialogView.findViewById(R.id.timeText);
TextView weekRangeDetailText = dialogView.findViewById(R.id.weekRangeDetailText);
TextView locationDetailText = dialogView.findViewById(R.id.locationDetailText);
TextView teacherDetailText = dialogView.findViewById(R.id.teacherDetailText);

courseNameTitle.setText(course.getName());
timeText.setText(course.getTimeRange());
weekRangeDetailText.setText(String.format("%s(当前第%d周)", course.getWeekRange(), currentWeek));
locationDetailText.setText(course.getLocation());
teacherDetailText.setText(course.getTeacher());

// 创建并显示对话框
private void showCourseDetailDialog(Context context, Course course) {
new MaterialAlertDialogBuilder(context)
.setView(dialogView)
.setPositiveButton("关闭", null)
.setTitle(course.getName())
.setMessage(String.format(
"时间: %s\n位置: %s\n教师: %s\n周数: %s\n当前周: 第%d周",
course.getTimeRange(),
course.getLocation(),
course.getTeacher(),
course.getWeekRange(),
course.getCurrentWeek()
))
.setPositiveButton("确定", null)
.show();
}
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/example/CQUPT/model/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ public class Course {
private String teacher;
private int startWeek;
private int endWeek;
private int currentWeek;

public Course(String name, String startTime, String endTime, String location, String teacher, int startWeek, int endWeek) {
public Course(String name, String startTime, String endTime, String location, String teacher, int startWeek, int endWeek, int currentWeek) {
this.name = name;
this.startTime = startTime;
this.endTime = endTime;
this.location = location;
this.teacher = teacher;
this.startWeek = startWeek;
this.endWeek = endWeek;
this.currentWeek = currentWeek;
}

public String getName() {
Expand Down Expand Up @@ -51,6 +53,8 @@ public int getEndWeek() {
return endWeek;
}

public int getCurrentWeek() { return currentWeek; }

public String getWeekRange() {
return String.format("第%d-%d周", startWeek, endWeek);
}
Expand Down
18 changes: 3 additions & 15 deletions app/src/main/java/com/example/CQUPT/ui/home/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,10 @@ private void initializeFormats() {
semesterStartDate.set(2024, Calendar.FEBRUARY, 26);
}

private int getCurrentWeek() {
long diff = currentDate.getTimeInMillis() - semesterStartDate.getTimeInMillis();
int daysDiff = (int) (diff / (24 * 60 * 60 * 1000));
return (daysDiff / 7) + 1;
}

private void setupRecyclerView() {
RecyclerView recyclerView = binding.recyclerCourses;
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
courseAdapter = new CourseAdapter(new ArrayList<>(), getCurrentWeek());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
courseAdapter = new CourseAdapter(new ArrayList<>());
recyclerView.setAdapter(courseAdapter);
}

Expand Down Expand Up @@ -115,13 +109,7 @@ private void showDatePicker() {
private void updateDateDisplay() {
String dateStr = dateFormat.format(currentDate.getTime());
String weekDayStr = weekDayFormat.format(currentDate.getTime());
int currentWeek = getCurrentWeek();
binding.buttonDate.setText(String.format("%s %s (第%d周)", dateStr, weekDayStr, currentWeek));

// 更新适配器中的当前周
if (courseAdapter != null) {
courseAdapter.setCurrentWeek(currentWeek);
}
binding.buttonDate.setText(String.format("%s %s", dateStr, weekDayStr));
}

private void loadCoursesForDate(Date date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public HomeViewModel(Application application) {
apiDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
apiTimeFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application);

loadCoursesForDate(new Date());
}

Expand Down Expand Up @@ -123,14 +123,19 @@ private List<Course> filterCoursesForDate(List<CourseSchedule> schedules, Date t
int startWeek = weekNums.isEmpty() ? 1 : weekNums.get(0);
int endWeek = weekNums.isEmpty() ? 1 : weekNums.get(weekNums.size() - 1);

// 获取当前周
int currentWeek = schedule.getWeekNum();

// 创建课程对象
Course course = new Course(
schedule.getTitle(),
startTime,
endTime,
schedule.getLocation(),
schedule.getData().getTeacherName(),
startWeek,
endWeek
endWeek,
currentWeek
);
courses.add(course);
}
Expand Down

0 comments on commit eeecaec

Please sign in to comment.