Skip to content

Commit

Permalink
Refactor ShortcutRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEblan committed May 31, 2024
1 parent a253637 commit 63098a9
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
internal interface BuildVersionModule {

@SV2Qualifier
@Binds
@Singleton
fun sV2(impl: SV2): BuildVersionWrapper
fun buildVersionWrapper(impl: DefaultBuildVersionWrapper): BuildVersionWrapper
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ package com.android.geto.core.buildversion

import android.os.Build
import javax.inject.Inject
import javax.inject.Qualifier

internal class SV2 @Inject constructor() : BuildVersionWrapper {
internal class DefaultBuildVersionWrapper @Inject constructor() : BuildVersionWrapper {
override fun getSDKInt(): Int = Build.VERSION.SDK_INT
}

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class SV2Qualifier
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class FakeShortcutRepository @Inject constructor() : ShortcutRepository {
return false
}

override fun updateRequestPinShortcut(
override fun updateShortcuts(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): Boolean {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ internal class DefaultShortcutRepository @Inject constructor(
)
}

override fun updateRequestPinShortcut(
override fun updateShortcuts(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): Boolean {
return shortcutManagerCompatWrapper.updateShortcuts(
packageName = packageName,
appName = appName,
mappedShortcutInfoCompat = mappedShortcutInfoCompat,
shortcuts = shortcuts,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ interface ShortcutRepository {
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
): Boolean

fun updateRequestPinShortcut(
fun updateShortcuts(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): Boolean

fun getPinnedShortcuts(): List<MappedShortcutInfoCompat>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,30 @@ class ShortcutRepositoryTest {
}

@Test
fun updateRequestPinShortcut_isTrue() {
fun updateShortcuts_isTrue() {
shortcutManagerCompatWrapper.setUpdateImmutableShortcuts(false)

shortcutManagerCompatWrapper.setRequestPinShortcutSupported(true)

shortcutManagerCompatWrapper.setShortcuts(shortcuts)

assertTrue(
subject.updateRequestPinShortcut(
subject.updateShortcuts(
packageName = "com.android.geto",
appName = "Geto",
mappedShortcutInfoCompat = MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "Geto",
longLabel = "Geto",
shortcuts = listOf(
MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "Geto",
longLabel = "Geto",
),
),
),
)
}

@Test
fun updateRequestPinShortcut_throwsIllegalArgumentException() {
fun updateShortcuts_throwsIllegalArgumentException() {
shortcutManagerCompatWrapper.setUpdateImmutableShortcuts(true)

shortcutManagerCompatWrapper.setRequestPinShortcutSupported(false)
Expand All @@ -117,13 +119,15 @@ class ShortcutRepositoryTest {
assertFailsWith(
exceptionClass = IllegalArgumentException::class,
block = {
subject.updateRequestPinShortcut(
subject.updateShortcuts(
packageName = "com.android.geto",
appName = "Geto",
mappedShortcutInfoCompat = MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "Geto",
longLabel = "Geto",
shortcuts = listOf(
MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "Geto",
longLabel = "Geto",
),
),
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestShortcutManagerCompatWrapper : ShortcutManagerCompatWrapper {
override fun updateShortcuts(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): Boolean {
return if (updateImmutableShortcuts) {
throw IllegalArgumentException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ package com.android.geto.core.domain

import android.os.Build
import com.android.geto.core.buildversion.BuildVersionWrapper
import com.android.geto.core.buildversion.SV2Qualifier
import com.android.geto.core.data.repository.ClipboardRepository
import javax.inject.Inject

class SetPrimaryClipUseCase @Inject constructor(
private val clipboardRepository: ClipboardRepository,
@SV2Qualifier private val buildVersionWrapper: BuildVersionWrapper,
private val buildVersionWrapper: BuildVersionWrapper,
) {

operator fun invoke(label: String, text: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ class UpdateRequestPinShortcutUseCase @Inject constructor(private val shortcutRe
operator fun invoke(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): UpdateRequestPinShortcutResult {
val shortcutIds = shortcutRepository.getPinnedShortcuts().map { it.id }
val oldMappedShortcutInfoCompatIds = shortcutRepository.getPinnedShortcuts().map { it.id }

if (shortcutIds.contains(mappedShortcutInfoCompat.id).not()) {
val newMappedShortcutInfoCompatIds = shortcuts.map { it.id }

if (oldMappedShortcutInfoCompatIds.any { it !in newMappedShortcutInfoCompatIds }) {
return UpdateRequestPinShortcutResult.IDNotFound
}

return try {
if (shortcutRepository.updateRequestPinShortcut(
if (shortcutRepository.updateShortcuts(
packageName = packageName,
appName = appName,
mappedShortcutInfoCompat = mappedShortcutInfoCompat,
shortcuts = shortcuts,
)
) {
UpdateRequestPinShortcutResult.Success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class SetPrimaryClipUseCaseTest {

@Test
fun setPrimaryClipUseCase_isFalse_whenBuildVersion_isHigherThan32() = runTest {
buildVersionWrapper.setSdkInt(33)
buildVersionWrapper.setSDKInt(33)

assertFalse(setPrimaryClipUseCase(label = "Label", text = "Text"))
}

@Test
fun setPrimaryClipUseCase_isTrue_whenBuildVersion_is32AndLower() = runTest {
buildVersionWrapper.setSdkInt(31)
buildVersionWrapper.setSDKInt(31)

assertTrue(setPrimaryClipUseCase(label = "Label", text = "Text"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ class UpdateRequestPinShortcutUseCaseTest {
updateRequestPinShortcutUseCase(
packageName = "com.android.geto",
appName = "Geto",
mappedShortcutInfoCompat = MappedShortcutInfoCompat(
id = "0",
shortLabel = "shortLabel",
longLabel = "longLabel",
shortcuts = listOf(
MappedShortcutInfoCompat(
id = "0",
shortLabel = "shortLabel",
longLabel = "longLabel",
),
),
),
)
Expand All @@ -82,10 +84,12 @@ class UpdateRequestPinShortcutUseCaseTest {
updateRequestPinShortcutUseCase(
packageName = "com.android.geto",
appName = "Geto",
mappedShortcutInfoCompat = MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "shortLabel",
longLabel = "longLabel",
shortcuts = listOf(
MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "shortLabel",
longLabel = "longLabel",
),
),
),
)
Expand All @@ -111,10 +115,12 @@ class UpdateRequestPinShortcutUseCaseTest {
updateRequestPinShortcutUseCase(
packageName = "com.android.geto",
appName = "Geto",
mappedShortcutInfoCompat = MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "shortLabel",
longLabel = "longLabel",
shortcuts = listOf(
MappedShortcutInfoCompat(
id = "com.android.geto",
shortLabel = "shortLabel",
longLabel = "longLabel",
),
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ internal class DefaultShortcutManagerCompatWrapper @Inject constructor(@Applicat
override fun updateShortcuts(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): Boolean {
return ShortcutManagerCompat.updateShortcuts(
context,
listOf(
shortcuts.map { mappedShortcutInfoCompat ->
mappedShortcutInfoCompat.asShortcutInfoCompat(
context = context,
packageName = packageName,
appName = appName,
),
),
)
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface ShortcutManagerCompatWrapper {
fun updateShortcuts(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): Boolean

fun getShortcuts(matchFlags: Int): List<MappedShortcutInfoCompat>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TestBuildVersionWrapper : BuildVersionWrapper {

override fun getSDKInt(): Int = _sdkInt

fun setSdkInt(value: Int) {
fun setSDKInt(value: Int) {
_sdkInt = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class TestShortcutRepository : ShortcutRepository {
return requestPinShortcutSupported
}

override fun updateRequestPinShortcut(
override fun updateShortcuts(
packageName: String,
appName: String,
mappedShortcutInfoCompat: MappedShortcutInfoCompat,
shortcuts: List<MappedShortcutInfoCompat>,
): Boolean {
return if (updateImmutableShortcuts) {
throw IllegalArgumentException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class AppSettingsViewModel @Inject constructor(
updateRequestPinShortcutUseCase(
packageName = packageName,
appName = appName,
mappedShortcutInfoCompat,
shortcuts = listOf(mappedShortcutInfoCompat),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class AppSettingsViewModelTest {
}

@Test
fun applyAppSettingsResult_isAppSettingsDisabled_whenApplySettings() = runTest {
fun applyAppSettingsResult_isDisabledAppSettings_whenApplySettings() = runTest {
val appSettings = List(5) { index ->
AppSetting(
id = index,
Expand Down Expand Up @@ -422,7 +422,7 @@ class AppSettingsViewModelTest {
}

@Test
fun revertAppSettingsResult_isAppSettingsDisabled_whenRevertSettings() = runTest {
fun revertAppSettingsResult_isDisabledAppSettings_whenRevertSettings() = runTest {
val appSettings = List(5) { index ->
AppSetting(
id = index,
Expand All @@ -447,7 +447,7 @@ class AppSettingsViewModelTest {

@Test
fun setPrimaryClipResult_isFalse_whenCopyPermissionCommand() = runTest {
buildVersionWrapper.setSdkInt(33)
buildVersionWrapper.setSDKInt(33)

viewModel.copyPermissionCommand()

Expand All @@ -459,7 +459,7 @@ class AppSettingsViewModelTest {

@Test
fun setPrimaryClipResult_isTrue_whenCopyPermissionCommand() = runTest {
buildVersionWrapper.setSdkInt(32)
buildVersionWrapper.setSDKInt(32)

viewModel.copyPermissionCommand()

Expand Down

0 comments on commit 63098a9

Please sign in to comment.