forked from seedvault-app/seedvault
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
649 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/src/main/java/com/stevesoltys/seedvault/ui/check/FileCheckFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.ui.check | ||
|
||
import android.os.Bundle | ||
import android.text.format.Formatter | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Button | ||
import android.widget.ScrollView | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
import com.google.android.material.slider.LabelFormatter | ||
import com.google.android.material.slider.Slider | ||
import com.stevesoltys.seedvault.R | ||
import com.stevesoltys.seedvault.settings.SettingsViewModel | ||
import org.koin.androidx.viewmodel.ext.android.activityViewModel | ||
|
||
class FileCheckFragment : Fragment() { | ||
|
||
private val viewModel: SettingsViewModel by activityViewModel() | ||
private lateinit var sliderLabel: TextView | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
val v = inflater.inflate(R.layout.fragment_app_check, container, false) as ScrollView | ||
|
||
v.requireViewById<TextView>(R.id.titleView).setText(R.string.settings_file_check_title) | ||
v.requireViewById<TextView>(R.id.descriptionView).setText(R.string.settings_file_check_text) | ||
v.requireViewById<TextView>(R.id.introView).setText(R.string.settings_file_check_text2) | ||
|
||
val slider = v.requireViewById<Slider>(R.id.slider) | ||
sliderLabel = v.requireViewById(R.id.sliderLabel) | ||
|
||
// label not scrolling will be fixed in material-components 1.12.0 (next update) | ||
slider.setLabelFormatter { value -> | ||
viewModel.filesBackupSize.value?.let { | ||
Formatter.formatShortFileSize(context, (it * value / 100).toLong()) | ||
} ?: "${value.toInt()}%" | ||
} | ||
slider.addOnChangeListener { _, value, _ -> | ||
onSliderChanged(value) | ||
} | ||
|
||
viewModel.filesBackupSize.observe(viewLifecycleOwner) { | ||
if (it != null) { | ||
slider.labelBehavior = LabelFormatter.LABEL_VISIBLE | ||
slider.invalidate() | ||
onSliderChanged(slider.value) | ||
} | ||
// we can stop observing as the loaded size won't change again | ||
viewModel.filesBackupSize.removeObservers(viewLifecycleOwner) | ||
} | ||
|
||
v.requireViewById<Button>(R.id.startButton).setOnClickListener { | ||
viewModel.checkFileBackups(slider.value.toInt()) | ||
requireActivity().onBackPressedDispatcher.onBackPressed() | ||
} | ||
return v | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
viewModel.loadFileBackupSize() | ||
} | ||
|
||
private fun onSliderChanged(value: Float) { | ||
val size = viewModel.filesBackupSize.value | ||
// when size is unknown, we show warning based on percent | ||
val showWarning = if (size == null) { | ||
value > WARN_PERCENT | ||
} else { | ||
size * value / 100 > WARN_BYTES | ||
} | ||
// only update label visibility when different from before | ||
val newVisibility = if (showWarning) View.VISIBLE else View.GONE | ||
if (sliderLabel.visibility != newVisibility) { | ||
sliderLabel.visibility = newVisibility | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/stevesoltys/seedvault/ui/check/FileCheckResultActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.ui.check | ||
|
||
import android.os.Bundle | ||
import com.stevesoltys.seedvault.ui.setupEdgeToEdge | ||
import org.calyxos.backup.storage.api.StorageBackup | ||
import org.calyxos.backup.storage.ui.check.CheckResultActivity | ||
import org.koin.android.ext.android.inject | ||
|
||
class FileCheckResultActivity : CheckResultActivity() { | ||
|
||
override val storageBackup: StorageBackup by inject() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
setupEdgeToEdge() | ||
super.onCreate(savedInstanceState) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/stevesoltys/seedvault/worker/FileCheckerWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.worker | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkerParameters | ||
import com.stevesoltys.seedvault.BackupStateManager | ||
import com.stevesoltys.seedvault.ui.check.FileCheckResultActivity | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.coroutines.flow.first | ||
import org.calyxos.backup.storage.api.StorageBackup | ||
import org.calyxos.backup.storage.check.CheckerWorker | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class FileCheckerWorker( | ||
appContext: Context, | ||
workerParams: WorkerParameters, | ||
) : CheckerWorker(appContext, workerParams), KoinComponent { | ||
|
||
companion object { | ||
private val TAG = FileCheckerWorker::class.simpleName | ||
|
||
fun scheduleNow( | ||
context: Context, | ||
percent: Int, | ||
) { | ||
scheduleNow(context, percent, OneTimeWorkRequestBuilder<FileCheckerWorker>()) | ||
} | ||
} | ||
|
||
private val log = KotlinLogging.logger {} | ||
private val backupStateManager: BackupStateManager by inject() | ||
override val storageBackup: StorageBackup by inject() | ||
override val resultActivityClass: Class<*> = FileCheckResultActivity::class.java | ||
|
||
override suspend fun doWork(): Result { | ||
log.info { "Start worker $this ($id)" } | ||
if (backupStateManager.isBackupRunning.first()) { | ||
Log.i(TAG, "isBackupRunning was true, so retrying later...") | ||
return Result.retry() | ||
} | ||
return super.doWork() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
storage/lib/src/main/java/org/calyxos/backup/storage/check/NotificationCheckObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.calyxos.backup.storage.check | ||
|
||
import android.content.Context | ||
import org.calyxos.backup.storage.api.CheckObserver | ||
import org.calyxos.backup.storage.ui.Notifications | ||
|
||
public open class NotificationCheckObserver internal constructor( | ||
private val n: Notifications, | ||
private val reportActivityClass: Class<*>, | ||
) : CheckObserver { | ||
|
||
public constructor( | ||
context: Context, | ||
reportActivityClass: Class<*>, | ||
) : this(Notifications(context), reportActivityClass) | ||
|
||
override fun onStartChecking() { | ||
n.showCheckStartedNotification() | ||
} | ||
|
||
override fun onCheckUpdate(speed: Long, thousandth: Int) { | ||
n.showCheckNotification(speed, thousandth) | ||
} | ||
|
||
override fun onCheckSuccess(size: Long, speed: Long) { | ||
n.onCheckComplete(reportActivityClass, size, speed) | ||
} | ||
|
||
override fun onCheckFoundErrors(size: Long, speed: Long) { | ||
n.onCheckFinishedWithError(reportActivityClass, size, speed) | ||
} | ||
|
||
} |
Oops, something went wrong.