Skip to content

Commit

Permalink
Merge branch 'dev' into remotecontrols/move-into-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Sep 13, 2024
2 parents 1473aed + 46140b1 commit da7bb10
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 184 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Attention: don't forget to add the flag for F-Droid before release
- [FIX] Fix app bar colors on remote controls
- [FIX] Fix remote controls texts and favorite dropdown
- [FIX] Move remote controls into tools tab
- [FIX] Fix crash when saving files on remote controls
- [FIX] Remove share from remote controls
- [CI] Fix merge-queue files diff
- [CI] Add https://github.com/LionZXY/detekt-decompose-rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ interface SaveTempSignalApi : InstanceKeeper.Instance {

sealed interface State {
data object Pending : State
data class Uploading(val progressInternal: Long, val total: Long) : State {
val progressPercent: Float = if (total == 0L) 0f else progressInternal / total.toFloat()
}
data class Uploading(val progressPercent: Float) : State

data object Uploaded : State
}
Expand Down
3 changes: 3 additions & 0 deletions components/remote-controls/setup/impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ dependencies {
implementation(projects.components.core.ui.ktx)
implementation(projects.components.core.ui.res)
implementation(projects.components.core.ui.dialog)
implementation(projects.components.core.preference)
implementation(projects.components.core.progress)

implementation(projects.components.bridge.dao.api)
implementation(projects.components.bridge.service.api)
implementation(projects.components.bridge.pbutils)
implementation(projects.components.bridge.api)
implementation(projects.components.bridge.rpc.api)
implementation(projects.components.keyemulate.api)
implementation(projects.components.infrared.utils)
implementation(projects.components.infrared.api)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.flipperdevices.remotecontrols.impl.setup.presentation.viewmodel

import android.content.Context
import com.flipperdevices.bridge.dao.api.model.FlipperFilePath
import com.flipperdevices.bridge.service.api.provider.FlipperServiceProvider
import com.flipperdevices.bridge.rpc.api.FlipperStorageApi
import com.flipperdevices.core.di.AppGraph
import com.flipperdevices.core.ktx.jre.FlipperDispatchers
import com.flipperdevices.core.ktx.jre.launchWithLock
import com.flipperdevices.core.log.LogTagProvider
import com.flipperdevices.core.preference.FlipperStorageProvider
import com.flipperdevices.core.ui.lifecycle.DecomposeViewModel
import com.flipperdevices.remotecontrols.api.SaveTempSignalApi
import com.flipperdevices.remotecontrols.impl.setup.api.save.file.SaveFileApi
import com.flipperdevices.remotecontrols.impl.setup.api.save.folder.SaveFolderApi
import com.squareup.anvil.annotations.ContributesBinding
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
Expand All @@ -24,9 +20,8 @@ private const val EXT_PATH = "/ext"

@ContributesBinding(AppGraph::class, SaveTempSignalApi::class)
class SaveTempSignalViewModel @Inject constructor(
private val serviceProvider: FlipperServiceProvider,
private val saveFileApi: SaveFileApi,
private val saveFolderApi: SaveFolderApi,
private val flipperStorageApi: FlipperStorageApi,
private val context: Context
) : DecomposeViewModel(),
LogTagProvider,
SaveTempSignalApi {
Expand All @@ -40,38 +35,26 @@ class SaveTempSignalViewModel @Inject constructor(
onFinished: () -> Unit,
) {
viewModelScope.launch {
var progressInternal = 0L
val totalSize = filesDesc.sumOf { it.textContent.toByteArray().size.toLong() }
_state.emit(SaveTempSignalApi.State.Uploading(0, totalSize))
val serviceApi = serviceProvider.getServiceApi()
_state.emit(SaveTempSignalApi.State.Uploading(0f))
launchWithLock(mutex, viewModelScope, "load") {
filesDesc.forEach { fileDesc ->
val absolutePath = FlipperFilePath(
folder = fileDesc.extFolderPath,
nameWithExtension = fileDesc.nameWithExtension
).getPathOnFlipper()

saveFolderApi.save(serviceApi.requestApi, "$EXT_PATH/${fileDesc.extFolderPath}")
val saveFileFlow = saveFileApi.save(
requestApi = serviceApi.requestApi,
textContent = fileDesc.textContent,
absolutePath = absolutePath
)
saveFileFlow
.flowOn(FlipperDispatchers.workStealingDispatcher)
.onEach {
_state.value = when (it) {
SaveFileApi.Status.Finished -> SaveTempSignalApi.State.Uploaded
is SaveFileApi.Status.Saving -> {
progressInternal += it.lastWriteSize
SaveTempSignalApi.State.Uploading(
progressInternal,
totalSize
)
}
filesDesc.forEachIndexed { index, fileDesc ->
FlipperStorageProvider.useTemporaryFile(context) { deviceFile ->
deviceFile.writeText(fileDesc.textContent)
val fAbsolutePath = FlipperFilePath(
folder = fileDesc.extFolderPath,
nameWithExtension = fileDesc.nameWithExtension
).getPathOnFlipper()
flipperStorageApi.mkdirs("$EXT_PATH/${fileDesc.extFolderPath}")
flipperStorageApi.upload(
pathOnFlipper = fAbsolutePath,
fileOnAndroid = deviceFile,
progressListener = { currentProgress ->
_state.value = SaveTempSignalApi.State.Uploading(
progressPercent = index + currentProgress
)
}
}
.collect()
)
}
}
_state.value = SaveTempSignalApi.State.Uploaded
onFinished.invoke()
Expand Down

0 comments on commit da7bb10

Please sign in to comment.