[UI] Add setting to share events/notes by default.

This commit is contained in:
Kuba Szczodrzyński 2022-10-24 23:32:09 +02:00
parent 2c34924052
commit ffd81f8b82
No known key found for this signature in database
GPG Key ID: 70CB8A85BA1633CB
8 changed files with 90 additions and 7 deletions

View File

@ -462,6 +462,8 @@ class App : MultiDexApplication(), Configuration.Provider, CoroutineScope {
} }
fun profileSave() = profileSave(profile) fun profileSave() = profileSave(profile)
fun profileSave(profile: Profile) { fun profileSave(profile: Profile) {
if (profile.id == profileId)
App.profile = profile
launch(Dispatchers.Default) { launch(Dispatchers.Default) {
App.db.profileDao().add(profile) App.db.profileDao().add(profile)
} }

View File

@ -29,6 +29,8 @@ class ProfileConfig(
var dataVersion by config<Int>(DATA_VERSION) var dataVersion by config<Int>(DATA_VERSION)
var hash by config<String>("") var hash by config<String>("")
var shareByDefault by config<Boolean>(false)
init { init {
if (dataVersion < DATA_VERSION) if (dataVersion < DATA_VERSION)
ProfileConfigMigration(this) ProfileConfigMigration(this)

View File

@ -80,6 +80,8 @@ class EventManualDialog(
SzkolnyApi(app) SzkolnyApi(app)
} }
private val profileConfig by lazy { app.config.forProfile() }
private var enqueuedWeekDialog: AlertDialog? = null private var enqueuedWeekDialog: AlertDialog? = null
private var enqueuedWeekStart = Date.getToday() private var enqueuedWeekStart = Date.getToday()
@ -107,9 +109,6 @@ class EventManualDialog(
} }
override suspend fun onShow() { override suspend fun onShow() {
b.shareSwitch.isChecked = editingShared
b.shareSwitch.isEnabled = !editingShared || (editingShared && editingOwn)
b.showMore.onClick { // TODO iconics is broken b.showMore.onClick { // TODO iconics is broken
it.apply { it.apply {
refreshDrawableState() refreshDrawableState()
@ -137,6 +136,13 @@ class EventManualDialog(
} }
loadLists() loadLists()
val shareByDefault = profileConfig.shareByDefault
&& profile.enableSharedEvents
&& profile.registration == Profile.REGISTRATION_ENABLED
b.shareSwitch.isChecked = editingShared || editingEvent == null && shareByDefault
b.shareSwitch.isEnabled = !editingShared || editingOwn
} }
private fun updateShareText(checked: Boolean = b.shareSwitch.isChecked) { private fun updateShareText(checked: Boolean = b.shareSwitch.isChecked) {
@ -411,6 +417,20 @@ class EventManualDialog(
return return
} }
if (share && !profile.enableSharedEvents) {
MaterialAlertDialogBuilder(activity)
.setTitle(R.string.event_sharing)
.setMessage(R.string.settings_register_shared_events_dialog_enabled_text)
.setPositiveButton(R.string.ok) { _, _ ->
profile.enableSharedEvents = true
app.profileSave(profile)
saveEvent()
}
.setNegativeButton(R.string.cancel, null)
.show()
return
}
b.dateDropdown.error = null b.dateDropdown.error = null
b.teamDropdown.error = null b.teamDropdown.error = null
b.typeDropdown.error = null b.typeDropdown.error = null

View File

@ -13,6 +13,7 @@ import com.mikepenz.iconics.typeface.library.community.material.CommunityMateria
import com.mikepenz.iconics.utils.colorInt import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeDp import com.mikepenz.iconics.utils.sizeDp
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.db.entity.Note import pl.szczodrzynski.edziennik.data.db.entity.Note
@ -59,6 +60,8 @@ class NoteEditorDialog(
private val textStylingManager private val textStylingManager
get() = app.textStylingManager get() = app.textStylingManager
private val profileConfig by lazy { app.config.forProfile() }
private var progressDialog: AlertDialog? = null private var progressDialog: AlertDialog? = null
override suspend fun onPositiveClick(): Boolean { override suspend fun onPositiveClick(): Boolean {
@ -76,6 +79,23 @@ class NoteEditorDialog(
return NO_DISMISS return NO_DISMISS
} }
if (note.isShared && !profile.enableSharedEvents) {
MaterialAlertDialogBuilder(activity)
.setTitle(R.string.event_sharing)
.setMessage(R.string.settings_register_shared_events_dialog_enabled_text)
.setPositiveButton(R.string.ok) { _, _ ->
profile.enableSharedEvents = true
app.profileSave(profile)
launch {
if (onPositiveClick())
dismiss()
}
}
.setNegativeButton(R.string.cancel, null)
.show()
return NO_DISMISS
}
if (note.isShared || editingNote?.isShared == true) { if (note.isShared || editingNote?.isShared == true) {
progressDialog = MaterialAlertDialogBuilder(activity) progressDialog = MaterialAlertDialogBuilder(activity)
.setTitle(R.string.please_wait) .setTitle(R.string.please_wait)
@ -127,8 +147,15 @@ class NoteEditorDialog(
topicStylingConfig = StylingConfigBase(editText = b.topic, htmlMode = HtmlMode.SIMPLE) topicStylingConfig = StylingConfigBase(editText = b.topic, htmlMode = HtmlMode.SIMPLE)
bodyStylingConfig = StylingConfigBase(editText = b.body, htmlMode = HtmlMode.SIMPLE) bodyStylingConfig = StylingConfigBase(editText = b.body, htmlMode = HtmlMode.SIMPLE)
val profile = withContext(Dispatchers.IO) {
app.db.profileDao().getByIdNow(profileId)
}
b.ownerType = owner?.getNoteType() ?: Note.OwnerType.NONE b.ownerType = owner?.getNoteType() ?: Note.OwnerType.NONE
b.editingNote = editingNote b.editingNote = editingNote
b.shareByDefault = profileConfig.shareByDefault
&& profile?.enableSharedEvents == true
&& profile.registration == Profile.REGISTRATION_ENABLED
b.color.clear().append(Note.Color.values().map { color -> b.color.clear().append(Note.Color.values().map { color ->
TextInputDropDown.Item( TextInputDropDown.Item(

View File

@ -39,7 +39,7 @@ class SettingsRegisterCard(util: SettingsUtil) : SettingsCard(util) {
subText = R.string.settings_register_shared_events_subtext, subText = R.string.settings_register_shared_events_subtext,
icon = CommunityMaterial.Icon3.cmd_share_outline, icon = CommunityMaterial.Icon3.cmd_share_outline,
value = app.profile.enableSharedEvents value = app.profile.enableSharedEvents
) { _, value -> ) { item, value ->
app.profile.enableSharedEvents = value app.profile.enableSharedEvents = value
app.profileSave() app.profileSave()
MaterialAlertDialogBuilder(activity) MaterialAlertDialogBuilder(activity)
@ -52,6 +52,23 @@ class SettingsRegisterCard(util: SettingsUtil) : SettingsCard(util) {
) )
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.show() .show()
if (value) {
card.items.after(item, sharedEventsDefaultItem)
} else {
card.items.remove(sharedEventsDefaultItem)
}
util.refresh()
}
}
private val sharedEventsDefaultItem by lazy {
util.createPropertyItem(
text = R.string.settings_register_share_by_default_text,
subText = R.string.settings_register_share_by_default_subtext,
icon = CommunityMaterial.Icon3.cmd_toggle_switch_outline,
value = configProfile.shareByDefault
) { _, value ->
configProfile.shareByDefault = value
} }
} }
@ -112,8 +129,11 @@ class SettingsRegisterCard(util: SettingsUtil) : SettingsCard(util) {
item.isChecked = enabled item.isChecked = enabled
if (value) { if (value) {
card.items.after(item, sharedEventsItem) card.items.after(item, sharedEventsItem)
if (app.profile.enableSharedEvents)
card.items.after(item, sharedEventsDefaultItem)
} else { } else {
card.items.remove(sharedEventsItem) card.items.remove(sharedEventsItem)
card.items.remove(sharedEventsDefaultItem)
} }
util.refresh() util.refresh()
}) })
@ -127,6 +147,11 @@ class SettingsRegisterCard(util: SettingsUtil) : SettingsCard(util) {
if (app.profile.canShare) if (app.profile.canShare)
sharedEventsItem sharedEventsItem
else
null,
if (app.profile.enableSharedEvents)
sharedEventsDefaultItem
else else
null null
) )

View File

@ -18,6 +18,10 @@
<variable <variable
name="editingNote" name="editingNote"
type="Note" /> type="Note" />
<variable
name="shareByDefault"
type="boolean" />
</data> </data>
<androidx.core.widget.NestedScrollView <androidx.core.widget.NestedScrollView
@ -89,7 +93,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:checked="@{editingNote.sharedBy != null}" android:checked="@{editingNote.sharedBy != null || editingNote == null &amp;&amp; shareByDefault}"
android:isVisible="@{ownerType.shareable}" android:isVisible="@{ownerType.shareable}"
android:minHeight="32dp" android:minHeight="32dp"
android:text="@string/dialog_event_manual_share_enabled" /> android:text="@string/dialog_event_manual_share_enabled" />

View File

@ -83,8 +83,9 @@
}, },
"university": { "university": {
"configOverrides": { "configOverrides": {
"timetableTrimHourRange": true, "shareByDefault": true,
"timetableColorSubjectName": true "timetableColorSubjectName": true,
"timetableTrimHourRange": true
}, },
"uiConfig": { "uiConfig": {
"lessonHeight": 45 "lessonHeight": 45

View File

@ -1551,4 +1551,6 @@
<string name="app_cannot_load_data">Nie można załadować danych aplikacji</string> <string name="app_cannot_load_data">Nie można załadować danych aplikacji</string>
<string name="legend_event_shared_received">{cmd-share-variant} udostępnione w klasie</string> <string name="legend_event_shared_received">{cmd-share-variant} udostępnione w klasie</string>
<string name="legend_event_shared_sent">{cmd-share-variant} udostępnione przez Ciebie</string> <string name="legend_event_shared_sent">{cmd-share-variant} udostępnione przez Ciebie</string>
<string name="settings_register_share_by_default_text">Domyślnie udostępniaj wydarzenia</string>
<string name="settings_register_share_by_default_subtext">Ustaw tworzone wydarzenia domyślnie jako udostępnione</string>
</resources> </resources>