WIP prefs: adapt code to PrefStore
This commit is contained in:
parent
942afc8503
commit
a43a749eb3
|
@ -68,13 +68,11 @@ import com.keylesspalace.tusky.viewdata.AttachmentViewData
|
||||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.rxjava3.core.Observable
|
import io.reactivex.rxjava3.core.Observable
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.rx3.asFlow
|
import kotlinx.coroutines.rx3.asFlow
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.concurrent.timer
|
|
||||||
|
|
||||||
class TimelineFragment :
|
class TimelineFragment :
|
||||||
SFragment(),
|
SFragment(),
|
||||||
|
@ -161,6 +159,12 @@ class TimelineFragment :
|
||||||
statusDisplayOptions,
|
statusDisplayOptions,
|
||||||
this
|
this
|
||||||
)
|
)
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
prefStore.data.collect { data ->
|
||||||
|
hideFab = data.hideFab
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
|
@ -421,9 +425,6 @@ class TimelineFragment :
|
||||||
|
|
||||||
private suspend fun onPreferenceChanged(key: String) {
|
private suspend fun onPreferenceChanged(key: String) {
|
||||||
when (key) {
|
when (key) {
|
||||||
PrefKeys.FAB_HIDE -> {
|
|
||||||
hideFab = prefStore.get().hideFab
|
|
||||||
}
|
|
||||||
PrefKeys.MEDIA_PREVIEW_ENABLED -> {
|
PrefKeys.MEDIA_PREVIEW_ENABLED -> {
|
||||||
val enabled = accountManager.activeAccount!!.mediaPreviewEnabled
|
val enabled = accountManager.activeAccount!!.mediaPreviewEnabled
|
||||||
val oldMediaPreviewEnabled = adapter.mediaPreviewEnabled
|
val oldMediaPreviewEnabled = adapter.mediaPreviewEnabled
|
||||||
|
|
|
@ -41,7 +41,6 @@ import com.keylesspalace.tusky.network.MastodonApi
|
||||||
import com.keylesspalace.tusky.network.TimelineCases
|
import com.keylesspalace.tusky.network.TimelineCases
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys
|
import com.keylesspalace.tusky.settings.PrefKeys
|
||||||
import com.keylesspalace.tusky.settings.PrefStore
|
import com.keylesspalace.tusky.settings.PrefStore
|
||||||
import com.keylesspalace.tusky.settings.get
|
|
||||||
import com.keylesspalace.tusky.settings.getBlocking
|
import com.keylesspalace.tusky.settings.getBlocking
|
||||||
import com.keylesspalace.tusky.viewdata.StatusViewData
|
import com.keylesspalace.tusky.viewdata.StatusViewData
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
|
@ -60,7 +59,6 @@ abstract class TimelineViewModel(
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
abstract val statuses: Flow<PagingData<StatusViewData>>
|
abstract val statuses: Flow<PagingData<StatusViewData>>
|
||||||
|
|
||||||
var kind: Kind = Kind.HOME
|
var kind: Kind = Kind.HOME
|
||||||
private set
|
private set
|
||||||
var id: String? = null
|
var id: String? = null
|
||||||
|
@ -96,6 +94,24 @@ abstract class TimelineViewModel(
|
||||||
.collect { event -> handleEvent(event) }
|
.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()
|
reloadFilters()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,22 +198,6 @@ abstract class TimelineViewModel(
|
||||||
|
|
||||||
private suspend fun onPreferenceChanged(key: String) {
|
private suspend fun onPreferenceChanged(key: String) {
|
||||||
when (key) {
|
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 -> {
|
Filter.HOME, Filter.NOTIFICATIONS, Filter.THREAD, Filter.PUBLIC, Filter.ACCOUNT -> {
|
||||||
if (filterContextMatchesKind(kind, listOf(key))) {
|
if (filterContextMatchesKind(kind, listOf(key))) {
|
||||||
reloadFilters()
|
reloadFilters()
|
||||||
|
|
|
@ -380,6 +380,17 @@ public class NotificationsFragment extends SFragment implements
|
||||||
onPreferenceChanged(((PreferenceChangedEvent) event).getPreferenceKey());
|
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
|
@Override
|
||||||
|
@ -801,10 +812,6 @@ public class NotificationsFragment extends SFragment implements
|
||||||
|
|
||||||
private void onPreferenceChanged(String key) {
|
private void onPreferenceChanged(String key) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case PrefKeys.FAB_HIDE: {
|
|
||||||
hideFab = Prefs.getBlocking(prefStore).getHideFab();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PrefKeys.MEDIA_PREVIEW_ENABLED: {
|
case PrefKeys.MEDIA_PREVIEW_ENABLED: {
|
||||||
boolean enabled = accountManager.getActiveAccount().getMediaPreviewEnabled();
|
boolean enabled = accountManager.getActiveAccount().getMediaPreviewEnabled();
|
||||||
if (enabled != adapter.isMediaPreviewEnabled()) {
|
if (enabled != adapter.isMediaPreviewEnabled()) {
|
||||||
|
@ -813,14 +820,6 @@ public class NotificationsFragment extends SFragment implements
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PrefKeys.SHOW_NOTIFICATIONS_FILTER: {
|
|
||||||
if (isAdded()) {
|
|
||||||
showNotificationsFilter = Prefs.getBlocking(prefStore).getShowNotificationsFilter();
|
|
||||||
updateFilterVisibility();
|
|
||||||
fullyRefreshWithProgressBar(true);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import com.keylesspalace.tusky.util.ThemeUtils
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.coroutines.rx3.asObservable
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
@ -93,3 +94,5 @@ fun makePrefStore(context: Context, scope: CoroutineScope): PrefStore {
|
||||||
File(context.filesDir, "datastore/prefs.json")
|
File(context.filesDir, "datastore/prefs.json")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun PrefStore.asObservable() = data.asObservable()
|
Loading…
Reference in New Issue