Skip to content

Commit

Permalink
Refactor Domain use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEblan committed Jan 31, 2024
1 parent fe81bce commit ba27e46
Show file tree
Hide file tree
Showing 15 changed files with 658 additions and 407 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DefaultClipboardRepository @Inject constructor(
override fun setPrimaryClip(label: String, text: String): String? {
clipboardManagerWrapper.setPrimaryClip(label = label, text = text)

return if (!buildVersionWrapper.isAtLeastApi31()) "$label copied to clipboard"
else null
return if (buildVersionWrapper.isApi31Higher()) null
else "$label copied to clipboard"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import javax.inject.Inject

class DefaultBuildVersionWrapper @Inject constructor() : BuildVersionWrapper {
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
override fun isAtLeastApi31(): Boolean {
override fun isApi31Higher(): Boolean {
return Build.VERSION.SDK_INT >= 31
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.core.domain.usecase

import com.core.domain.repository.AppSettingsRepository
import com.core.domain.repository.SecureSettingsRepository
import kotlinx.coroutines.flow.first
import javax.inject.Inject

class ApplyAppSettingsUseCase @Inject constructor(
private val appSettingsRepository: AppSettingsRepository,
private val secureSettingsRepository: SecureSettingsRepository
) {

suspend operator fun invoke(
packageName: String,
onEmptyAppSettingsList: (String) -> Unit,
onAppSettingsDisabled: (String) -> Unit,
onAppSettingsNotSafeToWrite: (String) -> Unit,
onApplied: (String) -> Unit,
onSecurityException: (String) -> Unit,
onFailure: (String?) -> Unit
) {

val appSettingsList = appSettingsRepository.getAppSettingsList(packageName).first()

if (appSettingsList.isEmpty()) {

onEmptyAppSettingsList("No settings found")

return
}

if (appSettingsList.any { !it.enabled }) {

onAppSettingsDisabled("Please enable atleast one setting")

return
}

if (appSettingsList.any { !it.safeToWrite }) {

onAppSettingsNotSafeToWrite("Applying settings that don't exist is not allowed. Please remove items highlighted as red")

return
}

secureSettingsRepository.applySecureSettings(appSettingsList).onSuccess { applied ->
if (applied) onApplied("Settings applied")
else onFailure("Database failure")
}.onFailure { t ->
if (t is SecurityException) {
onSecurityException("Permission not granted")
} else {
onFailure(t.localizedMessage)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.core.domain.usecase

import com.core.domain.repository.AppSettingsRepository
import com.core.domain.repository.SecureSettingsRepository
import kotlinx.coroutines.flow.first
import javax.inject.Inject

class RevertAppSettingsUseCase @Inject constructor(
private val appSettingsRepository: AppSettingsRepository,
private val secureSettingsRepository: SecureSettingsRepository
) {

suspend operator fun invoke(
packageName: String,
onEmptyAppSettingsList: (String) -> Unit,
onAppSettingsDisabled: (String) -> Unit,
onAppSettingsNotSafeToWrite: (String) -> Unit,
onReverted: (String) -> Unit,
onSecurityException: (String) -> Unit,
onFailure: (String?) -> Unit
) {

val appSettingsList = appSettingsRepository.getAppSettingsList(packageName).first()

if (appSettingsList.isEmpty()) {

onEmptyAppSettingsList("No settings found")

return
}

if (appSettingsList.any { !it.enabled }) {

onAppSettingsDisabled("Please enable atleast one setting")

return
}

if (appSettingsList.any { !it.safeToWrite }) {

onAppSettingsNotSafeToWrite("Reverting settings that don't exist is not allowed. Please remove items highlighted as red")

return
}

secureSettingsRepository.revertSecureSettings(appSettingsList).onSuccess { applied ->
if (applied) onReverted("Settings reverted")
else onFailure("Database failure")
}.onFailure { t ->
if (t is SecurityException) {
onSecurityException("Permission not granted")
} else {
onFailure(t.localizedMessage)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.core.domain.wrapper

interface BuildVersionWrapper {
fun isAtLeastApi31(): Boolean
fun isApi31Higher(): Boolean
}
Loading

0 comments on commit ba27e46

Please sign in to comment.