diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/ProfileDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/ProfileDao.kt index 497c4ce3..9f7eb5d8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/ProfileDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/ProfileDao.kt @@ -40,8 +40,8 @@ interface ProfileDao { @Query("SELECT profileId FROM profiles WHERE loginStoreId = :loginStoreId ORDER BY profileId") fun getIdsByLoginStoreIdNow(loginStoreId: Int): List - @get:Query("SELECT * FROM profiles WHERE syncEnabled = 1 AND archived = 0 AND profileId >= 0 ORDER BY profileId") - val profilesForSyncNow: List + @get:Query("SELECT * FROM profiles WHERE archived = 0 AND profileId >= 0 ORDER BY profileId") + val profilesForFirebaseNow: List @get:Query("SELECT profileId FROM profiles WHERE syncEnabled = 1 AND archived = 0 AND profileId >= 0 ORDER BY profileId") val idsForSyncNow: List diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseService.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseService.kt index 8099da42..00d33812 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseService.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseService.kt @@ -36,7 +36,7 @@ class MyFirebaseService : FirebaseService(), CoroutineScope { putString(System.currentTimeMillis().toString(), message.toString()) apply() } - val profiles = app.db.profileDao().profilesForSyncNow + val profiles = app.db.profileDao().profilesForFirebaseNow when (message.from) { "640759989760" -> SzkolnyAppFirebase(app, profiles, message) "747285019373" -> SzkolnyMobidziennikFirebase(app, profiles, message) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt index 39b5396e..78d03873 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt @@ -101,7 +101,9 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: val notificationList = mutableListOf() teams.filter { it.code == teamCode }.distinctBy { it.profileId }.forEach { team -> - val profile = profiles.firstOrNull { it.id == team.profileId } + val profile = profiles.firstOrNull { it.id == team.profileId } ?: return@forEach + if (profile.registration != Profile.REGISTRATION_ENABLED) + return@forEach val event = Event( team.profileId, json.getLong("id") ?: return, @@ -116,12 +118,9 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: team.id ) - // TODO? i guess - this comment is here for like a year - //val oldEvent: Event? = app.db.eventDao().getByIdNow(profile?.id ?: -1, event.id) - event.sharedBy = json.getString("sharedBy") event.sharedByName = json.getString("sharedByName") - if (profile?.userCode == event.sharedBy) event.sharedBy = "self" + if (profile.userCode == event.sharedBy) event.sharedBy = "self" val metadata = Metadata( event.profileId, @@ -132,18 +131,6 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: json.getLong("addedDate") ?: System.currentTimeMillis() ) - //val eventType = eventTypes.firstOrNull { it.profileId == profile?.id && it.id == event.type } - - /*val text = app.getString( - if (oldEvent == null) - R.string.notification_shared_event_format - else - R.string.notification_shared_event_modified_format, - event.sharedByName, - eventType?.name ?: "wydarzenie", - event.eventDate.formattedString, - event.topic - )*/ val type = if (event.type == Event.TYPE_HOMEWORK) Notification.TYPE_NEW_SHARED_HOMEWORK else Notification.TYPE_NEW_SHARED_EVENT val notificationFilter = app.config.getFor(event.profileId).sync.notificationFilter @@ -153,8 +140,8 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: title = app.getNotificationTitle(type), text = message, type = type, - profileId = profile?.id, - profileName = profile?.name, + profileId = profile.id, + profileName = profile.name, viewId = if (event.type == Event.TYPE_HOMEWORK) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA, addedDate = metadata.addedDate ).addExtra("eventId", event.id).addExtra("eventDate", event.eventDate.value.toLong()) @@ -177,18 +164,20 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: val notificationList = mutableListOf() teams.filter { it.code == teamCode }.distinctBy { it.profileId }.forEach { team -> - val profile = profiles.firstOrNull { it.id == team.profileId } + val profile = profiles.firstOrNull { it.id == team.profileId } ?: return@forEach + if (profile.registration != Profile.REGISTRATION_ENABLED) + return@forEach val notificationFilter = app.config.getFor(team.profileId).sync.notificationFilter if (!notificationFilter.contains(Notification.TYPE_REMOVED_SHARED_EVENT)) { val notification = Notification( - id = Notification.buildId(profile?.id + id = Notification.buildId(profile.id ?: 0, Notification.TYPE_REMOVED_SHARED_EVENT, eventId), title = app.getNotificationTitle(Notification.TYPE_REMOVED_SHARED_EVENT), text = message, type = Notification.TYPE_REMOVED_SHARED_EVENT, - profileId = profile?.id, - profileName = profile?.name, + profileId = profile.id, + profileName = profile.name, viewId = MainActivity.DRAWER_ITEM_AGENDA ) notificationList += notification diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt index c6b95cb8..ae21b2eb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt @@ -29,9 +29,11 @@ import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi import pl.szczodrzynski.edziennik.data.db.entity.Event import pl.szczodrzynski.edziennik.data.db.entity.EventType import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.full.EventFull import pl.szczodrzynski.edziennik.data.db.full.LessonFull import pl.szczodrzynski.edziennik.databinding.DialogEventManualV2Binding +import pl.szczodrzynski.edziennik.ui.dialogs.sync.RegistrationEnableDialog import pl.szczodrzynski.edziennik.ui.modules.views.TimeDropdown.Companion.DISPLAY_LESSONS import pl.szczodrzynski.edziennik.utils.Anim import pl.szczodrzynski.edziennik.utils.TextInputDropDown @@ -62,6 +64,7 @@ class EventManualDialog( private val app by lazy { activity.application as App } private lateinit var b: DialogEventManualV2Binding private lateinit var dialog: AlertDialog + private var profile: Profile? = null private var customColor: Int? = null private val editingShared = editingEvent?.sharedBy != null @@ -234,6 +237,8 @@ class EventManualDialog( } private fun loadLists() { launch { + profile = withContext(Dispatchers.Default) { app.db.profileDao().getByIdNow(profileId) } + with (b.dateDropdown) { db = app.db profileId = App.profileId @@ -407,6 +412,15 @@ class EventManualDialog( val share = b.shareSwitch.isChecked + if (share && profile?.registration != Profile.REGISTRATION_ENABLED) { + RegistrationEnableDialog(activity, profileId).showEventShareDialog { + if (it != null) + profile = it + saveEvent() + } + return + } + b.dateDropdown.error = null b.teamDropdown.error = null b.typeDropdown.error = null diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/sync/RegistrationEnableDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/sync/RegistrationEnableDialog.kt new file mode 100644 index 00000000..7fc0caf3 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/sync/RegistrationEnableDialog.kt @@ -0,0 +1,89 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2020-3-15. + */ + +package pl.szczodrzynski.edziennik.ui.dialogs.sync + +import android.text.Html +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.* +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi +import pl.szczodrzynski.edziennik.data.api.task.AppSync +import pl.szczodrzynski.edziennik.data.db.entity.Profile +import kotlin.coroutines.CoroutineContext + +class RegistrationEnableDialog( + val activity: AppCompatActivity, + val profileId: Int +) : CoroutineScope { + companion object { + private const val TAG = "RegistrationEnableDialog" + } + + private lateinit var app: App + + private val job = Job() + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main + + // local variables go here + private var progressDialog: AlertDialog? = null + + init { run { + if (activity.isFinishing) + return@run + app = activity.applicationContext as App + }} + + fun showEventShareDialog(onSuccess: (profile: Profile?) -> Unit) { + MaterialAlertDialogBuilder(activity) + .setTitle(R.string.event_manual_need_registration_title) + .setMessage(R.string.event_manual_need_registration_text) + .setPositiveButton(R.string.ok) { dialog, which -> + enableRegistration(onSuccess) + } + .setNegativeButton(R.string.cancel, null) + .show() + } + + fun showEnableDialog(onSuccess: (profile: Profile?) -> Unit) { + MaterialAlertDialogBuilder(activity) + .setTitle(R.string.registration_enable_dialog_title) + .setMessage(Html.fromHtml(app.getString(R.string.registration_enable_dialog_text))) + .setPositiveButton(R.string.ok) { dialog, which -> + enableRegistration(onSuccess) + } + .setNegativeButton(R.string.cancel, null) + .show() + } + + private fun enableRegistration(onSuccess: (profile: Profile?) -> Unit) { launch { + progressDialog = MaterialAlertDialogBuilder(activity) + .setTitle(R.string.please_wait) + .setMessage(R.string.registration_enable_progress_text) + .setCancelable(false) + .show() + + val profile = withContext(Dispatchers.Default) { + val profile = app.db.profileDao().getByIdNow(profileId) ?: return@withContext null + profile.registration = Profile.REGISTRATION_ENABLED + + // force full registration of the user + App.config.getFor(profile.id).hash = "" + + AppSync(app, mutableListOf(), listOf(profile), SzkolnyApi(app)).run(0L, markAsSeen = true) + app.db.profileDao().add(profile) + if (profile.id == App.profileId) { + App.profile.registration = profile.registration + } + return@withContext profile + } + + progressDialog?.dismiss() + onSuccess(profile) + }} +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cd5e28c9..a94a6b86 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1270,4 +1270,9 @@ Wybierz godzinę Udostępnianie wydarzenia… Usuwanie udostępnionego wydarzenia… + Udostępnianie wydarzeń + Aby móc udostępnić wydarzenie, należy włączyć opcję rejestracji na serwerze. Pozwala to na tworzenie i odbieranie wydarzeń udostępnionych w Twojej klasie.\n\nPo kliknięciu OK zostanie ona automatycznie włączona.\n\nUpewnij się, że zapoznałeś się z warunkami Polityki prywatności i akceptujesz jej postanowienia. + Pobieranie udostępnionych wydarzeń... + Rejestracja na serwerze + Rejestracja jest automatyczna, jeśli ta opcja jest włączona. Pozwala na tworzenie i odbieranie wydarzeń udostępnionych innym uczniom z Twojej klasy. Dzięki temu, można dodawać do dziennika pozycje nie zapisane przez nauczyciela.\n\nUpewnij się, że zapoznałeś się z warunkami Polityki prywatności i akceptujesz jej postanowienia.