move NotificationTypeConverter into Converters (#4908)
There is no reason why we would treat this one differently. Ok, it might be a bit less efficient but I take that in exchange for the cleaner code.
This commit is contained in:
parent
8928cf617d
commit
b3d5aff435
5 changed files with 35 additions and 58 deletions
|
|
@ -48,8 +48,6 @@ import com.keylesspalace.tusky.settings.PrefKeys
|
|||
import com.keylesspalace.tusky.usecase.NotificationPolicyState
|
||||
import com.keylesspalace.tusky.usecase.NotificationPolicyUsecase
|
||||
import com.keylesspalace.tusky.usecase.TimelineCases
|
||||
import com.keylesspalace.tusky.util.deserialize
|
||||
import com.keylesspalace.tusky.util.serialize
|
||||
import com.keylesspalace.tusky.viewdata.NotificationViewData
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData
|
||||
import com.keylesspalace.tusky.viewdata.TranslationViewData
|
||||
|
|
@ -87,8 +85,8 @@ class NotificationsViewModel @Inject constructor(
|
|||
private val refreshTrigger = MutableStateFlow(0L)
|
||||
|
||||
val excludes: StateFlow<Set<Notification.Type>> = activeAccountFlow
|
||||
.map { account -> deserialize(account?.notificationsFilter ?: "[]") }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, deserialize(activeAccountFlow.value?.notificationsFilter ?: "[]"))
|
||||
.map { account -> account?.notificationsFilter.orEmpty() }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, activeAccountFlow.value?.notificationsFilter.orEmpty())
|
||||
|
||||
/** Map from notification id to translation. */
|
||||
private val translations = MutableStateFlow(mapOf<String, TranslationViewData>())
|
||||
|
|
@ -155,7 +153,7 @@ class NotificationsViewModel @Inject constructor(
|
|||
if (newFilters != excludes.value && account != null) {
|
||||
viewModelScope.launch {
|
||||
accountManager.updateAccount(account) {
|
||||
copy(notificationsFilter = serialize(newFilters))
|
||||
copy(notificationsFilter = newFilters)
|
||||
}
|
||||
db.notificationsDao().cleanupNotifications(accountId, 0)
|
||||
refreshTrigger.value++
|
||||
|
|
|
|||
|
|
@ -32,9 +32,7 @@ import com.keylesspalace.tusky.settings.preferenceCategory
|
|||
import com.keylesspalace.tusky.settings.sliderPreference
|
||||
import com.keylesspalace.tusky.settings.switchPreference
|
||||
import com.keylesspalace.tusky.util.LocaleManager
|
||||
import com.keylesspalace.tusky.util.deserialize
|
||||
import com.keylesspalace.tusky.util.icon
|
||||
import com.keylesspalace.tusky.util.serialize
|
||||
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import de.c1710.filemojicompat_ui.views.picker.preference.EmojiPickerPreference
|
||||
|
|
@ -245,9 +243,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {
|
|||
key = PrefKeys.WELLBEING_LIMITED_NOTIFICATIONS
|
||||
setOnPreferenceChangeListener { _, value ->
|
||||
for (account in accountManager.accounts) {
|
||||
val notificationFilter = deserialize(
|
||||
account.notificationsFilter
|
||||
).toMutableSet()
|
||||
val notificationFilter = account.notificationsFilter.toMutableSet()
|
||||
|
||||
if (value == true) {
|
||||
notificationFilter.add(Notification.Type.FAVOURITE)
|
||||
|
|
@ -260,7 +256,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {
|
|||
}
|
||||
|
||||
lifecycleScope.launch {
|
||||
accountManager.updateAccount(account) { copy(notificationsFilter = serialize(notificationFilter)) }
|
||||
accountManager.updateAccount(account) { copy(notificationsFilter = notificationFilter) }
|
||||
}
|
||||
}
|
||||
true
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import com.keylesspalace.tusky.entity.Emoji
|
|||
import com.keylesspalace.tusky.entity.FilterResult
|
||||
import com.keylesspalace.tusky.entity.HashTag
|
||||
import com.keylesspalace.tusky.entity.NewPoll
|
||||
import com.keylesspalace.tusky.entity.Notification
|
||||
import com.keylesspalace.tusky.entity.Poll
|
||||
import com.keylesspalace.tusky.entity.PreviewCard
|
||||
import com.keylesspalace.tusky.entity.Status
|
||||
|
|
@ -37,6 +38,8 @@ import java.net.URLEncoder
|
|||
import java.util.Date
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlin.collections.forEach
|
||||
import org.json.JSONArray
|
||||
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
@ProvidedTypeConverter
|
||||
|
|
@ -224,4 +227,29 @@ class Converters @Inject constructor(
|
|||
fun jsonToApplication(applicationJson: String?): Status.Application? {
|
||||
return applicationJson?.let { moshi.adapter<Status.Application?>().fromJson(it) }
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun notificationTypeListToJson(data: Set<Notification.Type>?): String {
|
||||
val array = JSONArray()
|
||||
data?.forEach {
|
||||
array.put(it.presentation)
|
||||
}
|
||||
return array.toString()
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToNotificationTypeList(data: String?): Set<Notification.Type> {
|
||||
val ret = HashSet<Notification.Type>()
|
||||
data?.let {
|
||||
val array = JSONArray(data)
|
||||
for (i in 0 until array.length()) {
|
||||
val item = array.getString(i)
|
||||
val type = Notification.Type.byString(item)
|
||||
if (type != Notification.Type.UNKNOWN) {
|
||||
ret.add(type)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import com.keylesspalace.tusky.TabData
|
|||
import com.keylesspalace.tusky.db.Converters
|
||||
import com.keylesspalace.tusky.defaultTabs
|
||||
import com.keylesspalace.tusky.entity.Emoji
|
||||
import com.keylesspalace.tusky.entity.Notification
|
||||
import com.keylesspalace.tusky.entity.Status
|
||||
import com.keylesspalace.tusky.settings.DefaultReplyVisibility
|
||||
|
||||
|
|
@ -93,7 +94,7 @@ data class AccountEntity(
|
|||
val notificationMarkerId: String = "0",
|
||||
val emojis: List<Emoji> = emptyList(),
|
||||
val tabPreferences: List<TabData> = defaultTabs(),
|
||||
val notificationsFilter: String = "[\"follow_request\"]",
|
||||
val notificationsFilter: Set<Notification.Type> = setOf(Notification.Type.FOLLOW_REQUEST),
|
||||
// Scope cannot be changed without re-login, so store it in case
|
||||
// the scope needs to be changed in the future
|
||||
val oauthScopes: String = "",
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
/* Copyright 2019 Joel Pyska
|
||||
*
|
||||
* This file is a part of Tusky.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
package com.keylesspalace.tusky.util
|
||||
|
||||
import com.keylesspalace.tusky.entity.Notification
|
||||
import org.json.JSONArray
|
||||
|
||||
/**
|
||||
* Serialize to string array and deserialize notifications type
|
||||
*/
|
||||
|
||||
fun serialize(data: Set<Notification.Type>?): String {
|
||||
val array = JSONArray()
|
||||
data?.forEach {
|
||||
array.put(it.presentation)
|
||||
}
|
||||
return array.toString()
|
||||
}
|
||||
|
||||
fun deserialize(data: String?): Set<Notification.Type> {
|
||||
val ret = HashSet<Notification.Type>()
|
||||
data?.let {
|
||||
val array = JSONArray(data)
|
||||
for (i in 0 until array.length()) {
|
||||
val item = array.getString(i)
|
||||
val type = Notification.Type.byString(item)
|
||||
if (type != Notification.Type.UNKNOWN) {
|
||||
ret.add(type)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue