Skip to content

Commit

Permalink
refactor(PublicShareViewModel): Use a flow to avoid observing several…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
FabianDevel committed Oct 1, 2024
1 parent 2b1511e commit af27dfb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.content.Intent
import androidx.annotation.StringRes
import androidx.core.content.FileProvider
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.ui.fileList.BaseDownloadProgressDialog.DownloadAction
Expand All @@ -34,6 +35,7 @@ import com.infomaniak.drive.views.FileInfoActionsView.OnItemClickListener.Compan
import com.infomaniak.lib.core.utils.safeNavigate
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.invoke
import kotlinx.coroutines.launch

interface OnPublicShareItemClickListener : FileInfoActionsView.OnItemClickListener {

Expand All @@ -46,8 +48,10 @@ interface OnPublicShareItemClickListener : FileInfoActionsView.OnItemClickListen
fun onDownloadError(@StringRes errorMessage: Int)

fun observeCacheFileForAction(viewLifecycleOwner: LifecycleOwner) {
publicShareViewModel.fetchCacheFileForActionResult.observe(viewLifecycleOwner) { (cacheFile, action) ->
cacheFile?.let { file -> executeDownloadAction(action, file) } ?: onDownloadError(getErrorMessage(action))
viewLifecycleOwner.lifecycleScope.launch {
publicShareViewModel.fetchCacheFileForActionResult.collect { (cacheFile, action) ->
executeAction(cacheFile, action)
}
}
}

Expand All @@ -72,16 +76,15 @@ interface OnPublicShareItemClickListener : FileInfoActionsView.OnItemClickListen
}

private fun startAction(action: DownloadAction) {
val cacheFileResult = publicShareViewModel.fetchCacheFileForAction(
publicShareViewModel.fetchCacheFileForAction(
file = currentFile,
action = action,
navigateToDownloadDialog = ::navigateToDownloadDialog,
)
}

ownerFragment?.viewLifecycleOwner?.let { lifecycleOwner ->
cacheFileResult.observe(lifecycleOwner) { cacheFile ->
cacheFile?.let { file -> executeDownloadAction(action, file) } ?: onDownloadError(getErrorMessage(action))
}
}
fun executeAction(cacheFile: IOFile?, action: DownloadAction) {
cacheFile?.let { file -> executeDownloadAction(action, file) } ?: onDownloadError(getErrorMessage(action))
}

private suspend fun navigateToDownloadDialog() = Dispatchers.Main {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.activity.result.contract.ActivityResultContracts.StartActivityFo
import androidx.core.content.FileProvider
import androidx.core.view.isGone
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
Expand All @@ -37,6 +38,7 @@ import com.infomaniak.drive.ui.SaveExternalFilesActivity
import com.infomaniak.drive.ui.SaveExternalFilesActivity.Companion.DESTINATION_DRIVE_ID_KEY
import com.infomaniak.drive.ui.SaveExternalFilesActivity.Companion.DESTINATION_FOLDER_ID_KEY
import com.infomaniak.drive.ui.SaveExternalFilesActivityArgs
import com.infomaniak.drive.ui.fileList.BaseDownloadProgressDialog.DownloadAction
import com.infomaniak.drive.ui.fileList.FileListFragment
import com.infomaniak.drive.ui.fileList.multiSelect.MultiSelectActionsBottomSheetDialog
import com.infomaniak.drive.ui.fileList.preview.PreviewDownloadProgressDialogArgs
Expand All @@ -54,6 +56,7 @@ import com.infomaniak.lib.core.utils.safeNavigate
import com.infomaniak.lib.core.utils.whenResultIsOk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.invoke
import kotlinx.coroutines.launch
import com.infomaniak.lib.core.R as RCore

class PublicShareListFragment : FileListFragment() {
Expand Down Expand Up @@ -121,6 +124,12 @@ class PublicShareListFragment : FileListFragment() {

observeRootFile()
observeFiles()

viewLifecycleOwner.lifecycleScope.launch {
publicShareViewModel.fetchCacheFileForActionResult.collect { (cacheFile, action) ->
if (action == DownloadAction.OPEN_BOOKMARK) executeOpenBookmarkAction(cacheFile)
}
}
}

private fun initFileAdapter() {
Expand Down Expand Up @@ -219,6 +228,7 @@ class PublicShareListFragment : FileListFragment() {
private fun openBookmark(file: File) {
publicShareViewModel.fetchCacheFileForAction(
file = file,
action = DownloadAction.OPEN_BOOKMARK,
navigateToDownloadDialog = {
Dispatchers.Main {
safeNavigate(
Expand All @@ -227,7 +237,7 @@ class PublicShareListFragment : FileListFragment() {
)
}
},
).observe(viewLifecycleOwner, ::executeOpenBookmarkAction)
)
}

private fun executeOpenBookmarkAction(cacheFile: IOFile?) = runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package com.infomaniak.drive.ui.publicShare

import android.app.Application
import androidx.lifecycle.*
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.infomaniak.drive.MainApplication
import com.infomaniak.drive.data.api.ApiRepository
import com.infomaniak.drive.data.api.CursorApiResponse
Expand All @@ -34,6 +37,8 @@ import com.infomaniak.lib.core.models.ApiError
import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.lib.core.utils.SingleLiveEvent
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow

class PublicShareViewModel(application: Application, val savedStateHandle: SavedStateHandle) : AndroidViewModel(application) {

Expand All @@ -44,7 +49,10 @@ class PublicShareViewModel(application: Application, val savedStateHandle: Saved
var fileClicked: File? = null
val downloadProgressLiveData = MutableLiveData(0)
val buildArchiveResult = SingleLiveEvent<Pair<Int?, ArchiveUUID?>>()
val fetchCacheFileForActionResult = SingleLiveEvent<Pair<IOFile?, DownloadAction>>()
val fetchCacheFileForActionResult = MutableSharedFlow<Pair<IOFile?, DownloadAction>>(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
val initPublicShareResult = SingleLiveEvent<Pair<ApiError?, ShareLink?>>()
val submitPasswordResult = SingleLiveEvent<Boolean?>()
var hasBeenAuthenticated = false
Expand Down Expand Up @@ -159,19 +167,21 @@ class PublicShareViewModel(application: Application, val savedStateHandle: Saved
buildArchiveResult.postValue(result)
}

fun fetchCacheFileForAction(file: File?, navigateToDownloadDialog: suspend () -> Unit) = liveData(Dispatchers.IO) {
runCatching {
emit(
file!!.convertToIOFile(
context = appContext,
onProgress = downloadProgressLiveData::postValue,
navigateToDownloadDialog = navigateToDownloadDialog,
fun fetchCacheFileForAction(file: File?, action: DownloadAction, navigateToDownloadDialog: suspend () -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
runCatching {
fetchCacheFileForActionResult.emit(
file!!.convertToIOFile(
context = appContext,
onProgress = downloadProgressLiveData::postValue,
navigateToDownloadDialog = navigateToDownloadDialog,
) to action
)
)
}.onFailure { exception ->
emit(null)
downloadProgressLiveData.postValue(null)
exception.printStackTrace()
}.onFailure { exception ->
fetchCacheFileForActionResult.emit(null to action)
downloadProgressLiveData.postValue(null)
exception.printStackTrace()
}
}
}

Expand Down

0 comments on commit af27dfb

Please sign in to comment.