Skip to content

Commit

Permalink
Syncs files with the intermediate branch, so we can delete it.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlcerreca committed Dec 10, 2024
1 parent b3a362b commit 11b3b8b
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 270 deletions.
93 changes: 0 additions & 93 deletions app/src/androidTest/java/WorkerInstrumentationTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.work.WorkInfo
import kotlinx.coroutines.flow.Flow

interface BluromaticRepository {
val outputWorkInfo: Flow<WorkInfo>
val outputWorkInfo: Flow<WorkInfo?>
fun applyBlur(blurLevel: Int)
fun cancelWork()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,45 @@ package com.example.bluromatic.data

import android.content.Context
import android.net.Uri
import androidx.lifecycle.asFlow
import androidx.work.Constraints
import androidx.work.Data
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager
import com.example.bluromatic.IMAGE_MANIPULATION_WORK_NAME
import com.example.bluromatic.KEY_BLUR_LEVEL
import com.example.bluromatic.KEY_IMAGE_URI
import com.example.bluromatic.TAG_OUTPUT
import com.example.bluromatic.getImageUri
import com.example.bluromatic.workers.BlurWorker
import com.example.bluromatic.workers.CleanupWorker
import com.example.bluromatic.workers.SaveImageToFileWorker
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.MutableStateFlow

class WorkManagerBluromaticRepository(context: Context) : BluromaticRepository {

private var imageUri: Uri = context.getImageUri()
private val workManager = WorkManager.getInstance(context)

override val outputWorkInfo: Flow<WorkInfo> =
workManager.getWorkInfosByTagLiveData(TAG_OUTPUT).asFlow().mapNotNull {
if (it.isNotEmpty()) it.first() else null
}
override val outputWorkInfo: Flow<WorkInfo?> = MutableStateFlow(null)

/**
* Create the WorkRequests to apply the blur and save the resulting image
* @param blurLevel The amount to blur the image
*/
override fun applyBlur(blurLevel: Int) {
// Add WorkRequest to Cleanup temporary images
var continuation = workManager
.beginUniqueWork(
IMAGE_MANIPULATION_WORK_NAME,
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest.from(CleanupWorker::class.java)
)

// Create low battery constraint
val constraints = Constraints.Builder()
.setRequiresBatteryNotLow(true)
.build()
var continuation = workManager.beginWith(OneTimeWorkRequest.from(CleanupWorker::class.java))

// Add WorkRequest to blur the image
val blurBuilder = OneTimeWorkRequestBuilder<BlurWorker>()

// Input the Uri for the blur operation along with the blur level
blurBuilder.setInputData(createInputDataForWorkRequest(blurLevel, imageUri))

blurBuilder.setConstraints(constraints)

continuation = continuation.then(blurBuilder.build())

// Add WorkRequest to save the image to the filesystem
val save = OneTimeWorkRequestBuilder<SaveImageToFileWorker>()
.addTag(TAG_OUTPUT)
.build()
continuation = continuation.then(save)

Expand All @@ -88,9 +67,7 @@ class WorkManagerBluromaticRepository(context: Context) : BluromaticRepository {
/**
* Cancel any ongoing WorkRequests
* */
override fun cancelWork() {
workManager.cancelUniqueWork(IMAGE_MANIPULATION_WORK_NAME)
}
override fun cancelWork() {}

/**
* Creates the input data bundle which includes the blur level to
Expand Down

This file was deleted.

This file was deleted.

32 changes: 2 additions & 30 deletions app/src/main/java/com/example/bluromatic/ui/BlurViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@ package com.example.bluromatic.ui
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.work.WorkInfo
import com.example.bluromatic.BluromaticApplication
import com.example.bluromatic.KEY_IMAGE_URI
import com.example.bluromatic.data.BlurAmountData
import com.example.bluromatic.data.BluromaticRepository
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

/**
* [BlurViewModel] starts and stops the WorkManger and applies blur to the image. Also updates the
Expand All @@ -40,23 +35,7 @@ class BlurViewModel(private val bluromaticRepository: BluromaticRepository) : Vi

internal val blurAmount = BlurAmountData.blurAmount

val blurUiState: StateFlow<BlurUiState> = bluromaticRepository.outputWorkInfo
.map { info ->
val outputImageUri = info.outputData.getString(KEY_IMAGE_URI)
when {
info.state.isFinished && !outputImageUri.isNullOrEmpty() -> {
BlurUiState.Complete(outputUri = outputImageUri)
}
info.state == WorkInfo.State.CANCELLED -> {
BlurUiState.Default
}
else -> BlurUiState.Loading
}
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = BlurUiState.Default
)
val blurUiState: StateFlow<BlurUiState> = MutableStateFlow(BlurUiState.Default)

/**
* Call the method from repository to create the WorkRequest to apply the blur
Expand All @@ -67,13 +46,6 @@ class BlurViewModel(private val bluromaticRepository: BluromaticRepository) : Vi
bluromaticRepository.applyBlur(blurLevel)
}

/**
* Call method from repository to cancel any ongoing WorkRequest
* */
fun cancelWork() {
bluromaticRepository.cancelWork()
}

/**
* Factory for [BlurViewModel] that takes [BluromaticRepository] as a dependency
*/
Expand Down
36 changes: 7 additions & 29 deletions app/src/main/java/com/example/bluromatic/ui/BluromaticScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.calculateEndPadding
Expand All @@ -35,14 +34,11 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
Expand Down Expand Up @@ -89,7 +85,7 @@ fun BluromaticScreen(blurViewModel: BlurViewModel = viewModel(factory = BlurView
blurUiState = uiState,
blurAmountOptions = blurViewModel.blurAmount,
applyBlur = blurViewModel::applyBlur,
cancelWork = blurViewModel::cancelWork,
cancelWork = {},
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(dimensionResource(R.dimen.padding_medium))
Expand Down Expand Up @@ -125,9 +121,7 @@ fun BluromaticScreenContent(
BlurActions(
blurUiState = blurUiState,
onStartClick = { applyBlur(selectedValue) },
onSeeFileClick = { currentUri ->
showBlurredImage(context, currentUri)
},
onSeeFileClick = {},
onCancelClick = { cancelWork() },
modifier = Modifier.fillMaxWidth()
)
Expand All @@ -146,27 +140,11 @@ private fun BlurActions(
modifier = modifier,
horizontalArrangement = Arrangement.Center
) {
when (blurUiState) {
is BlurUiState.Default -> {
Button(
onClick = onStartClick,
modifier = Modifier.fillMaxWidth()
) {
Text(stringResource(R.string.start))
}
}

is BlurUiState.Loading -> {
FilledTonalButton(onCancelClick) { Text(stringResource(R.string.cancel_work)) }
CircularProgressIndicator(modifier = Modifier.padding(dimensionResource(R.dimen.padding_small)))
}

is BlurUiState.Complete -> {
Button(onStartClick) { Text(stringResource(R.string.start)) }
Spacer(modifier = Modifier.width(dimensionResource(R.dimen.padding_small)))
FilledTonalButton({ onSeeFileClick(blurUiState.outputUri) })
{ Text(stringResource(R.string.see_file)) }
}
Button(
onClick = onStartClick,
modifier = Modifier.fillMaxWidth()
) {
Text(stringResource(R.string.start))
}
}
}
Expand Down
Loading

0 comments on commit 11b3b8b

Please sign in to comment.