WIP prefs: adapt code to PrefStore

This commit is contained in:
charlag 2022-06-08 18:23:21 +02:00
parent 942afc8503
commit a43a749eb3
No known key found for this signature in database
GPG Key ID: 5B96E7C76F0CA558
4 changed files with 39 additions and 36 deletions

View File

@ -68,13 +68,11 @@ import com.keylesspalace.tusky.viewdata.AttachmentViewData
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx3.asFlow
import java.io.IOException
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.concurrent.timer
class TimelineFragment :
SFragment(),
@ -161,6 +159,12 @@ class TimelineFragment :
statusDisplayOptions,
this
)
lifecycleScope.launch {
prefStore.data.collect { data ->
hideFab = data.hideFab
}
}
}
override fun onCreateView(
@ -421,9 +425,6 @@ class TimelineFragment :
private suspend fun onPreferenceChanged(key: String) {
when (key) {
PrefKeys.FAB_HIDE -> {
hideFab = prefStore.get().hideFab
}
PrefKeys.MEDIA_PREVIEW_ENABLED -> {
val enabled = accountManager.activeAccount!!.mediaPreviewEnabled
val oldMediaPreviewEnabled = adapter.mediaPreviewEnabled

View File

@ -41,7 +41,6 @@ import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.network.TimelineCases
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.settings.PrefStore
import com.keylesspalace.tusky.settings.get
import com.keylesspalace.tusky.settings.getBlocking
import com.keylesspalace.tusky.viewdata.StatusViewData
import kotlinx.coroutines.Job
@ -60,7 +59,6 @@ abstract class TimelineViewModel(
) : ViewModel() {
abstract val statuses: Flow<PagingData<StatusViewData>>
var kind: Kind = Kind.HOME
private set
var id: String? = null
@ -96,6 +94,24 @@ abstract class TimelineViewModel(
.collect { event -> handleEvent(event) }
}
viewModelScope.launch {
prefStore.data.collect { data ->
val repliesFilter = data.tabFilterHomeReplies
val oldRemoveReplies = filterRemoveReplies
filterRemoveReplies = kind == Kind.HOME && !repliesFilter
if (oldRemoveReplies != filterRemoveReplies) {
fullReload()
}
val boostsFilter = data.tabFilterHomeBoosts
val oldRemoveReblogs = filterRemoveReblogs
filterRemoveReblogs = kind == Kind.HOME && !boostsFilter
if (oldRemoveReblogs != filterRemoveReblogs) {
fullReload()
}
}
}
reloadFilters()
}
@ -182,22 +198,6 @@ abstract class TimelineViewModel(
private suspend fun onPreferenceChanged(key: String) {
when (key) {
PrefKeys.TAB_FILTER_HOME_REPLIES -> {
val filter = prefStore.get().tabFilterHomeReplies
val oldRemoveReplies = filterRemoveReplies
filterRemoveReplies = kind == Kind.HOME && !filter
if (oldRemoveReplies != filterRemoveReplies) {
fullReload()
}
}
PrefKeys.TAB_FILTER_HOME_BOOSTS -> {
val filter = prefStore.get().tabFilterHomeBoosts
val oldRemoveReblogs = filterRemoveReblogs
filterRemoveReblogs = kind == Kind.HOME && !filter
if (oldRemoveReblogs != filterRemoveReblogs) {
fullReload()
}
}
Filter.HOME, Filter.NOTIFICATIONS, Filter.THREAD, Filter.PUBLIC, Filter.ACCOUNT -> {
if (filterContextMatchesKind(kind, listOf(key))) {
reloadFilters()

View File

@ -380,6 +380,17 @@ public class NotificationsFragment extends SFragment implements
onPreferenceChanged(((PreferenceChangedEvent) event).getPreferenceKey());
}
});
Prefs.asObservable(prefStore)
.to(autoDisposable(from(this)))
.subscribe((prefData) -> {
hideFab = prefData.getHideFab();
if (isAdded()) {
showNotificationsFilter = Prefs.getBlocking(prefStore).getShowNotificationsFilter();
updateFilterVisibility();
fullyRefreshWithProgressBar(true);
}
});
}
@Override
@ -801,10 +812,6 @@ public class NotificationsFragment extends SFragment implements
private void onPreferenceChanged(String key) {
switch (key) {
case PrefKeys.FAB_HIDE: {
hideFab = Prefs.getBlocking(prefStore).getHideFab();
break;
}
case PrefKeys.MEDIA_PREVIEW_ENABLED: {
boolean enabled = accountManager.getActiveAccount().getMediaPreviewEnabled();
if (enabled != adapter.isMediaPreviewEnabled()) {
@ -813,14 +820,6 @@ public class NotificationsFragment extends SFragment implements
}
break;
}
case PrefKeys.SHOW_NOTIFICATIONS_FILTER: {
if (isAdded()) {
showNotificationsFilter = Prefs.getBlocking(prefStore).getShowNotificationsFilter();
updateFilterVisibility();
fullyRefreshWithProgressBar(true);
}
break;
}
}
}

View File

@ -11,6 +11,7 @@ import com.keylesspalace.tusky.util.ThemeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.rx3.asObservable
import java.io.File
import java.io.InputStream
import java.io.OutputStream
@ -92,4 +93,6 @@ fun makePrefStore(context: Context, scope: CoroutineScope): PrefStore {
// yet.
File(context.filesDir, "datastore/prefs.json")
}
}
}
fun PrefStore.asObservable() = data.asObservable()