RemoteFileBrowser: move currentPath to viewmodel, and fix title bar

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-06-06 17:40:57 +02:00 committed by Andy Scherzinger
parent 2fd97856d8
commit ffdadc6c01
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
2 changed files with 30 additions and 16 deletions

View File

@ -67,7 +67,6 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe
private var filesSelectionDoneMenuItem: MenuItem? = null
private val selectedPaths: MutableSet<String> = Collections.synchronizedSet(TreeSet())
private var currentPath: String = "/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -88,7 +87,6 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe
ResourcesCompat.getColor(resources, R.color.bg_default, null)
)
supportActionBar?.title = "current patch"
supportActionBar?.setDisplayHomeAsUpEnabled(true)
initViewModel()
@ -97,10 +95,10 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe
binding.swipeRefreshList.setColorSchemeResources(R.color.colorPrimary)
binding.swipeRefreshList.setProgressBackgroundColorSchemeResource(R.color.refresh_spinner_background)
binding.pathNavigationBackButton.setOnClickListener { goBack() }
binding.pathNavigationBackButton.setOnClickListener { viewModel.navigateUp() }
binding.sortButton.setOnClickListener { changeSorting() }
viewModel.loadItems(currentPath)
viewModel.loadItems()
}
private fun initViewModel() {
@ -156,6 +154,12 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe
binding.sortButton.setText(DisplayUtils.getSortOrderStringId(sortOrder))
}
}
viewModel.currentPath.observe(this) { path ->
if (path != null) {
supportActionBar?.title = path
}
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@ -168,8 +172,7 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe
private fun onItemClicked(remoteFileBrowserItem: RemoteFileBrowserItem) {
if ("inode/directory" == remoteFileBrowserItem.mimeType) {
currentPath = remoteFileBrowserItem.path!!
viewModel.loadItems(currentPath)
viewModel.changePath(remoteFileBrowserItem.path!!)
} else {
toggleBrowserItemSelection(remoteFileBrowserItem.path!!)
}
@ -189,13 +192,6 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe
)
}
private fun goBack(): Boolean {
if (currentPath != "/") {
viewModel.loadItems(File(currentPath).parent!!)
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
@ -244,7 +240,7 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe
}
private fun refreshCurrentPath() {
viewModel.loadItems(currentPath)
viewModel.loadItems()
}
private fun shouldPathBeSelectedDueToParent(currentPath: String): Boolean {

View File

@ -33,6 +33,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener
import java.io.File
import javax.inject.Inject
class RemoteFileBrowserItemsViewModel @Inject constructor(
@ -59,6 +60,10 @@ class RemoteFileBrowserItemsViewModel @Inject constructor(
val fileSortOrder: LiveData<FileSortOrderNew>
get() = _fileSortOrder
private val _currentPath: MutableLiveData<String> = MutableLiveData(ROOT_PATH)
val currentPath: LiveData<String>
get() = _currentPath
init {
appPreferences.registerSortingChangeListener(sortingPrefListener)
}
@ -74,9 +79,9 @@ class RemoteFileBrowserItemsViewModel @Inject constructor(
appPreferences.unregisterSortingChangeListener(sortingPrefListener)
}
fun loadItems(path: String) {
fun loadItems() {
_viewState.value = LoadingItemsState
repository.listFolder(path).subscribeOn(Schedulers.io())
repository.listFolder(currentPath.value!!).subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(RemoteFileBrowserItemsObserver())
}
@ -126,6 +131,19 @@ class RemoteFileBrowserItemsViewModel @Inject constructor(
}
}
fun changePath(path: String) {
_currentPath.value = path
loadItems()
}
fun navigateUp() {
val path = _currentPath.value
if (path!! != ROOT_PATH) {
_currentPath.value = File(path).parent!!
loadItems()
}
}
companion object {
private val TAG = RemoteFileBrowserItemsViewModel::class.simpleName
private const val ROOT_PATH = "/"