[Agenda] Add teachers absence counting as event

This commit is contained in:
Kacper Ziubryniewicz 2019-09-29 00:07:29 +02:00
parent f4b997b41f
commit 0b0cd4c76e
16 changed files with 333 additions and 156 deletions

View File

@ -56,7 +56,7 @@ import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesFragment
import pl.szczodrzynski.edziennik.utils.models.NavTarget
import pl.szczodrzynski.edziennik.network.ServerRequest
import pl.szczodrzynski.edziennik.sync.SyncJob
import pl.szczodrzynski.edziennik.ui.modules.agenda.AgendaDefaultFragment
import pl.szczodrzynski.edziennik.ui.modules.agenda.AgendaFragment
import pl.szczodrzynski.edziennik.ui.modules.announcements.AnnouncementsFragment
import pl.szczodrzynski.edziennik.ui.modules.attendance.AttendanceFragment
import pl.szczodrzynski.edziennik.ui.modules.base.DebugFragment
@ -128,7 +128,7 @@ class MainActivity : AppCompatActivity() {
.withBadgeTypeId(TYPE_LESSON_CHANGE)
.isInDrawer(true)
list += NavTarget(DRAWER_ITEM_AGENDA, R.string.menu_agenda, AgendaDefaultFragment::class)
list += NavTarget(DRAWER_ITEM_AGENDA, R.string.menu_agenda, AgendaFragment::class)
.withIcon(CommunityMaterial.Icon.cmd_calendar)
.withBadgeTypeId(TYPE_EVENT)
.isInDrawer(true)

View File

@ -12,7 +12,7 @@ import androidx.room.RawQuery;
import java.util.List;
import pl.szczodrzynski.edziennik.utils.models.Date;
import pl.szczodrzynski.edziennik.ui.modules.agenda.LessonChangeCounter;
import pl.szczodrzynski.edziennik.ui.modules.agenda.lessonchange.LessonChangeCounter;
import static pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata.TYPE_LESSON_CHANGE;
import static pl.szczodrzynski.edziennik.utils.Utils.d;

View File

@ -4,7 +4,7 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import pl.szczodrzynski.edziennik.utils.models.Date
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
@Dao
interface TeacherAbsenceDao {
@ -15,12 +15,13 @@ interface TeacherAbsenceDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addAll(teacherAbsenceList: List<TeacherAbsence>)
@Query("SELECT profileId, teacherAbsenceDateFrom, teacherAbsenceDateTo, COUNT(*) as teacherAbsenceCount" +
"from teacherAbsence WHERE profileId = :profileId GROUP BY teacherAbsenceDateFrom, teacherAbsenceDateTo")
fun getCounters(profileId: Int)
@Query("SELECT * FROM teacherAbsence WHERE profileId = :profileId")
fun getAll(profileId: Int): List<TeacherAbsence>
@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)
@Query("SELECT *, teachers.teacherName || ' ' || teachers.teacherSurname as teacherFullName, " +
"metadata.seen, metadata.notified, metadata.addedDate FROM teacherAbsence " +
"LEFT JOIN teachers USING (profileId, teacherId) " +
"LEFT JOIN metadata ON teacherAbsenceId = thingId AND metadata.thingType = " + Metadata.TYPE_TEACHER_ABSENCE +
" AND metadata.profileId = :profileId WHERE teachers.profileId = :profileId")
fun getAllFull(profileId: Int): List<TeacherAbsenceFull>
}

View File

@ -5,8 +5,6 @@ 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

View File

