From 71343b7da761ad89641b2a2d5e7f3e953c218d32 Mon Sep 17 00:00:00 2001 From: LivingWithHippos Date: Thu, 26 Sep 2024 23:30:06 +0200 Subject: [PATCH] Fixed crashing because of torrent monitoring (#394) * fixed wrong int -> float formatting in torrent notification see #393 * Update libs.versions.toml * bumped version for release * updated symbols for debugging in release * fixed wrong version file link * better new version check Beta * user data caching * formatted code --- app/app/build.gradle.kts | 7 +++-- .../unchained/base/MainActivity.kt | 20 ++++++++----- .../unchained/data/model/User.kt | 5 +++- .../start/viewmodel/MainActivityViewModel.kt | 22 ++++++++++++++ .../user/view/UserProfileFragment.kt | 22 +++++++------- .../user/viewmodel/UserProfileViewModel.kt | 29 ------------------- .../unchained/utilities/Constants.kt | 2 +- app/app/src/main/res/values-es/strings.xml | 2 +- app/app/src/main/res/values-fr/strings.xml | 2 +- app/app/src/main/res/values-it/strings.xml | 2 +- app/app/src/main/res/values/strings.xml | 2 +- app/gradle/libs.versions.toml | 2 +- 12 files changed, 58 insertions(+), 59 deletions(-) delete mode 100644 app/app/src/main/java/com/github/livingwithhippos/unchained/user/viewmodel/UserProfileViewModel.kt diff --git a/app/app/build.gradle.kts b/app/app/build.gradle.kts index cdd57fbd..0826e4e7 100644 --- a/app/app/build.gradle.kts +++ b/app/app/build.gradle.kts @@ -60,8 +60,8 @@ android { applicationId = "com.github.livingwithhippos.unchained" minSdk = 22 targetSdk = 34 - versionCode = 46 - versionName = "1.3.2" + versionCode = 47 + versionName = "1.3.3" javaCompileOptions { annotationProcessorOptions { @@ -96,7 +96,7 @@ android { // use local file if available or Environment variables (for CI) create("release") { if (keyPropertiesFile.exists()) { - storeFile = keyPropertiesFile + storeFile = file(keyProperties["store"] as String? ?: "release.pfk") storePassword = keyProperties["releaseStorePassword"] as String keyAlias = keyProperties["keyAlias"] as String keyPassword = keyProperties["releaseStorePassword"] as String @@ -148,6 +148,7 @@ android { release { + ndk.debugSymbolLevel = "FULL" signingConfig = signingConfigs.getByName("release") isDebuggable = false isMinifyEnabled = true diff --git a/app/app/src/main/java/com/github/livingwithhippos/unchained/base/MainActivity.kt b/app/app/src/main/java/com/github/livingwithhippos/unchained/base/MainActivity.kt index c9b95303..f208aeff 100644 --- a/app/app/src/main/java/com/github/livingwithhippos/unchained/base/MainActivity.kt +++ b/app/app/src/main/java/com/github/livingwithhippos/unchained/base/MainActivity.kt @@ -77,6 +77,7 @@ class MainActivity : AppCompatActivity() { private lateinit var navController: NavController private lateinit var appBarConfiguration: AppBarConfiguration + private var checkedUpdate: Boolean = false // Countly crash reporter set up. Debug mode only override fun onStart() { @@ -230,8 +231,7 @@ class MainActivity : AppCompatActivity() { viewModel.fsmAuthenticationState.observe(this) { // do not inline this variable in the when, because getContentIfNotHandled() will change - // its - // value to null if checked again in WaitingUserAction + // its value to null if checked again in WaitingUserAction when (val authState: FSMAuthenticationState? = it?.getContentIfNotHandled()) { null -> { // do nothing @@ -248,6 +248,10 @@ class MainActivity : AppCompatActivity() { FSMAuthenticationState.AuthenticatedOpenToken -> { // unlock the bottom menu enableAllBottomNavItems() + if (!checkedUpdate) { + checkedUpdate = true + viewModel.checkUpdates(BuildConfig.VERSION_CODE, getApplicationSignatures()) + } } FSMAuthenticationState.RefreshingOpenToken -> { viewModel.refreshToken() @@ -255,6 +259,10 @@ class MainActivity : AppCompatActivity() { FSMAuthenticationState.AuthenticatedPrivateToken -> { // unlock the bottom menu enableAllBottomNavItems() + if (!checkedUpdate) { + checkedUpdate = true + viewModel.checkUpdates(BuildConfig.VERSION_CODE, getApplicationSignatures()) + } } FSMAuthenticationState.WaitingToken -> { // this state should be managed by the fragments directly @@ -601,8 +609,6 @@ class MainActivity : AppCompatActivity() { } viewModel.clearCache(applicationContext.cacheDir) - - viewModel.checkUpdates(BuildConfig.VERSION_CODE, getApplicationSignatures()) } override fun onResume() { @@ -697,8 +703,7 @@ class MainActivity : AppCompatActivity() { private fun processExternalRequestOnAuthentication(uri: Uri) { lifecycleScope.launch { // avoid some issues with the authentication state machine being too late - delay(AUTH_DELAY) - delayLoop@ for (loop in 1..5) { + delayLoop@ for (loop in 1..100) { when (viewModel.getCurrentAuthenticationStatus()) { CurrentFSMAuthentication.Authenticated -> { // auth ok, process link and exit loop @@ -712,7 +717,7 @@ class MainActivity : AppCompatActivity() { } CurrentFSMAuthentication.Waiting -> { // auth may become ok, delay and continue loop - delay(AUTH_DELAY) + delay(100) } } } @@ -880,6 +885,5 @@ class MainActivity : AppCompatActivity() { companion object { private const val EXIT_WAIT_TIME = 2000L - private const val AUTH_DELAY = 500L } } diff --git a/app/app/src/main/java/com/github/livingwithhippos/unchained/data/model/User.kt b/app/app/src/main/java/com/github/livingwithhippos/unchained/data/model/User.kt index 2133cb8e..e9b98bc0 100644 --- a/app/app/src/main/java/com/github/livingwithhippos/unchained/data/model/User.kt +++ b/app/app/src/main/java/com/github/livingwithhippos/unchained/data/model/User.kt @@ -1,11 +1,14 @@ package com.github.livingwithhippos.unchained.data.model +import android.os.Parcelable import androidx.annotation.Keep import com.squareup.moshi.Json import com.squareup.moshi.JsonClass +import kotlinx.parcelize.Parcelize @Keep @JsonClass(generateAdapter = true) +@Parcelize data class User( @Json(name = "id") val id: Int, @Json(name = "username") val username: String, @@ -16,4 +19,4 @@ data class User( @Json(name = "type") val type: String, @Json(name = "premium") val premium: Int, @Json(name = "expiration") val expiration: String, -) +) : Parcelable diff --git a/app/app/src/main/java/com/github/livingwithhippos/unchained/start/viewmodel/MainActivityViewModel.kt b/app/app/src/main/java/com/github/livingwithhippos/unchained/start/viewmodel/MainActivityViewModel.kt index 24de61b8..5db154dc 100644 --- a/app/app/src/main/java/com/github/livingwithhippos/unchained/start/viewmodel/MainActivityViewModel.kt +++ b/app/app/src/main/java/com/github/livingwithhippos/unchained/start/viewmodel/MainActivityViewModel.kt @@ -110,6 +110,8 @@ constructor( val fsmAuthenticationState = MutableLiveData?>() + val userLiveData = MutableLiveData>() + val externalLinkLiveData = MutableLiveData>() val downloadedFileLiveData = MutableLiveData>() @@ -442,6 +444,25 @@ constructor( savedStateHandle[SearchViewModel.KEY_CACHE] = cache } + private fun setCachedUser(user: User?) { + savedStateHandle["current_user_key"] = user + } + + fun getCachedUser(): User? { + return savedStateHandle["current_user_key"] + } + + fun fetchUser() { + viewModelScope.launch { + val credentials = protoStore.getCredentials() + val user = userRepository.getUserInfo(credentials.accessToken) + if (user != null) { + setCachedUser(user) + userLiveData.postEvent(user) + } + } + } + /** * Set current torrent cache pick, see TorrentCachePicker * @@ -480,6 +501,7 @@ constructor( ) { when (user) { is EitherResult.Success -> { + setCachedUser(user.success) if (isPrivateToken) { transitionAuthenticationMachine(FSMAuthenticationEvent.OnWorkingPrivateToken) } else { diff --git a/app/app/src/main/java/com/github/livingwithhippos/unchained/user/view/UserProfileFragment.kt b/app/app/src/main/java/com/github/livingwithhippos/unchained/user/view/UserProfileFragment.kt index e249b499..b364a141 100644 --- a/app/app/src/main/java/com/github/livingwithhippos/unchained/user/view/UserProfileFragment.kt +++ b/app/app/src/main/java/com/github/livingwithhippos/unchained/user/view/UserProfileFragment.kt @@ -10,7 +10,6 @@ import android.view.View import android.view.ViewGroup import androidx.core.content.ContextCompat import androidx.core.content.PermissionChecker -import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import com.github.livingwithhippos.unchained.R @@ -20,7 +19,6 @@ import com.github.livingwithhippos.unchained.settings.view.SettingsActivity import com.github.livingwithhippos.unchained.settings.view.SettingsFragment.Companion.KEY_REFERRAL_ASKED import com.github.livingwithhippos.unchained.settings.view.SettingsFragment.Companion.KEY_REFERRAL_USE import com.github.livingwithhippos.unchained.statemachine.authentication.FSMAuthenticationState -import com.github.livingwithhippos.unchained.user.viewmodel.UserProfileViewModel import com.github.livingwithhippos.unchained.utilities.ACCOUNT_LINK import com.github.livingwithhippos.unchained.utilities.REFERRAL_LINK import com.github.livingwithhippos.unchained.utilities.extension.openExternalWebPage @@ -33,8 +31,6 @@ import kotlinx.coroutines.launch @AndroidEntryPoint class UserProfileFragment : UnchainedFragment() { - private val viewModel: UserProfileViewModel by viewModels() - @Inject lateinit var preferences: SharedPreferences override fun onCreateView( @@ -45,15 +41,17 @@ class UserProfileFragment : UnchainedFragment() { // Inflate the layout for this fragment val userBinding = FragmentUserProfileBinding.inflate(inflater, container, false) - viewModel.fetchUserInfo() + val user = activityViewModel.getCachedUser() + if (user == null) { + activityViewModel.fetchUser() + } else { + userBinding.user = user + } + lifecycleScope.launch { userBinding.privateToken = activityViewModel.isTokenPrivate() } - viewModel.userLiveData.observe(viewLifecycleOwner) { - if (it != null) { - userBinding.user = it - lifecycleScope.launch { - userBinding.privateToken = activityViewModel.isTokenPrivate() - } - } + activityViewModel.userLiveData.observe(viewLifecycleOwner) { + userBinding.user = it.peekContent() + lifecycleScope.launch { userBinding.privateToken = activityViewModel.isTokenPrivate() } } userBinding.bAccount.setOnClickListener { diff --git a/app/app/src/main/java/com/github/livingwithhippos/unchained/user/viewmodel/UserProfileViewModel.kt b/app/app/src/main/java/com/github/livingwithhippos/unchained/user/viewmodel/UserProfileViewModel.kt deleted file mode 100644 index e1c257c4..00000000 --- a/app/app/src/main/java/com/github/livingwithhippos/unchained/user/viewmodel/UserProfileViewModel.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.livingwithhippos.unchained.user.viewmodel - -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope -import com.github.livingwithhippos.unchained.data.local.ProtoStore -import com.github.livingwithhippos.unchained.data.model.User -import com.github.livingwithhippos.unchained.data.repository.UserRepository -import dagger.hilt.android.lifecycle.HiltViewModel -import javax.inject.Inject -import kotlinx.coroutines.launch - -@HiltViewModel -class UserProfileViewModel -@Inject -constructor(private val userRepository: UserRepository, private val protoStore: ProtoStore) : - ViewModel() { - - val userLiveData = MutableLiveData() - - fun fetchUserInfo() { - - viewModelScope.launch { - val credentials = protoStore.getCredentials() - val user = userRepository.getUserInfo(credentials.accessToken) - userLiveData.postValue(user) - } - } -} diff --git a/app/app/src/main/java/com/github/livingwithhippos/unchained/utilities/Constants.kt b/app/app/src/main/java/com/github/livingwithhippos/unchained/utilities/Constants.kt index 44f2189d..3f98dbb8 100644 --- a/app/app/src/main/java/com/github/livingwithhippos/unchained/utilities/Constants.kt +++ b/app/app/src/main/java/com/github/livingwithhippos/unchained/utilities/Constants.kt @@ -99,7 +99,7 @@ const val TORRENTS_TAB = 1 object SIGNATURE { const val URL = - "https://gist.githubusercontent.com/LivingWithHippos/5525e73f0439d06c1c3ff4f9484e35dd/raw/f97b79e706aa67d729806039d49f80aba4042793/unchained_versions.json" + "https://gist.githubusercontent.com/LivingWithHippos/5525e73f0439d06c1c3ff4f9484e35dd/raw/unchained_versions.json" const val PLAY_STORE = "31F17448AA3888B63ED04EB5F965E3F70C12592F" const val F_DROID = "412DABCABBDB75A82FF66F767C71EE045C02275B" const val GITHUB = "0E7BE3FA6B47C20394517C568570E10761A0A4FA" diff --git a/app/app/src/main/res/values-es/strings.xml b/app/app/src/main/res/values-es/strings.xml index f5a58d58..75c88b09 100644 --- a/app/app/src/main/res/values-es/strings.xml +++ b/app/app/src/main/res/values-es/strings.xml @@ -209,7 +209,7 @@ %d seeders seeder/s: %s - %1$d%% - %2$.2f MB/s + %1$.2f%% - %2$.2f MB/s Descargando - %1$d%% Descarga completa Estado desconocido diff --git a/app/app/src/main/res/values-fr/strings.xml b/app/app/src/main/res/values-fr/strings.xml index a967de3b..b0ae4cbe 100644 --- a/app/app/src/main/res/values-fr/strings.xml +++ b/app/app/src/main/res/values-fr/strings.xml @@ -209,7 +209,7 @@ %d seeders seeder/s: %s - %1$d%% - %2$.2f Mo/s + %1$.2f%% - %2$.2f Mo/s Téléchargement - %1$d%% Téléchargement terminé État inconnu diff --git a/app/app/src/main/res/values-it/strings.xml b/app/app/src/main/res/values-it/strings.xml index 243327e2..e91451bc 100644 --- a/app/app/src/main/res/values-it/strings.xml +++ b/app/app/src/main/res/values-it/strings.xml @@ -209,7 +209,7 @@ %d seeders seeder/s: %s - %1$d%% - %2$.2f MB/s + %1$.2f%% - %2$.2f MB/s Downloading - %1$d%% Download completato Stato sconosciuto diff --git a/app/app/src/main/res/values/strings.xml b/app/app/src/main/res/values/strings.xml index 75aaeef5..089b2baa 100644 --- a/app/app/src/main/res/values/strings.xml +++ b/app/app/src/main/res/values/strings.xml @@ -419,7 +419,7 @@ %d seeders seeder/s: %s - %1$d%% - %2$.2f MB/s + %1$.2f%% - %2$.2f MB/s Downloading - %1$d%% Download complete Unknown Status diff --git a/app/gradle/libs.versions.toml b/app/gradle/libs.versions.toml index 725eb3c2..1fc975d0 100644 --- a/app/gradle/libs.versions.toml +++ b/app/gradle/libs.versions.toml @@ -1,6 +1,6 @@ [versions] aboutlibraries = "11.2.3" -android_gradle_plugin = "8.6.0" +android_gradle_plugin = "8.6.1" appcompat = "1.7.0" constraint_layout = "2.1.4" coil = "2.7.0"