From f4b997b41fbfa1747624659f8cd94274bd339db8 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sat, 28 Sep 2019 22:14:57 +0200 Subject: [PATCH] [Api/Librus] Add syncing and saving teacher absence --- .../edziennik/data/api/Librus.java | 45 ++- .../edziennik/data/db/AppDb.java | 24 +- .../db/modules/lessons/LessonChangeDao.java | 2 +- .../data/db/modules/metadata/Metadata.java | 1 + .../data/db/modules/metadata/MetadataDao.java | 2 +- .../db/modules/teachers/TeacherAbsence.kt | 27 ++ .../db/modules/teachers/TeacherAbsenceDao.kt | 26 ++ .../db/modules/teachers/TeacherAbsenceFull.kt | 14 + .../modules/agenda/AgendaDefaultFragment.java | 337 +++++++++--------- .../modules/agenda}/LessonChangeCounter.java | 2 +- .../utils/models/{db => }/UnreadCounter.kt | 2 +- 11 files changed, 298 insertions(+), 184 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceDao.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt rename app/src/main/java/pl/szczodrzynski/edziennik/{utils/models/db => ui/modules/agenda}/LessonChangeCounter.java (77%) rename app/src/main/java/pl/szczodrzynski/edziennik/utils/models/{db => }/UnreadCounter.kt (86%) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java index aa7f635f..0887267f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java @@ -72,6 +72,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile; import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull; import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject; import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher; +import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsence; import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesComposeInfo; import pl.szczodrzynski.edziennik.utils.models.Date; @@ -170,6 +171,7 @@ public class Librus implements EdziennikInterface { private List teamList; private List teacherList; + private List teacherAbsenceList; private List subjectList; private List lessonList; private List lessonChangeList; @@ -240,6 +242,7 @@ public class Librus implements EdziennikInterface { teamList = profileId == -1 ? new ArrayList<>() : app.db.teamDao().getAllNow(profileId); teacherList = profileId == -1 ? new ArrayList<>() : app.db.teacherDao().getAllNow(profileId); + teacherAbsenceList = new ArrayList<>(); subjectList = new ArrayList<>(); lessonList = new ArrayList<>(); lessonChangeList = new ArrayList<>(); @@ -293,6 +296,7 @@ public class Librus implements EdziennikInterface { targetEndpoints.add("BehaviourGrades"); targetEndpoints.add("Events"); + targetEndpoints.add("TeacherFreeDays"); targetEndpoints.add("CustomTypes"); targetEndpoints.add("Homework"); targetEndpoints.add("LuckyNumbers"); @@ -349,6 +353,7 @@ public class Librus implements EdziennikInterface { targetEndpoints.add("CustomTypes"); targetEndpoints.add("PtMeetings"); targetEndpoints.add("SchoolFreeDays"); + targetEndpoints.add("TeacherFreeDays"); break; case FEATURE_GRADES: targetEndpoints.add("SavedGradeCategories"); @@ -589,6 +594,8 @@ public class Librus implements EdziennikInterface { } if (eventTypeList.size() > 0) app.db.eventTypeDao().addAll(eventTypeList); + if (teacherAbsenceList.size() > 0) + app.db.teacherAbsenceDao().addAll(teacherAbsenceList); if (noticeList.size() > 0) { app.db.noticeDao().clear(profileId); app.db.noticeDao().addAll(noticeList); @@ -3107,6 +3114,10 @@ public class Librus implements EdziennikInterface { private SparseArray teacherFreeDaysTypes = new SparseArray<>(); private void getTeacherFreeDaysTypes() { + if (!fullSync) { + r("finish", "TeacherFreeDaysTypes"); + return; + } callback.onActionStarted(R.string.sync_action_syncing_teacher_free_days_types); apiRequest("TeacherFreeDays/Types", data -> { if (data == null) { @@ -3141,27 +3152,35 @@ public class Librus implements EdziennikInterface { JsonObject freeDay = freeDayEl.getAsJsonObject(); long id = freeDay.get("Id").getAsLong(); + long teacherId = freeDay.getAsJsonObject("Teacher").get("Id").getAsLong(); Date dateFrom = Date.fromY_m_d(freeDay.get("DateFrom").getAsString()); Date dateTo = Date.fromY_m_d(freeDay.get("DateTo").getAsString()); - int type = freeDay.getAsJsonObject("Type").get("Id").getAsInt(); - String topic = teacherFreeDaysTypes.get(type)+"\n"+(dateFrom.getValue() != dateTo.getValue() ? dateFrom.getFormattedString()+" - "+dateTo.getFormattedString() : ""); - Event eventObject = new Event( + long type = freeDay.getAsJsonObject("Type").get("Id").getAsLong(); + + //String topic = teacherFreeDaysTypes.get(type)+"\n"+(dateFrom.getValue() != dateTo.getValue() ? dateFrom.getFormattedString()+" - "+dateTo.getFormattedString() : ""); + + TeacherAbsence teacherAbsence = new TeacherAbsence( profileId, id, + teacherId, + type, dateFrom, - null, - topic, - -1, - TYPE_TEACHER_ABSENCE, - false, - freeDay.getAsJsonObject("Teacher").get("Id").getAsLong(), - -1, - -1 + dateTo ); - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_EVENT, eventObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); + + teacherAbsenceList.add(teacherAbsence); + metadataList.add( + new Metadata( + profileId, + Metadata.TYPE_TEACHER_ABSENCE, + teacherAbsence.getId(), + true, + true, + System.currentTimeMillis()) + ); + } r("finish", "TeacherFreeDays"); } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java index 647a94aa..207bd749 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java @@ -1,5 +1,6 @@ package pl.szczodrzynski.edziennik.data.db; +import androidx.annotation.NonNull; import androidx.sqlite.db.SupportSQLiteDatabase; import androidx.room.Database; import androidx.room.Room; @@ -49,6 +50,8 @@ import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileDao; import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject; import pl.szczodrzynski.edziennik.data.db.modules.subjects.SubjectDao; import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher; +import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsence; +import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsenceDao; import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherDao; import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; import pl.szczodrzynski.edziennik.data.db.modules.teams.TeamDao; @@ -60,6 +63,7 @@ import android.content.Context; Grade.class, //GradeCategory.class, Teacher.class, + TeacherAbsence.class, Subject.class, Notice.class, Lesson.class, @@ -77,7 +81,7 @@ import android.content.Context; Message.class, MessageRecipient.class, DebugLog.class, - Metadata.class}, version = 52) + Metadata.class}, version = 53) @TypeConverters({ ConverterTime.class, ConverterDate.class, @@ -89,6 +93,7 @@ public abstract class AppDb extends RoomDatabase { public abstract GradeDao gradeDao(); //public abstract GradeCategoryDao gradeCategoryDao(); public abstract TeacherDao teacherDao(); + public abstract TeacherAbsenceDao teacherAbsenceDao(); public abstract SubjectDao subjectDao(); public abstract NoticeDao noticeDao(); public abstract LessonDao lessonDao(); @@ -532,6 +537,20 @@ public abstract class AppDb extends RoomDatabase { database.execSQL("ALTER TABLE teachers ADD teacherTypeDescription TEXT DEFAULT NULL"); } }; + private static final Migration MIGRATION_52_53 = new Migration(52, 53) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("CREATE TABLE IF NOT EXISTS teacherAbsence (" + + "profileId INTEGER NOT NULL," + + "teacherAbsenceId INTEGER NOT NULL," + + "teacherId INTEGER NOT NULL," + + "teacherAbsenceType INTEGER NOT NULL," + + "teacherAbsenceDateFrom TEXT NOT NULL," + + "teacherAbsenceDateTo TEXT NOT NULL," + + "PRIMARY KEY(profileId, teacherAbsenceId)" + + ")"); + } + }; public static AppDb getDatabase(final Context context) { @@ -581,7 +600,8 @@ public abstract class AppDb extends RoomDatabase { MIGRATION_48_49, MIGRATION_49_50, MIGRATION_50_51, - MIGRATION_51_52) + MIGRATION_51_52, + MIGRATION_52_53) .allowMainThreadQueries() //.fallbackToDestructiveMigration() .build(); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonChangeDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonChangeDao.java index d270eed3..2200ccd6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonChangeDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonChangeDao.java @@ -12,7 +12,7 @@ import androidx.room.RawQuery; import java.util.List; import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.db.LessonChangeCounter; +import pl.szczodrzynski.edziennik.ui.modules.agenda.LessonChangeCounter; import static pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata.TYPE_LESSON_CHANGE; import static pl.szczodrzynski.edziennik.utils.Utils.d; diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/Metadata.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/Metadata.java index 3e5be9f0..a621fe26 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/Metadata.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/Metadata.java @@ -18,6 +18,7 @@ public class Metadata { public static final int TYPE_LESSON_CHANGE = 6; public static final int TYPE_ANNOUNCEMENT = 7; public static final int TYPE_MESSAGE = 8; + public static final int TYPE_TEACHER_ABSENCE = 9; public int profileId; diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/MetadataDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/MetadataDao.java index e7f3a410..d86b9f16 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/MetadataDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/metadata/MetadataDao.java @@ -17,7 +17,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade; import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange; import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonFull; import pl.szczodrzynski.edziennik.data.db.modules.messages.Message; -import pl.szczodrzynski.edziennik.utils.models.db.UnreadCounter; +import pl.szczodrzynski.edziennik.utils.models.UnreadCounter; import static pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata.TYPE_ANNOUNCEMENT; import static pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata.TYPE_ATTENDANCE; diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt new file mode 100644 index 00000000..a8839fc9 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt @@ -0,0 +1,27 @@ +package pl.szczodrzynski.edziennik.data.db.modules.teachers + +import androidx.room.ColumnInfo +import androidx.room.Entity +import pl.szczodrzynski.edziennik.utils.models.Date + +@Entity(tableName = "teacherAbsence", + primaryKeys = ["profileId", "teacherAbsenceId"]) +open class TeacherAbsence ( + + val profileId: Int, + + @ColumnInfo(name = "teacherAbsenceId") + val id: Long, + + val teacherId: Long, + + @ColumnInfo(name = "teacherAbsenceType") + val type: Long, + + @ColumnInfo(name = "teacherAbsenceDateFrom") + val dateFrom: Date, + + @ColumnInfo(name = "teacherAbsenceDateTo") + val dateTo: Date + +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceDao.kt new file mode 100644 index 00000000..68f2ae95 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceDao.kt @@ -0,0 +1,26 @@ +package pl.szczodrzynski.edziennik.data.db.modules.teachers + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import pl.szczodrzynski.edziennik.utils.models.Date + +@Dao +interface TeacherAbsenceDao { + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun add(teacherAbsence: TeacherAbsence) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun addAll(teacherAbsenceList: List) + + @Query("SELECT profileId, teacherAbsenceDateFrom, teacherAbsenceDateTo, COUNT(*) as teacherAbsenceCount" + + "from teacherAbsence WHERE profileId = :profileId GROUP BY teacherAbsenceDateFrom, teacherAbsenceDateTo") + fun getCounters(profileId: Int) + + @Query("SELECT profileId, teacherAbsenceDateFrom, teacherAbsenceDateTo, COUNT(*) as teacherAbsenceCount" + + "from teacherAbsence WHERE profileId = :profileId AND :date BETWEEN teacherAbsenceDateFrom and teacherAbsenceDateTo" + + "GROUP BY teacherAbsenceDateFrom, teacherAbsenceDateTo") + fun getCounterByDate(profileId: Int, date: Date) +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt new file mode 100644 index 00000000..b37f24f1 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt @@ -0,0 +1,14 @@ +package pl.szczodrzynski.edziennik.data.db.modules.teachers + +import pl.szczodrzynski.edziennik.utils.models.Date + +class TeacherAbsenceFull(profileId: Int, id: Long, teacherId: Long, type: Long, dateFrom: Date, dateTo: Date) + : TeacherAbsence(profileId, id, teacherId, type, dateFrom, dateTo) { + + var teacherFullName = "" + + // metadata + var seen: Boolean = false + var notified: Boolean = false + var addedDate: Long = 0 +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaDefaultFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaDefaultFragment.java index ec5f847b..98a2f18f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaDefaultFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaDefaultFragment.java @@ -45,7 +45,6 @@ import pl.szczodrzynski.edziennik.ui.dialogs.event.EventManualDialog; import pl.szczodrzynski.edziennik.ui.dialogs.lessonchange.LessonChangeDialog; import pl.szczodrzynski.edziennik.utils.models.Date; import pl.szczodrzynski.edziennik.utils.models.Time; -import pl.szczodrzynski.edziennik.utils.models.db.LessonChangeCounter; import pl.szczodrzynski.edziennik.utils.Colors; import pl.szczodrzynski.edziennik.utils.Themes; import pl.szczodrzynski.edziennik.utils.Utils; @@ -136,68 +135,76 @@ public class AgendaDefaultFragment extends Fragment { activity.gainAttention(); if (viewType == AGENDA_DEFAULT) { - List unreadEventDates = new ArrayList<>(); + createDefaultAgendaView(); + } + else { + createCalendarAgendaView(); + } + } - final Handler handler = new Handler(); - handler.postDelayed(() -> AsyncTask.execute(() -> { - if (app == null || app.profile == null || activity == null || b_default == null || !isAdded()) - return; + private void createDefaultAgendaView() { + List unreadEventDates = new ArrayList<>(); - List eventList = new ArrayList<>(); + final Handler handler = new Handler(); + handler.postDelayed(() -> AsyncTask.execute(() -> { + if (app == null || app.profile == null || activity == null || b_default == null || !isAdded()) + return; - List lessonChangeCounters = app.db.lessonChangeDao().getLessonChangeCountersNow(App.profileId); - for (LessonChangeCounter counter : lessonChangeCounters) { - Calendar startTime = Calendar.getInstance(); - Calendar endTime = Calendar.getInstance(); - if (counter.lessonChangeDate == null) { - continue; - } - startTime.set(counter.lessonChangeDate.year, counter.lessonChangeDate.month - 1, counter.lessonChangeDate.day, 10, 0, 0); - endTime.setTimeInMillis(startTime.getTimeInMillis() + (1000 * 60 * 45)); - eventList.add(new LessonChangeEvent( - counter.lessonChangeDate.getInMillis(), - 0xff78909c, - Colors.legibleTextColor(0xff78909c), - startTime, - endTime, - counter.profileId, - counter.lessonChangeDate, - counter.lessonChangeCount - )); + List eventList = new ArrayList<>(); + + List lessonChangeCounters = app.db.lessonChangeDao().getLessonChangeCountersNow(App.profileId); + for (LessonChangeCounter counter : lessonChangeCounters) { + Calendar startTime = Calendar.getInstance(); + Calendar endTime = Calendar.getInstance(); + if (counter.lessonChangeDate == null) { + continue; } + startTime.set(counter.lessonChangeDate.year, counter.lessonChangeDate.month - 1, counter.lessonChangeDate.day, 10, 0, 0); + endTime.setTimeInMillis(startTime.getTimeInMillis() + (1000 * 60 * 45)); + eventList.add(new LessonChangeEvent( + counter.lessonChangeDate.getInMillis(), + 0xff78909c, + Colors.legibleTextColor(0xff78909c), + startTime, + endTime, + counter.profileId, + counter.lessonChangeDate, + counter.lessonChangeCount + )); + } - List events = app.db.eventDao().getAllNow(App.profileId); - for (EventFull event : events) { - Calendar startTime = Calendar.getInstance(); - Calendar endTime = Calendar.getInstance(); - if (event.eventDate == null) - continue; - startTime.set( - event.eventDate.year, - event.eventDate.month - 1, - event.eventDate.day, - event.startTime == null ? 0 : event.startTime.hour, - event.startTime == null ? 0 : event.startTime.minute, - event.startTime == null ? 0 : event.startTime.second - ); - endTime.setTimeInMillis(startTime.getTimeInMillis() + (1000 * 60 * 45)); - eventList.add(new BaseCalendarEvent(event.typeName + " - " + event.topic, - "", - (event.startTime == null ? getString(R.string.agenda_event_all_day) : event.startTime.getStringHM()) + - Utils.bs(", ", event.subjectLongName) + - Utils.bs(", ", event.teacherFullName) + - Utils.bs(", ", event.teamName), - event.getColor(), - Colors.legibleTextColor(event.getColor()), - startTime, - endTime, - event.startTime == null, - event.id, !event.seen)); - if (!event.seen) { - unreadEventDates.add(event.eventDate.getValue()); - } + List events = app.db.eventDao().getAllNow(App.profileId); + for (EventFull event : events) { + Calendar startTime = Calendar.getInstance(); + Calendar endTime = Calendar.getInstance(); + if (event.eventDate == null) + continue; + startTime.set( + event.eventDate.year, + event.eventDate.month - 1, + event.eventDate.day, + event.startTime == null ? 0 : event.startTime.hour, + event.startTime == null ? 0 : event.startTime.minute, + event.startTime == null ? 0 : event.startTime.second + ); + endTime.setTimeInMillis(startTime.getTimeInMillis() + (1000 * 60 * 45)); + eventList.add(new BaseCalendarEvent(event.typeName + " - " + event.topic, + "", + (event.startTime == null ? getString(R.string.agenda_event_all_day) : event.startTime.getStringHM()) + + Utils.bs(", ", event.subjectLongName) + + Utils.bs(", ", event.teacherFullName) + + Utils.bs(", ", event.teamName), + event.getColor(), + Colors.legibleTextColor(event.getColor()), + startTime, + endTime, + event.startTime == null, + event.id, !event.seen)); + if (!event.seen) { + unreadEventDates.add(event.eventDate.getValue()); } + } /*List lessonChanges = app.db.lessonChangeDao().getAllChangesWithLessonsNow(App.profileId); for (LessonFull lesson: lessonChanges) { @@ -232,126 +239,126 @@ public class AgendaDefaultFragment extends Fragment { (int)lesson.changeId, false)); }*/ - activity.runOnUiThread(() -> { - AgendaCalendarView mAgendaCalendarView = b_default.agendaDefaultView; - // minimum and maximum date of our calendar - // 2 month behind, one year ahead, example: March 2015 <-> May 2015 <-> May 2016 - Calendar minDate = Calendar.getInstance(); - Calendar maxDate = Calendar.getInstance(); + activity.runOnUiThread(() -> { + AgendaCalendarView mAgendaCalendarView = b_default.agendaDefaultView; + // minimum and maximum date of our calendar + // 2 month behind, one year ahead, example: March 2015 <-> May 2015 <-> May 2016 + Calendar minDate = Calendar.getInstance(); + Calendar maxDate = Calendar.getInstance(); - minDate.add(Calendar.MONTH, -2); - minDate.set(Calendar.DAY_OF_MONTH, 1); - maxDate.add(Calendar.MONTH, 2); + minDate.add(Calendar.MONTH, -2); + minDate.set(Calendar.DAY_OF_MONTH, 1); + maxDate.add(Calendar.MONTH, 2); - mAgendaCalendarView.init(eventList, minDate, maxDate, Locale.getDefault(), new CalendarPickerController() { - @Override - public void onDaySelected(IDayItem dayItem) { - } - - @Override - public void onScrollToDate(Calendar calendar) { - int scrolledDate = Date.fromCalendar(calendar).getValue(); - if (unreadEventDates.contains(scrolledDate)) { - AsyncTask.execute(() -> app.db.eventDao().setSeenByDate(App.profileId, Date.fromYmd(intToStr(scrolledDate)), true)); - unreadEventDates.remove(unreadEventDates.indexOf(scrolledDate)); - } - } - - @Override - public void onEventSelected(CalendarEvent calendarEvent) { - if (calendarEvent instanceof BaseCalendarEvent) { - if (!calendarEvent.isPlaceholder() && !calendarEvent.isAllDay()) { - new EventListDialog(activity).show(app, Date.fromCalendar(calendarEvent.getInstanceDay()), Time.fromMillis(calendarEvent.getStartTime().getTimeInMillis()), true); - } else { - new EventListDialog(activity).show(app, Date.fromCalendar(calendarEvent.getInstanceDay())); - } - } else if (calendarEvent instanceof LessonChangeEvent) { - new LessonChangeDialog(activity).show(app, Date.fromCalendar(calendarEvent.getInstanceDay())); - //Toast.makeText(app, "Clicked "+((LessonChangeEvent) calendarEvent).getLessonChangeDate().getFormattedString(), Toast.LENGTH_SHORT).show(); - } - } - }, new LessonChangeEventRenderer()); - b_default.progressBar.setVisibility(View.GONE); - }); - }), 500); - } - else { - List unreadEventDates = new ArrayList<>(); - - final Handler handler = new Handler(); - handler.postDelayed(() -> AsyncTask.execute(() -> { - if (app == null || app.profile == null || activity == null || b_calendar == null || !isAdded()) - return; - Context c = getContext(); - Activity a = getActivity(); - assert c != null; - assert a != null; - if (!isAdded()) { - return; - } - - List eventList = new ArrayList<>(); - - List events = app.db.eventDao().getAllNow(App.profileId); - for (EventFull event : events) { - if (event.eventDate == null) - continue; - Calendar startTime = Calendar.getInstance(); - startTime.set( - event.eventDate.year, - event.eventDate.month - 1, - event.eventDate.day, - event.startTime == null ? 0 : event.startTime.hour, - event.startTime == null ? 0 : event.startTime.minute, - event.startTime == null ? 0 : event.startTime.second - ); - Drawable eventIcon = new IconicsDrawable(activity).icon(CommunityMaterial.Icon.cmd_checkbox_blank_circle).size(IconicsSize.dp(10)).color(IconicsColor.colorInt(event.getColor())); - eventList.add(new EventDay(startTime, eventIcon)); - if (!event.seen) { - unreadEventDates.add(event.eventDate.getValue()); + mAgendaCalendarView.init(eventList, minDate, maxDate, Locale.getDefault(), new CalendarPickerController() { + @Override + public void onDaySelected(IDayItem dayItem) { } - } - List lessonChanges = app.db.lessonChangeDao().getAllChangesWithLessonsNow(App.profileId); - - for (LessonFull lesson: lessonChanges) { - Calendar startTime = Calendar.getInstance(); - if (lesson.lessonDate == null) { - continue; - } - startTime.set( - lesson.lessonDate.year, - lesson.lessonDate.month - 1, - lesson.lessonDate.day, - lesson.startTime.hour, - lesson.startTime.minute, - lesson.startTime.second); - Drawable eventIcon = new IconicsDrawable(activity).icon(CommunityMaterial.Icon.cmd_checkbox_blank_circle).size(IconicsSize.dp(10)).color(IconicsColor.colorInt(0xff78909c)); - eventList.add(new EventDay(startTime, eventIcon)); - } - - getActivity().runOnUiThread(() -> { - //List eventList = new ArrayList<>(); - - //Collections.sort(eventList, new EventListComparator()); - - CalendarView calendarView = b_calendar.agendaCalendarView; - calendarView.setEvents(eventList); - calendarView.setOnDayClickListener(eventDay -> { - Date dayDate = Date.fromCalendar(eventDay.getCalendar()); - int scrolledDate = dayDate.getValue(); + @Override + public void onScrollToDate(Calendar calendar) { + int scrolledDate = Date.fromCalendar(calendar).getValue(); if (unreadEventDates.contains(scrolledDate)) { AsyncTask.execute(() -> app.db.eventDao().setSeenByDate(App.profileId, Date.fromYmd(intToStr(scrolledDate)), true)); unreadEventDates.remove(unreadEventDates.indexOf(scrolledDate)); } + } - new EventListDialog(getContext()).show(app, dayDate); - }); - b_calendar.progressBar.setVisibility(View.GONE); + @Override + public void onEventSelected(CalendarEvent calendarEvent) { + if (calendarEvent instanceof BaseCalendarEvent) { + if (!calendarEvent.isPlaceholder() && !calendarEvent.isAllDay()) { + new EventListDialog(activity).show(app, Date.fromCalendar(calendarEvent.getInstanceDay()), Time.fromMillis(calendarEvent.getStartTime().getTimeInMillis()), true); + } else { + new EventListDialog(activity).show(app, Date.fromCalendar(calendarEvent.getInstanceDay())); + } + } else if (calendarEvent instanceof LessonChangeEvent) { + new LessonChangeDialog(activity).show(app, Date.fromCalendar(calendarEvent.getInstanceDay())); + //Toast.makeText(app, "Clicked "+((LessonChangeEvent) calendarEvent).getLessonChangeDate().getFormattedString(), Toast.LENGTH_SHORT).show(); + } + } + }, new LessonChangeEventRenderer()); + b_default.progressBar.setVisibility(View.GONE); + }); + }), 500); + } + + private void createCalendarAgendaView() { + List unreadEventDates = new ArrayList<>(); + + final Handler handler = new Handler(); + handler.postDelayed(() -> AsyncTask.execute(() -> { + if (app == null || app.profile == null || activity == null || b_calendar == null || !isAdded()) + return; + Context c = getContext(); + Activity a = getActivity(); + assert c != null; + assert a != null; + if (!isAdded()) { + return; + } + + List eventList = new ArrayList<>(); + + List events = app.db.eventDao().getAllNow(App.profileId); + for (EventFull event : events) { + if (event.eventDate == null) + continue; + Calendar startTime = Calendar.getInstance(); + startTime.set( + event.eventDate.year, + event.eventDate.month - 1, + event.eventDate.day, + event.startTime == null ? 0 : event.startTime.hour, + event.startTime == null ? 0 : event.startTime.minute, + event.startTime == null ? 0 : event.startTime.second + ); + Drawable eventIcon = new IconicsDrawable(activity).icon(CommunityMaterial.Icon.cmd_checkbox_blank_circle).size(IconicsSize.dp(10)).color(IconicsColor.colorInt(event.getColor())); + eventList.add(new EventDay(startTime, eventIcon)); + if (!event.seen) { + unreadEventDates.add(event.eventDate.getValue()); + } + } + + List lessonChanges = app.db.lessonChangeDao().getAllChangesWithLessonsNow(App.profileId); + + for (LessonFull lesson: lessonChanges) { + Calendar startTime = Calendar.getInstance(); + if (lesson.lessonDate == null) { + continue; + } + startTime.set( + lesson.lessonDate.year, + lesson.lessonDate.month - 1, + lesson.lessonDate.day, + lesson.startTime.hour, + lesson.startTime.minute, + lesson.startTime.second); + Drawable eventIcon = new IconicsDrawable(activity).icon(CommunityMaterial.Icon.cmd_checkbox_blank_circle).size(IconicsSize.dp(10)).color(IconicsColor.colorInt(0xff78909c)); + eventList.add(new EventDay(startTime, eventIcon)); + } + + getActivity().runOnUiThread(() -> { + //List eventList = new ArrayList<>(); + + //Collections.sort(eventList, new EventListComparator()); + + CalendarView calendarView = b_calendar.agendaCalendarView; + calendarView.setEvents(eventList); + calendarView.setOnDayClickListener(eventDay -> { + Date dayDate = Date.fromCalendar(eventDay.getCalendar()); + int scrolledDate = dayDate.getValue(); + if (unreadEventDates.contains(scrolledDate)) { + AsyncTask.execute(() -> app.db.eventDao().setSeenByDate(App.profileId, Date.fromYmd(intToStr(scrolledDate)), true)); + unreadEventDates.remove(unreadEventDates.indexOf(scrolledDate)); + } + + new EventListDialog(getContext()).show(app, dayDate); }); - }), 300); - } + b_calendar.progressBar.setVisibility(View.GONE); + }); + }), 300); } public static class EventListComparator implements java.util.Comparator { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/db/LessonChangeCounter.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/LessonChangeCounter.java similarity index 77% rename from app/src/main/java/pl/szczodrzynski/edziennik/utils/models/db/LessonChangeCounter.java rename to app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/LessonChangeCounter.java index 2ebf319a..799794d8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/db/LessonChangeCounter.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/LessonChangeCounter.java @@ -1,4 +1,4 @@ -package pl.szczodrzynski.edziennik.utils.models.db; +package pl.szczodrzynski.edziennik.ui.modules.agenda; import pl.szczodrzynski.edziennik.utils.models.Date; diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/db/UnreadCounter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/UnreadCounter.kt similarity index 86% rename from app/src/main/java/pl/szczodrzynski/edziennik/utils/models/db/UnreadCounter.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/utils/models/UnreadCounter.kt index 7e05bc2b..73fbb1a8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/db/UnreadCounter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/UnreadCounter.kt @@ -1,4 +1,4 @@ -package pl.szczodrzynski.edziennik.utils.models.db +package pl.szczodrzynski.edziennik.utils.models import pl.szczodrzynski.navlib.drawer.IUnreadCounter