Skip to content

Commit 7081415

Browse files
committed
refactor: import feature to resolve deprecations
1 parent b00659e commit 7081415

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/SettingsImportModule.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import app.k9mail.feature.settings.import.ui.AuthViewModel
44
import app.k9mail.feature.settings.import.ui.ImportAppFetcher
55
import app.k9mail.feature.settings.import.ui.PickAppViewModel
66
import app.k9mail.feature.settings.import.ui.SettingsImportViewModel
7+
import org.koin.android.ext.koin.androidContext
78
import org.koin.core.module.dsl.viewModel
89
import org.koin.dsl.module
910

1011
val featureSettingsImportModule = module {
11-
factory { ImportAppFetcher(context = get()) }
12+
factory {
13+
ImportAppFetcher(
14+
context = androidContext(),
15+
logger = get(),
16+
)
17+
}
1218

1319
viewModel {
1420
SettingsImportViewModel(
@@ -17,6 +23,7 @@ val featureSettingsImportModule = module {
1723
accountActivator = get(),
1824
migrationManager = get(),
1925
importAppFetcher = get(),
26+
logger = get(),
2027
)
2128
}
2229

@@ -25,6 +32,7 @@ val featureSettingsImportModule = module {
2532
application = get(),
2633
accountManager = get(),
2734
getOAuthRequestIntent = get(),
35+
logger = get(),
2836
)
2937
}
3038

feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/ui/AuthViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ import net.openid.appauth.AuthorizationResponse
2828
import net.openid.appauth.AuthorizationService
2929
import net.thunderbird.core.android.account.LegacyAccountDto
3030
import net.thunderbird.core.android.account.LegacyAccountDtoManager
31+
import net.thunderbird.core.logging.Logger
3132

3233
private const val KEY_AUTHORIZATION = "app.k9mail_auth"
34+
private const val TAG = "AuthViewModel"
3335

3436
@Suppress("TooManyFunctions")
3537
internal class AuthViewModel(
3638
application: Application,
3739
private val accountManager: LegacyAccountDtoManager,
3840
private val getOAuthRequestIntent: GetOAuthRequestIntent,
41+
private val logger: Logger,
3942
) : AndroidViewModel(application) {
4043
private var authService: AuthorizationService? = null
4144
private val authState = AuthState()
@@ -73,14 +76,15 @@ internal class AuthViewModel(
7376
try {
7477
startLogin(account)
7578
} catch (e: ActivityNotFoundException) {
79+
logger.error(TAG, e) { "No browser found to start OAuth login flow." }
7680
_uiState.update { AuthFlowState.BrowserNotFound }
7781
}
7882
}
7983
}
8084

8185
private suspend fun startLogin(account: LegacyAccountDto) {
8286
val authRequestIntentResult = withContext(Dispatchers.IO) {
83-
getOAuthRequestIntent.execute(account.incomingServerSettings.host!!, account.email)
87+
getOAuthRequestIntent.execute(account.incomingServerSettings.host, account.email)
8488
}
8589

8690
when (authRequestIntentResult) {

feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/ui/ImportAppFetcher.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ package app.k9mail.feature.settings.import.ui
33
import android.content.Context
44
import android.content.pm.PackageManager
55
import androidx.annotation.WorkerThread
6+
import androidx.core.content.pm.PackageInfoCompat
7+
import net.thunderbird.core.logging.Logger
8+
9+
private const val TAG = "ImportAppFetcher"
610

711
internal class ImportAppFetcher(
812
private val context: Context,
13+
private val logger: Logger,
914
) {
1015
private val packageManager by lazy { context.packageManager }
1116

@@ -23,6 +28,7 @@ internal class ImportAppFetcher(
2328
getApplicationInfo(packageName, 0)
2429
true
2530
} catch (e: PackageManager.NameNotFoundException) {
31+
logger.error(TAG, e) { "App with package name $packageName is not installed." }
2632
false
2733
}
2834
}
@@ -37,17 +43,17 @@ internal class ImportAppFetcher(
3743
.toList()
3844
}
3945

40-
@Suppress("SwallowedException")
4146
private fun PackageManager.loadAppInfo(app: AppVersion): AppInfo? {
4247
return try {
4348
val packageInfo = getPackageInfo(app.packageName, 0)
44-
val isImportSupported = packageInfo.versionCode >= app.minVersionCode
49+
val isImportSupported = PackageInfoCompat.getLongVersionCode(packageInfo) >= app.minVersionCode
4550

4651
val applicationInfo = getApplicationInfo(app.packageName, 0)
4752
val appName = packageManager.getApplicationLabel(applicationInfo).toString()
4853

4954
AppInfo(app.packageName, appName, isImportSupported)
5055
} catch (e: PackageManager.NameNotFoundException) {
56+
logger.error(TAG, e) { "Failed to load app info for package ${app.packageName}." }
5157
null
5258
}
5359
}

feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/ui/SettingsImportViewModel.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import kotlinx.coroutines.delay
2828
import kotlinx.coroutines.launch
2929
import kotlinx.coroutines.withContext
3030
import kotlinx.parcelize.Parcelize
31-
import net.thunderbird.core.logging.legacy.Log
31+
import net.thunderbird.core.logging.Logger
3232

3333
private typealias AccountUuid = String
3434
private typealias AccountNumber = Int
35+
private const val TAG = "SettingsImportViewModel"
3536

3637
internal class SettingsImportViewModel(
3738
private val contentResolver: ContentResolver,
@@ -40,6 +41,7 @@ internal class SettingsImportViewModel(
4041
private val migrationManager: MigrationManager,
4142
private val importAppFetcher: ImportAppFetcher,
4243
private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.IO,
44+
private val logger: Logger,
4345
viewModelScope: CoroutineScope = CoroutineScope(Dispatchers.Main.immediate + SupervisorJob()),
4446
) : ViewModel(viewModelScope) {
4547
private val uiModelLiveData = MutableLiveData<SettingsImportUiModel>()
@@ -153,7 +155,11 @@ internal class SettingsImportViewModel(
153155
null
154156
}
155157

156-
val savedAccountList = savedInstanceState.getParcelableArrayList<SavedAccountState>(STATE_ACCOUNT_LIST)!!
158+
val savedAccountList = BundleCompat.getParcelableArrayList(
159+
savedInstanceState,
160+
STATE_ACCOUNT_LIST,
161+
SavedAccountState::class.java,
162+
) ?: error("Saved account list is missing")
157163

158164
savedAccountList.forEach { saved ->
159165
accountsMap[saved.accountIndex] = saved.accountUuid
@@ -387,7 +393,7 @@ internal class SettingsImportViewModel(
387393
initializeSettingsList(items)
388394
}
389395
} catch (e: Exception) {
390-
Log.e(e, "Error reading settings file")
396+
logger.error(TAG, e) { "Error reading settings file" }
391397

392398
updateUiModel {
393399
showReadFailureText()
@@ -423,7 +429,7 @@ internal class SettingsImportViewModel(
423429
updateCloseButtonAndImportStatusText()
424430
}
425431
} catch (e: Exception) {
426-
Log.e(e, "Error importing settings")
432+
logger.error(TAG, e) { "Error importing settings" }
427433

428434
updateUiModel {
429435
showImportErrorText()

feature/settings/import/src/test/kotlin/app/k9mail/feature/settings/import/ui/ImportAppFetcherTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import assertk.assertions.isEmpty
1212
import assertk.assertions.isFalse
1313
import assertk.assertions.isTrue
1414
import kotlin.test.Test
15+
import net.thunderbird.core.logging.testing.TestLogger
1516
import org.junit.runner.RunWith
1617
import org.robolectric.RobolectricTestRunner
1718
import org.robolectric.Shadows.shadowOf
@@ -24,7 +25,10 @@ private const val MY_PACKAGE_NAME = "net.thunderbird.android"
2425
class ImportAppFetcherTest {
2526
private val appContext = ApplicationProvider.getApplicationContext<Context>()
2627
private val context = createPackageContext()
27-
private val importAppFetcher = ImportAppFetcher(context)
28+
private val importAppFetcher = ImportAppFetcher(
29+
context,
30+
logger = TestLogger(),
31+
)
2832

2933
@Test
3034
fun `isAtLeastOneAppInstalled() without any other apps installed`() {
@@ -102,6 +106,7 @@ class ImportAppFetcherTest {
102106
this.applicationInfo = ApplicationInfo().apply {
103107
this.name = name
104108
}
109+
@Suppress("DEPRECATION")
105110
this.versionCode = versionCode
106111
},
107112
)

feature/settings/import/src/test/kotlin/app/k9mail/feature/settings/import/ui/SettingsImportViewModelTest.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app.k9mail.feature.settings.import.ui
33
import android.app.Application
44
import android.content.ContentResolver
55
import androidx.core.net.toUri
6+
import androidx.lifecycle.viewmodel.compose.viewModel
67
import app.k9mail.feature.settings.import.SettingsImportExternalContract.AccountActivator
78
import assertk.all
89
import assertk.assertThat
@@ -22,9 +23,7 @@ import com.fsck.k9.preferences.SettingsImporter
2223
import kotlin.test.Test
2324
import kotlinx.coroutines.Dispatchers
2425
import kotlinx.coroutines.test.TestScope
25-
import net.thunderbird.core.logging.legacy.Log
2626
import net.thunderbird.core.logging.testing.TestLogger
27-
import org.junit.Before
2827
import org.junit.runner.RunWith
2928
import org.mockito.kotlin.doReturn
3029
import org.mockito.kotlin.doThrow
@@ -50,13 +49,9 @@ class SettingsImportViewModelTest {
5049
importAppFetcher = importAppFetcher,
5150
backgroundDispatcher = Dispatchers.Unconfined,
5251
viewModelScope = testScope,
52+
logger = TestLogger(),
5353
)
5454

55-
@Before
56-
fun setUp() {
57-
Log.logger = TestLogger()
58-
}
59-
6055
@Test
6156
fun `scanQrCodeButton and pickAppButton should be hidden when migration feature is disabled`() {
6257
migrationManager.featureIncluded = false

0 commit comments

Comments
 (0)