Skip to content

Commit

Permalink
[优化] 登入后遇到特殊状况不再自动登出
Browse files Browse the repository at this point in the history
[修复] 主页面取色可能导致的崩溃问题
  • Loading branch information
YenalyLiew committed Oct 14, 2024
1 parent 6090921 commit 47df8f2
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 17 deletions.
3 changes: 0 additions & 3 deletions app/src/main/java/com/yenaly/han1meviewer/HanimeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.itxca.spannablex.spannable
import com.yenaly.han1meviewer.Preferences.isAlreadyLogin
import com.yenaly.han1meviewer.Preferences.loginCookie
import com.yenaly.han1meviewer.logic.network.HCookieJar
import com.yenaly.han1meviewer.ui.viewmodel.AppViewModel
import com.yenaly.han1meviewer.util.CookieString
import kotlinx.serialization.json.Json

Expand Down Expand Up @@ -54,15 +53,13 @@ fun String.toVideoCode() = videoUrlRegex.find(this)?.groupValues?.get(1)

fun logout() {
isAlreadyLogin = false
AppViewModel.loginStateFlow.value = false
loginCookie = CookieString(EMPTY_STRING)
HCookieJar.cookieMap.clear()
CookieManager.getInstance().removeAllCookies(null)
}

fun login(cookies: String) {
isAlreadyLogin = true
AppViewModel.loginStateFlow.value = true
loginCookie = CookieString(cookies)
}

Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/com/yenaly/han1meviewer/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.yenaly.han1meviewer.util.CookieString
import com.yenaly.yenaly_libs.utils.applicationContext
import com.yenaly.yenaly_libs.utils.getSpValue
import com.yenaly.yenaly_libs.utils.putSpValue
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.time.Duration.Companion.days
Expand All @@ -31,16 +32,26 @@ object Preferences {
/**
* 是否登入,一般跟[loginCookie]一起賦值
*/
var isAlreadyLogin
var isAlreadyLogin: Boolean
get() = getSpValue(ALREADY_LOGIN, false)
set(value) = putSpValue(ALREADY_LOGIN, value)
set(value) {
loginStateFlow.value = value
putSpValue(ALREADY_LOGIN, value)
}

val loginStateFlow = MutableStateFlow(isAlreadyLogin)

/**
* 保存的string格式的登入cookie
*/
var loginCookie
get() = CookieString(getSpValue(LOGIN_COOKIE, EMPTY_STRING))
set(value) = putSpValue(LOGIN_COOKIE, value.cookie)
set(value) {
loginCookieStateFlow.value = value
putSpValue(LOGIN_COOKIE, value.cookie)
}

val loginCookieStateFlow = MutableStateFlow(loginCookie)

// 更新 相關

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yenaly.han1meviewer.logic.network

import com.yenaly.han1meviewer.Preferences.loginCookie
import com.yenaly.han1meviewer.Preferences
import com.yenaly.han1meviewer.util.toLoginCookieList
import okhttp3.Cookie
import okhttp3.CookieJar
Expand All @@ -24,12 +24,13 @@ class HCookieJar : CookieJar {
}

override fun loadForRequest(url: HttpUrl): List<Cookie> {
return cookieMap[url.host] ?: loginCookie.toLoginCookieList(url.host)
return cookieMap[url.host]
?: Preferences.loginCookieStateFlow.value.toLoginCookieList(url.host)
}

override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookieMap[url.host] = cookies.toMutableList().also {
it += loginCookie.toLoginCookieList(url.host)
it += Preferences.loginCookieStateFlow.value.toLoginCookieList(url.host)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ class MainActivity : YenalyActivity<ActivityMainBinding>(), DrawerListener {
if (state is WebsiteState.Success) {
if (isAlreadyLogin) {
if (state.info.username == null) {
logoutWithRefresh()
showShortToast(R.string.login_expired_auto_logout)
headerAvatar.load(R.mipmap.ic_launcher) {
crossfade(true)
transformations(CircleCropTransformation())
}
headerUsername.setText(R.string.refresh_page_or_login_expired)
} else {
headerAvatar.load(state.info.avatarUrl) {
crossfade(true)
Expand Down Expand Up @@ -278,7 +281,6 @@ class MainActivity : YenalyActivity<ActivityMainBinding>(), DrawerListener {
loginNeededFragmentList.forEach {
binding.nvMain.menu.findItem(it).setOnMenuItemClickListener {
showShortToast(R.string.login_first)
gotoLoginActivity()
return@setOnMenuItemClickListener false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class HomePageFragment : YenalyFragment<FragmentHomePageBinding>(),
) {
interpolator = animInterpolator
duration = animDuration
addUpdateListener(viewLifecycleOwner.lifecycle) {
addUpdateListener(this@HomePageFragment) {
val color = it.animatedValue as Int
binding.aColor.setBackgroundColor(color)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.yenaly.han1meviewer.ui.viewmodel
import android.util.Log
import androidx.lifecycle.viewModelScope
import androidx.work.WorkManager
import com.google.firebase.Firebase
import com.google.firebase.crashlytics.crashlytics
import com.google.firebase.crashlytics.setCustomKeys
import com.google.firebase.Firebase
import com.yenaly.han1meviewer.FirebaseConstants
import com.yenaly.han1meviewer.Preferences
import com.yenaly.han1meviewer.logic.NetworkRepo
Expand Down Expand Up @@ -33,8 +33,6 @@ object AppViewModel : YenalyViewModel(application), IHCsrfToken {
*/
override var csrfToken: String? = null

val loginStateFlow = MutableStateFlow(Preferences.isAlreadyLogin)

private val _versionFlow = MutableStateFlow<WebsiteState<Latest?>>(WebsiteState.Loading)
val versionFlow = _versionFlow.asStateFlow()

Expand All @@ -43,7 +41,7 @@ object AppViewModel : YenalyViewModel(application), IHCsrfToken {
WorkManager.getInstance(application).pruneWork()

viewModelScope.launch {
loginStateFlow.collect { isLogin ->
Preferences.loginStateFlow.collect { isLogin ->
Log.d("LoginState", "isLogin: $isLogin")
Firebase.crashlytics.setCustomKeys {
key(FirebaseConstants.LOGIN_STATE, isLogin)
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/yenaly/han1meviewer/util/Animations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.yenaly.han1meviewer.util
import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import androidx.annotation.ColorInt
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
Expand All @@ -22,6 +23,14 @@ fun ValueAnimator.addUpdateListener(
})
}

fun ValueAnimator.addUpdateListener(
fragment: Fragment,
listener: ValueAnimator.AnimatorUpdateListener?
) {
if (fragment.isDetached || fragment.view == null) return
addUpdateListener(fragment.viewLifecycleOwner.lifecycle, listener)
}

fun colorTransition(
@ColorInt fromColor: Int,
@ColorInt toColor: Int,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,6 @@ Recommendations are as follows:\n
<string name="privacy">Privacy</string>
<string name="about_analytics_summary">Would you mind if the developer collected your device information and your usage? It will help the developer a lot.&lt;br&gt;&lt;br&gt;Data analysis service is provided by Google Analytics for Firebase. Its privacy policy can be found at &lt;a href=&quot;https://www.google.com/policies/privacy/&quot;&gt;https://www.google.com/policies/privacy/&lt;/a&gt;.&lt;br&gt;&lt;br&gt;If you agree to join data analysis, your device information and your usage will be collected, including but not limited to Android API version, equipment model, language residence, the application version, conversion time, function times. The developer promise that these information will NOT be collected, including phone number, e-mail address, IMEI.&lt;br&gt;&lt;br&gt;Your information will not be collected until you agree to join data analysis.&lt;br&gt;&lt;br&gt;The collected information will be analyzed by Google Analytics for Firebase. The analysis report can only be viewed by the developer of this application.</string>
<string name="about_analytics">About Analytics</string>
<string name="refresh_page_or_login_expired">Try refreshing the page\nor your login may have expired</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,6 @@
<string name="privacy">隐私</string>
<string name="about_analytics_summary">您同意让开发者收集您的设备信息与使用情况吗?这将对开发有很大帮助。&lt;br&gt;&lt;br&gt;数据统计服务由 Google Analytics for Firebase 提供,其隐私政策可在 &lt;a href=&quot;https://www.google.com/policies/privacy/&quot;&gt;https://www.google.com/policies/privacy/&lt;/a&gt; 查看。&lt;br&gt;&lt;br&gt;若您同意参与数据统计,您的设备信息与应用使用情况将会被记录,其包括但不限于:Android API 版本、设备型号、语言所在地、本应用版本号、会话时长、应用功能使用次数。开发者承诺以下信息并不会被记录:电话号码、电子邮件地址、IMEI。&lt;br&gt;&lt;br&gt;在您同意参与数据统计之前,您的信息不会被记录。&lt;br&gt;&lt;br&gt;被记录的数据会由 Google Analytics for Firebase 进行分析,分析报表仅可被本应用开发者访问。</string>
<string name="about_analytics">关于使用情况统计</string>
<string name="refresh_page_or_login_expired">刷新一下页面试试\n也可能登入已过期</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,6 @@
<string name="privacy">隱私</string>
<string name="about_analytics_summary">您同意讓開發者收集您的裝置資訊與使用方式嗎?這對開發有很大幫助。&lt;br&gt;&lt;br&gt;資料統計服務由 Google Analytics for Firebase 提供,其隱私政策可在 &lt;a href=&quot;https://www.google.com/policies/privacy/&quot;&gt;https://www.google.com/policies/privacy/&lt;/a&gt; 檢視。&lt;br&gt;&lt;br&gt;若您同意參與資料統計,您的裝置資訊與應用使用情況將會被記錄,其包括但不限於:Android API 版本、裝置型號、語言所在地、本應用版本號、會話時長、應用功能使用次數。開發者承諾以下資訊並不會被記錄:電話號碼、電子郵件地址、IMEI。&lt;br&gt;&lt;br&gt;在您同意參與資料統計之前,您的資訊不會被記錄。&lt;br&gt;&lt;br&gt;被記錄的資料會由 Google Analytics for Firebase 進行分析,分析報表僅由此應用程式開發者存取。</string>
<string name="about_analytics">關於使用情況統計</string>
<string name="refresh_page_or_login_expired">刷新一下頁面試試\n也可能登入已過期</string>

</resources>

0 comments on commit 47df8f2

Please sign in to comment.