From 729cf6f08edec5e94987bdf3cf52ed0615d74f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 13 Nov 2019 09:12:56 +0100 Subject: [PATCH] [Settings] New Profile removal dialog. --- .../szczodrzynski/edziennik/MainActivity.kt | 3 +- .../dialogs/settings/ProfileRemoveDialog.kt | 103 ++++++++++++++++++ .../modules/settings/SettingsNewFragment.java | 3 +- app/src/main/res/values-en/strings.xml | 2 +- app/src/main/res/values/strings.xml | 3 +- 5 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/settings/ProfileRemoveDialog.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index e147b1a6..8fdc45c1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -45,6 +45,7 @@ import pl.szczodrzynski.edziennik.network.ServerRequest import pl.szczodrzynski.edziennik.sync.AppManagerDetectedEvent import pl.szczodrzynski.edziennik.sync.SyncWorker import pl.szczodrzynski.edziennik.ui.dialogs.changelog.ChangelogDialog +import pl.szczodrzynski.edziennik.ui.dialogs.settings.ProfileRemoveDialog import pl.szczodrzynski.edziennik.ui.modules.agenda.AgendaFragment import pl.szczodrzynski.edziennik.ui.modules.announcements.AnnouncementsFragment import pl.szczodrzynski.edziennik.ui.modules.attendance.AttendanceFragment @@ -1043,7 +1044,7 @@ class MainActivity : AppCompatActivity() { } loadTarget(DRAWER_ITEM_SETTINGS, null) } else if (item.itemId == 2) { - app.apiEdziennik.guiRemoveProfile(this@MainActivity, profileId, profile.name?.getText(this).toString()) + ProfileRemoveDialog(this, profileId, profile.name?.getText(this)?.toString() ?: "?") } true } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/settings/ProfileRemoveDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/settings/ProfileRemoveDialog.kt new file mode 100644 index 00000000..fa626091 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/settings/ProfileRemoveDialog.kt @@ -0,0 +1,103 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-13. + */ + +package pl.szczodrzynski.edziennik.ui.dialogs.settings + +import android.widget.Toast +import androidx.appcompat.app.AlertDialog +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.* +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.MainActivity +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.databinding.DialogLessonDetailsBinding +import pl.szczodrzynski.edziennik.utils.models.Notification +import java.util.* +import kotlin.coroutines.CoroutineContext + +class ProfileRemoveDialog( + val activity: MainActivity, + val profileId: Int, + val profileName: String +) : CoroutineScope { + companion object { + private const val TAG = "ProfileRemoveDialog" + } + + private lateinit var job: Job + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main + + private val app by lazy { activity.application as App } + private lateinit var b: DialogLessonDetailsBinding + private lateinit var dialog: AlertDialog + + init { run { + job = Job() + + dialog = MaterialAlertDialogBuilder(activity) + .setTitle(R.string.profile_menu_remove_confirm) + .setMessage(activity.getString(R.string.profile_menu_remove_confirm_text_format, profileName, profileName)) + .setPositiveButton(R.string.remove) { _, _ -> + removeProfile() + } + .setNeutralButton(R.string.cancel) { dialog, _ -> dialog.dismiss() } + .setCancelable(false) + .show() + }} + + private fun removeProfile() { launch { + val deferred = async(Dispatchers.Default) { + val profileObject = app.db.profileDao().getByIdNow(profileId) ?: return@async + app.db.announcementDao().clear(profileId) + app.db.attendanceDao().clear(profileId) + app.db.eventDao().clear(profileId) + app.db.eventTypeDao().clear(profileId) + app.db.gradeDao().clear(profileId) + app.db.gradeCategoryDao().clear(profileId) + app.db.lessonDao().clear(profileId) + app.db.lessonChangeDao().clear(profileId) + app.db.luckyNumberDao().clear(profileId) + app.db.noticeDao().clear(profileId) + app.db.subjectDao().clear(profileId) + app.db.teacherDao().clear(profileId) + app.db.teamDao().clear(profileId) + app.db.messageRecipientDao().clear(profileId) + app.db.messageDao().clear(profileId) + app.db.endpointTimerDao().clear(profileId) + app.db.attendanceTypeDao().clear(profileId) + app.db.classroomDao().clear(profileId) + app.db.lessonRangeDao().clear(profileId) + app.db.noticeTypeDao().clear(profileId) + app.db.teacherAbsenceDao().clear(profileId) + app.db.teacherAbsenceTypeDao().clear(profileId) + app.db.timetableDao().clear(profileId) + + val loginStoreId = profileObject.loginStoreId + val profilesUsingLoginStore = app.db.profileDao().getIdsByLoginStoreIdNow(loginStoreId) + if (profilesUsingLoginStore.size == 1) { + app.db.loginStoreDao().remove(loginStoreId) + } + app.db.profileDao().remove(profileId) + app.db.metadataDao().deleteAll(profileId) + + val toRemove = ArrayList() + for (notification in app.appConfig.notifications) { + if (notification.profileId == profileId) { + toRemove.add(notification) + } + } + app.appConfig.notifications.removeAll(toRemove) + + app.profile = null + App.profileId = -1 + + app.profileLoadById(app.profileLastId()) + } + deferred.await() + dialog.dismiss() + activity.reloadTarget() + Toast.makeText(activity, R.string.dialog_profile_remove_success, Toast.LENGTH_LONG).show() + }} +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java index a03e4633..ee9e307d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java @@ -50,6 +50,7 @@ import pl.szczodrzynski.edziennik.network.ServerRequest; import pl.szczodrzynski.edziennik.receivers.BootReceiver; import pl.szczodrzynski.edziennik.sync.SyncWorker; import pl.szczodrzynski.edziennik.ui.dialogs.changelog.ChangelogDialog; +import pl.szczodrzynski.edziennik.ui.dialogs.settings.ProfileRemoveDialog; import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragment; import pl.szczodrzynski.edziennik.ui.modules.webpush.WebPushConfigActivity; import pl.szczodrzynski.edziennik.utils.Themes; @@ -263,7 +264,7 @@ public class SettingsNewFragment extends MaterialAboutFragment { .color(IconicsColor.colorInt(iconColor)) ) .setOnClickAction(() -> { - app.apiEdziennik.guiRemoveProfile(activity, app.profile.getId(), app.profile.getName()); + new ProfileRemoveDialog(activity, app.profile.getId(), app.profile.getName()); }) ); diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index ad6f407b..f6ec7c2a 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -526,7 +526,7 @@ Settings Remove profile Are you sure? - You are trying to remove profile %s. This means removing this profile\'s data (grades, timetables, events…) from the app.\n\nYou\'ll be able to restore most of the data by logging in again.\n\nDo you really want to remove profile %s? + You are trying to remove profile %s. This means removing this profile\'s data (grades, timetables, events…) from the app.\n\nYou\'ll be able to restore most of the data by logging in again.\n\nDo you really want to remove profile %s? Never Later Rate now diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 41589ce0..2e711d11 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -572,7 +572,7 @@ Ustawienia Usuń profil Potwierdź usunięcie - Zamierzasz usunąć profil %s. Oznacza to usunięcie z aplikacji wszystkich danych powiązanych z tym profilem (ocen, planu lekcji, sprawdzianów…).\n\nWiększość danych będziesz mógł pobrać na nowo poprzez ponowne zalogowanie się.\n\nCzy rzeczywiście chcesz usunąć profil %s? + Zamierzasz usunąć profil %s. Oznacza to usunięcie z aplikacji wszystkich danych powiązanych z tym profilem (ocen, planu lekcji, sprawdzianów…).\n\nWiększość danych będziesz mógł pobrać na nowo poprzez ponowne zalogowanie się.\n\nCzy rzeczywiście chcesz usunąć profil %s? Nigdy Później Oceń teraz @@ -1022,4 +1022,5 @@ dzisiaj (%s) następny %s (%s) Nie ma lekcji tego dnia + Profil został usunięty.