forked from github/szkolny
[Settings] New Profile removal dialog.
This commit is contained in:
parent
860a16b32d
commit
729cf6f08e
@ -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
|
||||
}
|
||||
|
@ -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<Notification>()
|
||||
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()
|
||||
}}
|
||||
}
|
@ -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());
|
||||
})
|
||||
);
|
||||
|
||||
|
@ -526,7 +526,7 @@
|
||||
<string name="profile_menu_open_settings">Settings</string>
|
||||
<string name="profile_menu_remove">Remove profile</string>
|
||||
<string name="profile_menu_remove_confirm">Are you sure?</string>
|
||||
<string name="profile_menu_remove_confirm_text_format">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?</string>
|
||||
<string name="profile_menu_remove_confirm_text_format">You are trying to remove profile <b>%s</b>. 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?</string>
|
||||
<string name="rate_snackbar_negative">Never</string>
|
||||
<string name="rate_snackbar_neutral">Later</string>
|
||||
<string name="rate_snackbar_positive">Rate now</string>
|
||||
|
@ -572,7 +572,7 @@
|
||||
<string name="profile_menu_open_settings">Ustawienia</string>
|
||||
<string name="profile_menu_remove">Usuń profil</string>
|
||||
<string name="profile_menu_remove_confirm">Potwierdź usunięcie</string>
|
||||
<string name="profile_menu_remove_confirm_text_format">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?</string>
|
||||
<string name="profile_menu_remove_confirm_text_format">Zamierzasz usunąć profil <b>%s</b>. 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?</string>
|
||||
<string name="rate_snackbar_negative">Nigdy</string>
|
||||
<string name="rate_snackbar_neutral">Później</string>
|
||||
<string name="rate_snackbar_positive">Oceń teraz</string>
|
||||
@ -1022,4 +1022,5 @@
|
||||
<string name="dialog_event_manual_date_today">dzisiaj (%s)</string>
|
||||
<string name="dialog_event_manual_date_next_week">następny %s (%s)</string>
|
||||
<string name="dialog_event_manual_no_lessons">Nie ma lekcji tego dnia</string>
|
||||
<string name="dialog_profile_remove_success">Profil został usunięty.</string>
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user