Skip to content

Commit

Permalink
[App] Refactor getting profile config.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Oct 25, 2022
1 parent 00e077d commit bfd2e98
Show file tree
Hide file tree
Showing 30 changed files with 181 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ class Config(db: AppDb) : BaseConfig(db) {
ConfigMigration(app, this)
}

fun getFor(profileId: Int): ProfileConfig {
operator fun get(profileId: Int): ProfileConfig {
return profileConfigs[profileId] ?: ProfileConfig(db, profileId, entries).also {
profileConfigs[profileId] = it
}
}

fun forProfile() = getFor(App.profileId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ class SzkolnyApi(val app: App) : CoroutineScope {
val teams = app.db.teamDao().allNow

val users = profiles.mapNotNull { profile ->
val config = app.config.getFor(profile.id)
val user = ServerSyncRequest.User(
profile.userCode,
profile.studentNameLong,
Expand All @@ -207,9 +206,9 @@ class SzkolnyApi(val app: App) : CoroutineScope {
teams.filter { it.profileId == profile.id }.map { it.code }
)
val hash = user.toString().md5()
if (hash == config.hash)
if (hash == profile.config.hash)
return@mapNotNull null
return@mapNotNull user to config
return@mapNotNull user to profile.config
}

val response = api.serverSync(ServerSyncRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SzkolnyTask(val app: App, val syncingProfiles: List<Profile>) : IApiTask(-
notificationList
.mapNotNull { it.profileId }
.distinct()
.map { app.config.getFor(it).sync.notificationFilter }
.map { app.config[it].sync.notificationFilter }
.forEach { filter ->
filter.forEach { type ->
notificationList.removeAll { it.type == type }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import android.content.Context
import android.widget.ImageView
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import com.google.gson.JsonObject
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.data.db.enums.LoginType
import pl.szczodrzynski.edziennik.ext.dateToSemester
import pl.szczodrzynski.edziennik.ext.getDrawable
Expand Down Expand Up @@ -88,6 +90,9 @@ open class Profile(
val canShare
get() = registration == REGISTRATION_ENABLED && !archived

@delegate:Ignore
val config by lazy { App.config[this.id] }

override fun getImageDrawable(context: Context) = this.getDrawable(context)
override fun getImageHolder(context: Context) = this.getHolder()
override fun applyImageTo(imageView: ImageView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
)

val type = if (event.isHomework) NotificationType.SHARED_HOMEWORK else NotificationType.SHARED_EVENT
val notificationFilter = app.config.getFor(event.profileId).sync.notificationFilter
val notificationFilter = app.config[event.profileId].sync.notificationFilter

if (!notificationFilter.contains(type) && event.sharedBy != "self" && event.date >= Date.getToday()) {
val notification = Notification(
Expand Down Expand Up @@ -211,7 +211,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
val profile = profiles.firstOrNull { it.id == team.profileId } ?: return@forEach
if (!profile.canShare)
return@forEach
val notificationFilter = app.config.getFor(team.profileId).sync.notificationFilter
val notificationFilter = app.config[team.profileId].sync.notificationFilter

if (!notificationFilter.contains(NotificationType.REMOVED_SHARED_EVENT)) {
val notification = Notification(
Expand Down Expand Up @@ -265,7 +265,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
return@forEach

val type = NotificationType.SHARED_NOTE
val notificationFilter = app.config.getFor(note.profileId).sync.notificationFilter
val notificationFilter = app.config[note.profileId].sync.notificationFilter

if (!notificationFilter.contains(type) && note.sharedBy != "self") {
val notification = Notification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import com.mikepenz.iconics.typeface.library.community.material.CommunityMateria
import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeDp
import eu.szkolny.font.SzkolnyFont
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.MainActivity
import pl.szczodrzynski.edziennik.R
Expand Down Expand Up @@ -53,7 +58,7 @@ class AgendaFragment : Fragment(), CoroutineScope {
if (getActivity() == null || context == null) return null
activity = getActivity() as MainActivity
context?.theme?.applyStyle(Themes.appTheme, true)
type = app.config.forProfile().ui.agendaViewType
type = app.profile.config.ui.agendaViewType
b = when (type) {
Profile.AGENDA_DEFAULT -> FragmentAgendaDefaultBinding.inflate(inflater, container, false)
Profile.AGENDA_CALENDAR -> FragmentAgendaCalendarBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -92,7 +97,7 @@ class AgendaFragment : Fragment(), CoroutineScope {
activity.bottomSheet.close()
type =
if (type == Profile.AGENDA_DEFAULT) Profile.AGENDA_CALENDAR else Profile.AGENDA_DEFAULT
app.config.forProfile().ui.agendaViewType = type
app.profile.config.ui.agendaViewType = type
activity.reloadTarget()
},
BottomSheetSeparatorItem(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import com.github.tibolte.agendacalendarview.agenda.AgendaAdapter
import com.github.tibolte.agendacalendarview.models.BaseCalendarEvent
import com.github.tibolte.agendacalendarview.models.CalendarEvent
import com.github.tibolte.agendacalendarview.models.IDayItem
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.MainActivity
import pl.szczodrzynski.edziennik.data.db.full.EventFull
Expand Down Expand Up @@ -49,7 +54,6 @@ class AgendaFragmentDefault(
private val unreadDates = mutableSetOf<Int>()
private val events = mutableListOf<CalendarEvent>()
private var isInitialized = false
private val profileConfig by lazy { app.config.forProfile().ui }

private val listView
get() = b.agendaDefaultView.agendaView.agendaListView
Expand Down Expand Up @@ -107,10 +111,10 @@ class AgendaFragmentDefault(
isInitialized = false

withContext(Dispatchers.Default) {
if (profileConfig.agendaLessonChanges)
if (app.profile.config.ui.agendaLessonChanges)
addLessonChanges(events)

if (profileConfig.agendaTeacherAbsence)
if (app.profile.config.ui.agendaTeacherAbsence)
addTeacherAbsence(events)
}

Expand All @@ -127,7 +131,7 @@ class AgendaFragmentDefault(
val dateStart = app.profile.dateSemester1Start.asCalendar
val dateEnd = app.profile.dateYearEnd.asCalendar

val isCompactMode = profileConfig.agendaCompactMode
val isCompactMode = app.profile.config.ui.agendaCompactMode

b.agendaDefaultView.init(
events,
Expand Down Expand Up @@ -247,7 +251,7 @@ class AgendaFragmentDefault(
) {
events.removeAll { it is AgendaEvent || it is AgendaEventGroup }

if (!profileConfig.agendaGroupByType) {
if (!app.profile.config.ui.agendaGroupByType) {
events += eventList.map {
if (!it.seen)
unreadDates.add(it.date.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import kotlinx.coroutines.Job
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.MainActivity
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.db.entity.Metadata
import pl.szczodrzynski.edziennik.data.db.enums.MetadataType
import pl.szczodrzynski.edziennik.databinding.AttendanceFragmentBinding
import pl.szczodrzynski.edziennik.ext.Bundle
Expand Down Expand Up @@ -83,7 +82,7 @@ class AttendanceFragment : Fragment(), CoroutineScope {
activity.gainAttention()

if (pageSelection == 1)
pageSelection = app.config.forProfile().attendance.attendancePageSelection
pageSelection = app.profile.config.attendance.attendancePageSelection

val pagerAdapter = FragmentLazyPagerAdapter(
parentFragmentManager,
Expand Down Expand Up @@ -114,7 +113,7 @@ class AttendanceFragment : Fragment(), CoroutineScope {
currentItem = pageSelection
addOnPageSelectedListener {
pageSelection = it
app.config.forProfile().attendance.attendancePageSelection = it
app.profile.config.attendance.attendancePageSelection = it
}
b.tabLayout.setupWithViewPager(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ class AttendanceListFragment : LazyFragment(), CoroutineScope {
if (attendance.isEmpty())
return mutableListOf()

val groupConsecutiveDays = app.config.forProfile().attendance.groupConsecutiveDays
val showPresenceInMonth = app.config.forProfile().attendance.showPresenceInMonth
val groupConsecutiveDays = app.profile.config.attendance.groupConsecutiveDays
val showPresenceInMonth = app.profile.config.attendance.showPresenceInMonth

if (viewType == AttendanceFragment.VIEW_DAYS) {
val items = attendance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.gson.*
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.google.gson.JsonPrimitive
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.MainActivity
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.api.models.ApiError
import pl.szczodrzynski.edziennik.databinding.TemplateListPageFragmentBinding
import pl.szczodrzynski.edziennik.ext.*
Expand Down Expand Up @@ -66,7 +71,7 @@ class LabProfileFragment : LazyFragment(), CoroutineScope {
"LoginStore" -> loginStore
"LoginStore / data" -> loginStore?.data ?: JsonObject()
"Config" -> app.config.values
"Config (profile)" -> app.config.forProfile().values
"Config (profile)" -> app.profile.config.values
else -> when (obj) {
is JsonObject -> (obj as JsonObject).get(el)
is JsonArray -> (obj as JsonArray).get(el.toInt())
Expand Down Expand Up @@ -176,7 +181,7 @@ class LabProfileFragment : LazyFragment(), CoroutineScope {
json.add("LoginStore", app.gson.toJsonTree(loginStore))
json.add("LoginStore / data", loginStore?.data ?: JsonObject())
json.add("Config", JsonParser.parseString(app.gson.toJson(app.config.values.toSortedMap())))
json.add("Config (profile)", JsonParser.parseString(app.gson.toJson(app.config.forProfile().values.toSortedMap())))
json.add("Config (profile)", JsonParser.parseString(app.gson.toJson(app.profile.config.values.toSortedMap())))
}
adapter.items = LabJsonAdapter.expand(json, 0)
adapter.notifyDataSetChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ package pl.szczodrzynski.edziennik.ui.dialogs.settings

import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.db.entity.Profile
import pl.szczodrzynski.edziennik.databinding.DialogConfigAgendaBinding
import pl.szczodrzynski.edziennik.ext.onChange
import pl.szczodrzynski.edziennik.ui.dialogs.base.ConfigDialog
import java.util.*

class AgendaConfigDialog(
activity: AppCompatActivity,
Expand All @@ -32,11 +30,9 @@ class AgendaConfigDialog(
override fun inflate(layoutInflater: LayoutInflater) =
DialogConfigAgendaBinding.inflate(layoutInflater)

private val profileConfig by lazy { app.config.forProfile() }

override suspend fun loadConfig() {
b.config = profileConfig
b.isAgendaMode = profileConfig.ui.agendaViewType == Profile.AGENDA_DEFAULT
b.config = app.profile.config
b.isAgendaMode = app.profile.config.ui.agendaViewType == Profile.AGENDA_DEFAULT

var calledFromListener = false
b.eventSharingEnabled.isChecked = app.profile.canShare
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,21 @@ class AttendanceConfigDialog(
override fun inflate(layoutInflater: LayoutInflater) =
AttendanceConfigDialogBinding.inflate(layoutInflater)

private val profileConfig by lazy { app.config.getFor(app.profileId).attendance }

override suspend fun loadConfig() {
b.useSymbols.isChecked = profileConfig.useSymbols
b.groupConsecutiveDays.isChecked = profileConfig.groupConsecutiveDays
b.showPresenceInMonth.isChecked = profileConfig.showPresenceInMonth
b.useSymbols.isChecked = app.profile.config.attendance.useSymbols
b.groupConsecutiveDays.isChecked = app.profile.config.attendance.groupConsecutiveDays
b.showPresenceInMonth.isChecked = app.profile.config.attendance.showPresenceInMonth
}

override fun initView() {
b.useSymbols.onChange { _, isChecked ->
profileConfig.useSymbols = isChecked
app.profile.config.attendance.useSymbols = isChecked
}
b.groupConsecutiveDays.onChange { _, isChecked ->
profileConfig.groupConsecutiveDays = isChecked
app.profile.config.attendance.groupConsecutiveDays = isChecked
}
b.showPresenceInMonth.onChange { _, isChecked ->
profileConfig.showPresenceInMonth = isChecked
app.profile.config.attendance.showPresenceInMonth = isChecked
}
}
}
Loading

0 comments on commit bfd2e98

Please sign in to comment.