@ -1,138 +0,0 @@
package pl.szczodrzynski.edziennik.ui.modules.agenda;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.applandeo.materialcalendarview.CalendarView;
import com.applandeo.materialcalendarview.EventDay;
import com.mikepenz.iconics.IconicsColor;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.IconicsSize;
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import pl.szczodrzynski.edziennik.App;
import pl.szczodrzynski.edziennik.R;
import pl.szczodrzynski.edziennik.databinding.FragmentAgendaCalendarBinding;
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull;
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonFull;
import pl.szczodrzynski.edziennik.ui.dialogs.event.EventListDialog;
import pl.szczodrzynski.edziennik.utils.models.Date;
import pl.szczodrzynski.edziennik.utils.Themes;
import static pl.szczodrzynski.edziennik.utils.Utils.intToStr;
public class AgendaCalendarFragment extends Fragment {
private App app = null;
private Activity activity = null;
private FragmentAgendaCalendarBinding b = null;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
activity = getActivity();
if (getActivity() == null || getContext() == null)
return null;
app = (App) activity.getApplication();
getContext().getTheme().applyStyle(Themes.INSTANCE.getAppTheme(), true);
if (app.profile == null)
return inflater.inflate(R.layout.fragment_loading, container, false);
// activity, context and profile is valid
b = DataBindingUtil.inflate(inflater, R.layout.fragment_agenda_calendar, container, false);
return b.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
if (app == null || app.profile == null || activity == null || b == null || !isAdded())
return;
List<Integer> unreadEventDates = new ArrayList<>();
final Handler handler = new Handler();
handler.postDelayed(() -> AsyncTask.execute(() -> {
Context c = getContext();
Activity a = getActivity();
assert c != null;
assert a != null;
if (!isAdded()) {
return;
}
List<EventDay> eventList = new ArrayList<>();
List<EventFull> 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<LessonFull> 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<EventDay> eventList = new ArrayList<>();
//Collections.sort(eventList, new EventListComparator());
CalendarView calendarView = b.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);
});
b.progressBar.setVisibility(View.GONE);
});
}), 300);
}
}

View File

@ -36,6 +36,7 @@ import java.util.Locale;
import pl.szczodrzynski.edziennik.App;
import pl.szczodrzynski.edziennik.R;
import pl.szczodrzynski.edziennik.MainActivity;
import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsenceFull;
import pl.szczodrzynski.edziennik.databinding.FragmentAgendaCalendarBinding;
import pl.szczodrzynski.edziennik.databinding.FragmentAgendaDefaultBinding;
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull;
@ -43,6 +44,12 @@ import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonFull;
import pl.szczodrzynski.edziennik.ui.dialogs.event.EventListDialog;
import pl.szczodrzynski.edziennik.ui.dialogs.event.EventManualDialog;
import pl.szczodrzynski.edziennik.ui.dialogs.lessonchange.LessonChangeDialog;
import pl.szczodrzynski.edziennik.ui.modules.agenda.lessonchange.LessonChangeCounter;
import pl.szczodrzynski.edziennik.ui.modules.agenda.lessonchange.LessonChangeEvent;
import pl.szczodrzynski.edziennik.ui.modules.agenda.lessonchange.LessonChangeEventRenderer;
import pl.szczodrzynski.edziennik.ui.modules.agenda.teacherabsence.TeacherAbsenceCounter;
import pl.szczodrzynski.edziennik.ui.modules.agenda.teacherabsence.TeacherAbsenceEvent;
import pl.szczodrzynski.edziennik.ui.modules.agenda.teacherabsence.TeacherAbsenceEventRenderer;
import pl.szczodrzynski.edziennik.utils.models.Date;
import pl.szczodrzynski.edziennik.utils.models.Time;
import pl.szczodrzynski.edziennik.utils.Colors;
@ -56,7 +63,7 @@ import static pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile.AGENDA
import static pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile.AGENDA_DEFAULT;
import static pl.szczodrzynski.edziennik.utils.Utils.intToStr;
public class AgendaDefaultFragment extends Fragment {
public class AgendaFragment extends Fragment {
private App app = null;
private MainActivity activity = null;
@ -174,6 +181,45 @@ public class AgendaDefaultFragment extends Fragment {
}
List<TeacherAbsenceFull> teacherAbsenceList = app.db.teacherAbsenceDao().getAllFull(App.profileId);
List<TeacherAbsenceCounter> teacherAbsenceCounters = new ArrayList<>();
for (TeacherAbsenceFull absence : teacherAbsenceList) {
for (Date date = absence.getDateFrom().clone(); date.compareTo(absence.getDateTo()) < 1; date.stepForward(0, 0, 1)) {
boolean counterFound = false;
for (TeacherAbsenceCounter counter : teacherAbsenceCounters) {
if (counter.getTeacherAbsenceDate().compareTo(date) == 0) {
counter.setTeacherAbsenceCount(counter.getTeacherAbsenceCount() + 1);
counterFound = true;
break;
}
}
if (!counterFound) {
teacherAbsenceCounters.add(new TeacherAbsenceCounter(date.clone(), 1));
}
}
}
for (TeacherAbsenceCounter counter : teacherAbsenceCounters) {
Calendar startTime = Calendar.getInstance();
Calendar endTime = Calendar.getInstance();
Date date = counter.getTeacherAbsenceDate();
startTime.set(date.year, date.month - 1, date.day, 10, 0, 0);
endTime.setTimeInMillis(startTime.getTimeInMillis() + (1000 * 60 * 45));
eventList.add(new TeacherAbsenceEvent(
date.getInMillis(),
0xff8f0119,
Colors.legibleTextColor(0xff8f0119),
startTime,
endTime,
App.profileId,
date,
counter.getTeacherAbsenceCount()
));
}
List<EventFull> events = app.db.eventDao().getAllNow(App.profileId);
for (EventFull event : events) {
Calendar startTime = Calendar.getInstance();
@ -278,7 +324,7 @@ public class AgendaDefaultFragment extends Fragment {
//Toast.makeText(app, "Clicked "+((LessonChangeEvent) calendarEvent).getLessonChangeDate().getFormattedString(), Toast.LENGTH_SHORT).show();
}
}
}, new LessonChangeEventRenderer());
}, new LessonChangeEventRenderer(), new TeacherAbsenceEventRenderer());
b_default.progressBar.setVisibility(View.GONE);
});
}), 500);

