-
-
Save DivS-15/7ba2b7215841038bd8d80851e67d2b06 to your computer and use it in GitHub Desktop.
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
@HiltViewModel | |
class BlurViewModel @Inject constructor( | |
application: Application, | |
private val workManager: WorkManager | |
) : ViewModel() { | |
//private val workManager = WorkManager.getInstance(application) | |
internal var imageUri: Uri? = null | |
internal var outputUri: Uri? = null | |
init { | |
imageUri = getImageUri(application.applicationContext) | |
} | |
private fun createInputDataForUri(): Data { | |
val builder = Data.Builder() | |
imageUri?.let { | |
builder.putString(KEY_IMAGE_URI, imageUri.toString()) | |
} | |
return builder.build() | |
} | |
/** | |
* Create the WorkRequest to apply the blur and save the resulting image | |
* @param blurLevel The amount to blur the image | |
*/ | |
internal fun applyBlur(blurLevel: Int) { | |
val workRequest = | |
OneTimeWorkRequestBuilder<BlurWorker>().setInputData(createInputDataForUri()).build() | |
workManager.enqueue(workRequest) | |
} | |
private fun uriOrNull(uriString: String?): Uri? { | |
return if (!uriString.isNullOrEmpty()) { | |
Uri.parse(uriString) | |
} else { | |
null | |
} | |
} | |
private fun getImageUri(context: Context): Uri { | |
val resources = context.resources | |
val imageUri = Uri.Builder() | |
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) | |
.authority(resources.getResourcePackageName(R.drawable.android_cupcake)) | |
.appendPath(resources.getResourceTypeName(R.drawable.android_cupcake)) | |
.appendPath(resources.getResourceEntryName(R.drawable.android_cupcake)) | |
.build() | |
return imageUri | |
} | |
internal fun setOutputUri(outputImageUri: String?) { | |
outputUri = uriOrNull(outputImageUri) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment