Skip to content

Commit

Permalink
Fixed crashing because of torrent monitoring (#394)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LivingWithHippos authored Sep 26, 2024
1 parent 00c18bb commit 71343b7
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 59 deletions.
7 changes: 4 additions & 3 deletions app/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -148,6 +148,7 @@ android {


release {
ndk.debugSymbolLevel = "FULL"
signingConfig = signingConfigs.getByName("release")
isDebuggable = false
isMinifyEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -248,13 +248,21 @@ class MainActivity : AppCompatActivity() {
FSMAuthenticationState.AuthenticatedOpenToken -> {
// unlock the bottom menu
enableAllBottomNavItems()
if (!checkedUpdate) {
checkedUpdate = true
viewModel.checkUpdates(BuildConfig.VERSION_CODE, getApplicationSignatures())
}
}
FSMAuthenticationState.RefreshingOpenToken -> {
viewModel.refreshToken()
}
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
Expand Down Expand Up @@ -601,8 +609,6 @@ class MainActivity : AppCompatActivity() {
}

viewModel.clearCache(applicationContext.cacheDir)

viewModel.checkUpdates(BuildConfig.VERSION_CODE, getApplicationSignatures())
}

override fun onResume() {
Expand Down Expand Up @@ -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
Expand All @@ -712,7 +717,7 @@ class MainActivity : AppCompatActivity() {
}
CurrentFSMAuthentication.Waiting -> {
// auth may become ok, delay and continue loop
delay(AUTH_DELAY)
delay(100)
}
}
}
Expand Down Expand Up @@ -880,6 +885,5 @@ class MainActivity : AppCompatActivity() {

companion object {
private const val EXIT_WAIT_TIME = 2000L
private const val AUTH_DELAY = 500L
}
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ constructor(

val fsmAuthenticationState = MutableLiveData<Event<FSMAuthenticationState>?>()

val userLiveData = MutableLiveData<Event<User>>()

val externalLinkLiveData = MutableLiveData<Event<Uri>>()

val downloadedFileLiveData = MutableLiveData<Event<Long>>()
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -480,6 +501,7 @@ constructor(
) {
when (user) {
is EitherResult.Success -> {
setCachedUser(user.success)
if (isPrivateToken) {
transitionAuthenticationMachine(FSMAuthenticationEvent.OnWorkingPrivateToken)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion app/app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
<item quantity="other">%d seeders</item>
</plurals>
<string name="seeders_string_format">seeder/s: %s</string>
<string name="torrent_in_progress_format">%1$d%% - %2$.2f MB/s</string>
<string name="torrent_in_progress_format">%1$.2f%% - %2$.2f MB/s</string>
<string name="download_in_progress_format">Descargando - %1$d%%</string>
<string name="download_complete">Descarga completa</string>
<string name="unknown_status">Estado desconocido</string>
Expand Down
2 changes: 1 addition & 1 deletion app/app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
<item quantity="many">%d seeders</item>
</plurals>
<string name="seeders_string_format">seeder/s: %s</string>
<string name="torrent_in_progress_format">%1$d%% - %2$.2f Mo/s</string>
<string name="torrent_in_progress_format">%1$.2f%% - %2$.2f Mo/s</string>
<string name="download_in_progress_format">Téléchargement - %1$d%%</string>
<string name="download_complete">Téléchargement terminé</string>
<string name="unknown_status">État inconnu</string>
Expand Down
2 changes: 1 addition & 1 deletion app/app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
<item quantity="other">%d seeders</item>
</plurals>
<string name="seeders_string_format">seeder/s: %s</string>
<string name="torrent_in_progress_format">%1$d%% - %2$.2f MB/s</string>
<string name="torrent_in_progress_format">%1$.2f%% - %2$.2f MB/s</string>
<string name="download_in_progress_format">Downloading - %1$d%%</string>
<string name="download_complete">Download completato</string>
<string name="unknown_status">Stato sconosciuto</string>
Expand Down
2 changes: 1 addition & 1 deletion app/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@
<item quantity="other">%d seeders</item>
</plurals>
<string name="seeders_string_format">seeder/s: %s</string>
<string name="torrent_in_progress_format">%1$d%% - %2$.2f MB/s</string>
<string name="torrent_in_progress_format">%1$.2f%% - %2$.2f MB/s</string>
<string name="download_in_progress_format">Downloading - %1$d%%</string>
<string name="download_complete">Download complete</string>
<string name="unknown_status">Unknown Status</string>
Expand Down
2 changes: 1 addition & 1 deletion app/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 71343b7

Please sign in to comment.