View File

@ -1,4 +1,4 @@
package pl.szczodrzynski.edziennik.ui.modules.agenda;
package pl.szczodrzynski.edziennik.ui.modules.agenda.lessonchange;
import pl.szczodrzynski.edziennik.utils.models.Date;

View File

@ -1,4 +1,4 @@
package pl.szczodrzynski.edziennik.ui.modules.agenda;
package pl.szczodrzynski.edziennik.ui.modules.agenda.lessonchange;
import com.github.tibolte.agendacalendarview.models.CalendarEvent;
import com.github.tibolte.agendacalendarview.models.IDayItem;

View File

@ -1,4 +1,4 @@
package pl.szczodrzynski.edziennik.ui.modules.agenda;
package pl.szczodrzynski.edziennik.ui.modules.agenda.lessonchange;
import android.view.View;
import android.widget.TextView;

View File

@ -0,0 +1,10 @@
package pl.szczodrzynski.edziennik.ui.modules.agenda.teacherabsence
import pl.szczodrzynski.edziennik.utils.models.Date
class TeacherAbsenceCounter (
val teacherAbsenceDate: Date,
var teacherAbsenceCount: Int = 0
)

View File

@ -0,0 +1,188 @@
package pl.szczodrzynski.edziennik.ui.modules.agenda.teacherabsence
import com.github.tibolte.agendacalendarview.models.CalendarEvent
import com.github.tibolte.agendacalendarview.models.IDayItem
import com.github.tibolte.agendacalendarview.models.IWeekItem
import pl.szczodrzynski.edziennik.utils.models.Date
import java.util.*
class TeacherAbsenceEvent : CalendarEvent {
/**
* Id of the event.
*/
private var mId: Long = 0
/**
* Color to be displayed in the agenda view.
*/
private var mColor: Int = 0
/**
* Text color displayed on the background color
*/
private var mTextColor: Int = 0
/**
* Calendar instance helping sorting the events per section in the agenda view.
*/
private var mInstanceDay: Calendar? = null
/**
* Start time of the event.
*/
private var mStartTime: Calendar? = null
/**
* End time of the event.
*/
private var mEndTime: Calendar? = null
/**
* References to a DayItem instance for that event, used to link interaction between the
* calendar view and the agenda view.
*/
private var mDayReference: IDayItem? = null
/**
* References to a WeekItem instance for that event, used to link interaction between the
* calendar view and the agenda view.
*/
private var mWeekReference: IWeekItem? = null
private var profileId: Int = 0
var teacherAbsenceDate: Date? = null
var teacherAbsenceCount: Int = 0
constructor(calendarEvent: TeacherAbsenceEvent) {
this.mId = calendarEvent.id
this.mColor = calendarEvent.color
this.mTextColor = calendarEvent.textColor
this.mStartTime = calendarEvent.startTime
this.mEndTime = calendarEvent.endTime
this.profileId = calendarEvent.profileId
this.teacherAbsenceDate = calendarEvent.teacherAbsenceDate
this.teacherAbsenceCount = calendarEvent.teacherAbsenceCount
}
constructor(mId: Long, mColor: Int, mTextColor: Int, mStartTime: Calendar, mEndTime: Calendar, profileId: Int, teacherAbsenceDate: Date, teacherAbsenceCount: Int) {
this.mId = mId
this.mColor = mColor
this.mTextColor = mTextColor
this.mStartTime = mStartTime
this.mEndTime = mEndTime
this.profileId = profileId
this.teacherAbsenceDate = teacherAbsenceDate
this.teacherAbsenceCount = teacherAbsenceCount
}
override fun setPlaceholder(placeholder: Boolean) {
}
override fun isPlaceholder(): Boolean {
return false
}
override fun getLocation(): String? {
return null
}
override fun setLocation(mLocation: String) {
}
override fun getId(): Long {
return mId
}
override fun setId(mId: Long) {
this.mId = mId
}
override fun getShowBadge(): Boolean {
return false
}
override fun setShowBadge(mShowBadge: Boolean) {
}
override fun getTextColor(): Int {
return mTextColor
}
override fun setTextColor(mTextColor: Int) {
this.mTextColor = mTextColor
}
override fun getDescription(): String? {
return null
}
override fun setDescription(mDescription: String) {
}
override fun isAllDay(): Boolean {
return false
}
override fun setAllDay(allDay: Boolean) {
}
override fun getStartTime(): Calendar? {
return mStartTime
}
override fun setStartTime(mStartTime: Calendar) {
this.mStartTime = mStartTime
}
override fun getEndTime(): Calendar? {
return mEndTime
}
override fun setEndTime(mEndTime: Calendar) {
this.mEndTime = mEndTime
}
override fun getTitle(): String? {
return null
}
override fun setTitle(mTitle: String) {
}
override fun getInstanceDay(): Calendar? {
return mInstanceDay
}
override fun setInstanceDay(mInstanceDay: Calendar) {
this.mInstanceDay = mInstanceDay
this.mInstanceDay!!.set(Calendar.HOUR, 0)
this.mInstanceDay!!.set(Calendar.MINUTE, 0)
this.mInstanceDay!!.set(Calendar.SECOND, 0)
this.mInstanceDay!!.set(Calendar.MILLISECOND, 0)
this.mInstanceDay!!.set(Calendar.AM_PM, 0)
}
override fun getDayReference(): IDayItem? {
return mDayReference
}
override fun setDayReference(mDayReference: IDayItem) {
this.mDayReference = mDayReference
}
override fun getWeekReference(): IWeekItem? {
return mWeekReference
}
override fun setWeekReference(mWeekReference: IWeekItem) {
this.mWeekReference = mWeekReference
}
override fun copy(): CalendarEvent {
return TeacherAbsenceEvent(this)
}
override fun getColor(): Int {
return mColor
}
}

