[UI/Agenda] Move common code to EventManager.

This commit is contained in:
Kuba Szczodrzyński 2021-04-14 16:34:31 +02:00
parent 73f3ba17de
commit 755b846b50
No known key found for this signature in database
GPG Key ID: 70CB8A85BA1633CB
12 changed files with 84 additions and 63 deletions

View File

@ -46,6 +46,8 @@ class EventDetailsDialog(
private var removeEventDialog: AlertDialog? = null
private val eventShared = event.sharedBy != null
private val eventOwn = event.sharedBy == "self"
private val manager
get() = app.eventManager
private val job = Job()
override val coroutineContext: CoroutineContext
@ -93,7 +95,7 @@ class EventDetailsDialog(
b.eventOwn = eventOwn
if (!event.seen) {
app.eventManager.markAsSeen(event)
manager.markAsSeen(event)
}
val bullet = ""
@ -104,11 +106,7 @@ class EventDetailsDialog(
}
catch (_: Exception) {}
b.legend.text = listOfNotNull(
if (event.addedManually) R.string.legend_event_added_manually else null,
if (event.isDone) R.string.legend_event_is_done else null
).map { activity.getString(it) }.join("\n")
b.legend.isVisible = b.legend.text.isNotBlank()
manager.setLegendText(b.legend, event)
b.typeColor.background?.setTintColor(event.eventColor)

View File

@ -33,7 +33,8 @@ class EventListAdapter(
) : RecyclerView.Adapter<EventListAdapter.ViewHolder>(), CoroutineScope {
private val app = context.applicationContext as App
private val manager = app.eventManager
private val manager
get() = app.eventManager
private val job = Job()
override val coroutineContext: CoroutineContext
@ -67,7 +68,7 @@ class EventListAdapter(
b.simpleMode = simpleMode
b.topic.text = event.topic
manager.setEventTopic(b.topic, event, showType = false)
b.topic.maxLines = if (simpleMode) 2 else 3
b.details.text = mutableListOf<CharSequence?>(
@ -102,8 +103,6 @@ class EventListAdapter(
}
b.editButton.attachToastHint(R.string.hint_edit_event)
b.isDone.isVisible = event.isDone
if (event.showAsUnseen == null)
event.showAsUnseen = !event.seen

View File

@ -50,7 +50,8 @@ class LessonDetailsDialog(
get() = job + Dispatchers.Main
private lateinit var adapter: EventListAdapter
private val manager by lazy { app.timetableManager }
private val manager
get() = app.timetableManager
init { run {
if (activity.isFinishing)

View File

@ -136,7 +136,13 @@ class AgendaFragmentDefault(
dateEnd,
Locale.getDefault(),
object : CalendarPickerController {
override fun onDaySelected(dayItem: IDayItem) {}
override fun onDaySelected(dayItem: IDayItem) {
val c = Calendar.getInstance()
c.time = dayItem.date
if (c.timeInMillis == selectedDate.inMillis) {
DayDialog(activity, app.profileId, selectedDate)
}
}
override fun onEventSelected(event: CalendarEvent) {
val date = Date.fromCalendar(event.instanceDay)
@ -180,7 +186,7 @@ class AgendaFragmentDefault(
}
}
},
AgendaEventRenderer(isCompactMode),
AgendaEventRenderer(app.eventManager, isCompactMode),
AgendaEventGroupRenderer(),
LessonChangesEventRenderer(),
TeacherAbsenceEventRenderer()
@ -197,7 +203,7 @@ class AgendaFragmentDefault(
manager.loadEvents(events, BaseCalendarEvent())
adapter?.updateEvents(manager.events)
listView.scrollToCurrentDate(selectedDate.asCalendar)
//listView.scrollToCurrentDate(selectedDate.asCalendar)
}
private fun setAsRead(date: Calendar) {

View File

@ -10,10 +10,6 @@ import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.github.tibolte.agendacalendarview.render.EventRenderer
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeDp
import com.mikepenz.iconics.view.IconicsTextView
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.databinding.AgendaWrappedEventBinding
@ -22,8 +18,10 @@ import pl.szczodrzynski.edziennik.join
import pl.szczodrzynski.edziennik.resolveAttr
import pl.szczodrzynski.edziennik.setTintColor
import pl.szczodrzynski.edziennik.utils.Colors
import pl.szczodrzynski.edziennik.utils.managers.EventManager
class AgendaEventRenderer(
val manager: EventManager,
val isCompact: Boolean
) : EventRenderer<AgendaEvent>() {
@ -55,8 +53,6 @@ class AgendaEventRenderer(
else
event.time!!.stringHM
var eventTitle = "${event.typeName ?: "wydarzenie"} - ${event.topic}"
val eventSubtitle = listOfNotNull(
timeText,
event.subjectLongName,
@ -64,28 +60,13 @@ class AgendaEventRenderer(
event.teamName
).join(", ")
if (event.addedManually) {
eventTitle = "{cmd-clipboard-edit-outline} $eventTitle"
}
card.foreground.setTintColor(event.eventColor)
card.background.setTintColor(event.eventColor)
title.text = eventTitle
manager.setEventTopic(title, event, doneIconColor = textColor)
title.setTextColor(textColor)
subtitle?.text = eventSubtitle
subtitle?.setTextColor(textColor)
title.setCompoundDrawables(
null,
null,
if (event.isDone) IconicsDrawable(card.context).apply {
icon = CommunityMaterial.Icon.cmd_check
colorInt = textColor
sizeDp = 24
} else null,
null
)
badgeBackground.isVisible = aEvent.showItemBadge
badgeBackground.background.setTintColor(
android.R.attr.colorBackground.resolveAttr(card.context)

View File

@ -41,7 +41,8 @@ class AttendanceListFragment : LazyFragment(), CoroutineScope {
get() = job + Dispatchers.Main
// local/private variables go here
private val manager by lazy { app.attendanceManager }
private val manager
get() = app.attendanceManager
private var viewType = AttendanceFragment.VIEW_DAYS
private var expandSubjectId = 0L

View File

@ -47,7 +47,8 @@ class AttendanceSummaryFragment : LazyFragment(), CoroutineScope {
get() = job + Dispatchers.Main
// local/private variables go here
private val manager by lazy { app.attendanceManager }
private val manager
get() = app.attendanceManager
private var expandSubjectId = 0L
private var attendance = listOf<AttendanceFull>()

View File

@ -40,7 +40,8 @@ class GradesAdapter(
}
private val app = activity.applicationContext as App
private val manager = app.gradesManager
private val manager
get() = app.gradesManager
private val job = Job()
override val coroutineContext: CoroutineContext

View File

@ -48,9 +48,12 @@ class GradesListFragment : Fragment(), CoroutineScope {
get() = job + Dispatchers.Main
// local/private variables go here
private val manager by lazy { app.gradesManager }
private val dontCountEnabled by lazy { manager.dontCountEnabled }
private val dontCountGrades by lazy { manager.dontCountGrades }
private val manager
get() = app.gradesManager
private val dontCountEnabled
get() = manager.dontCountEnabled
private val dontCountGrades
get() = manager.dontCountGrades
private var expandSubjectId = 0L
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

View File

@ -54,7 +54,8 @@ class TimetableDayFragment : LazyFragment(), CoroutineScope {
private var endHour = DEFAULT_END_HOUR
private var firstEventMinute = 24 * 60
private val manager by lazy { app.timetableManager }
private val manager
get() = app.timetableManager
// find SwipeRefreshLayout in the hierarchy
private val refreshLayout by lazy { view?.findParentById(R.id.refreshLayout) }

View File

@ -4,12 +4,17 @@
package pl.szczodrzynski.edziennik.utils.managers
import androidx.core.view.isVisible
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeDp
import com.mikepenz.iconics.view.IconicsTextView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.db.full.EventFull
import pl.szczodrzynski.edziennik.startCoroutineTimer
import kotlin.coroutines.CoroutineContext
class EventManager(val app: App) : CoroutineScope {
@ -32,4 +37,41 @@ class EventManager(val app: App) : CoroutineScope {
app.db.metadataDao().setSeen(event.profileId, event, true)
}
}
fun setEventTopic(
title: IconicsTextView,
event: EventFull,
showType: Boolean = true,
doneIconColor: Int? = null
) {
var eventTopic = if (showType)
"${event.typeName ?: "wydarzenie"} - ${event.topic}"
else
event.topic
if (event.addedManually) {
eventTopic = "{cmd-clipboard-edit-outline} $eventTopic"
}
title.text = eventTopic
title.setCompoundDrawables(
null,
null,
if (event.isDone) IconicsDrawable(title.context).apply {
icon = CommunityMaterial.Icon.cmd_check
colorInt = doneIconColor ?: R.color.md_green_500.resolveColor(title.context)
sizeDp = 24
} else null,
null
)
}
fun setLegendText(legend: IconicsTextView, event: EventFull) {
legend.text = listOfNotNull(
if (event.addedManually) R.string.legend_event_added_manually else null,
if (event.isDone) R.string.legend_event_is_done else null
).map { legend.context.getString(it) }.join("\n")
legend.isVisible = legend.text.isNotBlank()
}
}

View File

@ -4,8 +4,7 @@
-->
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
@ -61,12 +60,10 @@
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
<com.mikepenz.iconics.view.IconicsTextView
android:id="@+id/topic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="3"
@ -79,23 +76,13 @@
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:fontFamily="@font/community_material_font_v5_8_55"
android:minWidth="0dp"
android:text="\uf2f4"
android:textSize="20sp"
tools:visibility="gone" />
<com.mikepenz.iconics.view.IconicsImageView
android:id="@+id/isDone"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="top"
android:layout_marginHorizontal="4dp"
android:visibility="gone"
app:iiv_color="@color/md_green_500"
app:iiv_icon="cmd-check"
tools:background="@sample/check" />
tools:visibility="visible" />
</LinearLayout>
<com.mikepenz.iconics.view.IconicsTextView