Skip to content

Commit

Permalink
WIP Migrating away from StoreBox to DataStore
Browse files Browse the repository at this point in the history
- Added DataStore dependency
- Created AppPreferencesImpl

Signed-off-by: Julius Linus <[email protected]>
  • Loading branch information
rapterjet2004 committed Sep 6, 2023
1 parent bc7939f commit c786a0e
Show file tree
Hide file tree
Showing 6 changed files with 700 additions and 391 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ configurations.all {

dependencies {
implementation 'androidx.preference:preference-ktx:1.2.1'
implementation 'androidx.datastore:datastore-core:1.0.0'
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.1")

implementation fileTree(include: ['*'], dir: 'libs')
Expand Down Expand Up @@ -237,7 +238,7 @@ dependencies {
implementation "androidx.room:room-ktx:${roomVersion}"

implementation "org.parceler:parceler-api:$parcelerVersion"
implementation 'net.orange-box.storebox:storebox-lib:1.4.0'
implementation 'net.orange-box.:-lib:1.4.0'
implementation 'eu.davidea:flexible-adapter:5.1.0'
implementation 'eu.davidea:flexible-adapter-ui:1.0.0'
implementation 'org.apache.commons:commons-lang3:3.13.0'
Expand Down Expand Up @@ -282,6 +283,7 @@ dependencies {
//noinspection DuplicatePlatformClasses
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
})
implementation("androidx.datastore:datastore-preferences:1.0.0")

implementation 'androidx.core:core-ktx:1.10.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,33 @@

import com.nextcloud.talk.data.source.local.TalkDatabase;
import com.nextcloud.talk.utils.preferences.AppPreferences;

import net.orange_box.storebox.StoreBox;
import com.nextcloud.talk.utils.preferences.AppPreferencesImpl;

import javax.inject.Singleton;

import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import dagger.Module;
import dagger.Provides;
import kotlinx.coroutines.ExperimentalCoroutinesApi;

@Module
@OptIn(markerClass = ExperimentalCoroutinesApi.class)
public class DatabaseModule {
@Provides
@Singleton
public AppPreferences providePreferences(@NonNull final Context poContext) {
AppPreferences preferences = StoreBox.create(poContext, AppPreferences.class);
AppPreferences preferences = new AppPreferencesImpl(poContext);
preferences.removeLinkPreviews();
return preferences;
}

@Provides
@Singleton
public AppPreferencesImpl providePreferencesImpl(@NonNull final Context poContext) {
return new AppPreferencesImpl(poContext);
}

@Provides
@Singleton
public TalkDatabase provideTalkDatabase(@NonNull final Context context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@

package com.nextcloud.talk.remotefilebrowser.viewmodels

import android.content.res.Resources
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.nextcloud.talk.R
import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem
import com.nextcloud.talk.remotefilebrowser.repositories.RemoteFileBrowserItemsRepository
import com.nextcloud.talk.utils.FileSortOrder
import com.nextcloud.talk.utils.Mimetype.FOLDER
import com.nextcloud.talk.utils.preferences.AppPreferences
import com.nextcloud.talk.utils.preferences.AppPreferencesImpl
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

Expand All @@ -52,9 +58,12 @@ import javax.inject.Inject
* FinishState --> [*]
* @enduml
*/
class RemoteFileBrowserItemsViewModel @Inject constructor(
@OptIn(ExperimentalCoroutinesApi::class)
class RemoteFileBrowserItemsViewModel
@Inject
constructor(
private val repository: RemoteFileBrowserItemsRepository,
private val appPreferences: AppPreferences
private val appPreferences: AppPreferencesImpl
) :
ViewModel() {

Expand All @@ -66,7 +75,8 @@ class RemoteFileBrowserItemsViewModel @Inject constructor(
class FinishState(val selectedPaths: Set<String>) : ViewState

private val initialSortOrder = FileSortOrder.getFileSortOrder(appPreferences.sorting)
private val sortingPrefListener: SortChangeListener = SortChangeListener()

private var sortingFlow: Flow<String>

private val _viewState: MutableLiveData<ViewState> = MutableLiveData(InitialState)
val viewState: LiveData<ViewState>
Expand All @@ -86,20 +96,15 @@ class RemoteFileBrowserItemsViewModel @Inject constructor(
get() = _selectedPaths

init {
appPreferences.registerSortingChangeListener(sortingPrefListener)
}

inner class SortChangeListener : OnPreferenceValueChangedListener<String> {
override fun onChanged(newValue: String) {
onSelectSortOrder(newValue)
val key = Resources.getSystem().getString(R.string.nc_file_browser_sort_by_key)
sortingFlow = appPreferences.readString(key)
CoroutineScope(Dispatchers.Main).launch {
sortingFlow.collect { newString ->
onSelectSortOrder(newString)
}
}
}

override fun onCleared() {
super.onCleared()
appPreferences.unregisterSortingChangeListener(sortingPrefListener)
}

fun loadItems() {
_viewState.value = LoadingItemsState
repository.listFolder(currentPath.value!!).subscribeOn(Schedulers.io())
Expand Down
Loading

0 comments on commit c786a0e

Please sign in to comment.