View File

@ -0,0 +1,21 @@
package pl.szczodrzynski.edziennik.ui.modules.agenda.teacherabsence
import android.view.View
import android.widget.TextView
import androidx.cardview.widget.CardView
import com.github.tibolte.agendacalendarview.render.EventRenderer
import pl.szczodrzynski.edziennik.R
class TeacherAbsenceEventRenderer : EventRenderer<TeacherAbsenceEvent>() {
override fun render(view: View?, event: TeacherAbsenceEvent) {
val card = view?.findViewById<CardView>(R.id.teacher_absence_card)
val changeText = view?.findViewById<TextView>(R.id.teacher_absence_text)
val changeCount = view?.findViewById<TextView>(R.id.teacher_absence_count)
card?.setCardBackgroundColor(event.color)
changeText?.setTextColor(event.textColor)
changeCount?.setTextColor(event.textColor)
changeCount?.text = event.teacherAbsenceCount.toString()
}
override fun getEventLayout(): Int { return R.layout.agenda_event_teacher_absence }
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<com.github.tibolte.agendacalendarview.agenda.AgendaEventView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<include
layout="@layout/row_dialog_teacher_absence_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</com.github.tibolte.agendacalendarview.agenda.AgendaEventView>

View File

@ -0,0 +1,35 @@
<androidx.cardview.widget.CardView android:id="@+id/teacher_absence_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:cardBackgroundColor="@color/blue_selected"
app:cardCornerRadius="5dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/teacher_absence_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="@style/NavView.TextView.Medium"
android:text="@string/agenda_teacher_absence" />
<TextView
android:id="@+id/teacher_absence_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp"
tools:text="3" />
</LinearLayout>
</androidx.cardview.widget.CardView>

View File

@ -851,4 +851,5 @@
<string name="messages_compose_menu_discard">Abort message</string>
<string name="messages_compose_menu_save_draft">Save draft</string>
<string name="messages_compose_menu_send">Send</string>
<string name="agenda_teacher_absence">Teachers absence</string>
</resources>

View File

@ -908,4 +908,5 @@
<string name="homework_tab_current">Aktualne</string>
<string name="homework_tab_past">Minione</string>
<string name="homework_no_data">Brak zadań domowych.</string>
<string name="agenda_teacher_absence">Nieobecność nauczycieli</string>
</resources>