fix a serious bug in v5.3 (#20)

use composable function instead of fragment to display authenticate screen
fix authenticate screen is transparent in light theme
This commit is contained in:
BinTianqi
2024-05-19 20:35:52 +08:00
parent 074b8d919b
commit 32b78a9738
10 changed files with 196 additions and 186 deletions

View File

@@ -6,11 +6,9 @@ import android.content.ComponentName
import android.content.Context
import android.os.Build.VERSION
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
@@ -29,14 +27,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
@@ -51,67 +47,43 @@ import java.util.Locale
var backToHome = false
@ExperimentalMaterial3Api
class MainActivity : FragmentActivity() {
private var auth = false
private val showAuth = mutableStateOf(false)
@SuppressLint("UnrememberedMutableState")
override fun onCreate(savedInstanceState: Bundle?) {
registerActivityResult(this)
enableEdgeToEdge()
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)
setContentView(R.layout.base)
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.add(R.id.base, HomeFragment(), "home")
if(sharedPref.getBoolean("auth", false)){
transaction.add(R.id.base, AuthFragment(), "auth")
showAuth.value = true
}
transaction.commit()
val locale = applicationContext.resources?.configuration?.locale
zhCN = locale==Locale.SIMPLIFIED_CHINESE||locale==Locale.CHINESE||locale==Locale.CHINA
setContent {
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
OwnDroidTheme(materialYou.value, blackTheme.value){
Home(materialYou, blackTheme)
if(showAuth.value){
AuthScreen(this, showAuth)
}
}
}
}
override fun onResume() {
super.onResume()
if(auth){
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
if(
sharedPref.getBoolean("auth", false) &&
sharedPref.getBoolean("lock_in_background", false)
){
val fragmentManager = supportFragmentManager
val fragment = fragmentManager.findFragmentByTag("auth")
if(fragment == null){
val transaction = fragmentManager.beginTransaction()
transaction.setCustomAnimations(R.anim.enter, R.anim.exit)
transaction.add(R.id.base, AuthFragment(), "auth").commit()
}
}
auth = false
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
if(
sharedPref.getBoolean("auth", false) &&
sharedPref.getBoolean("lock_in_background", false)
){
showAuth.value = true
}
}
override fun onRestart() {
super.onRestart()
auth = true
}
}
class HomeFragment: Fragment() {
@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("UnrememberedMutableState")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val sharedPref = context?.getSharedPreferences("data", Context.MODE_PRIVATE)!!
return ComposeView(requireContext()).apply {
setContent {
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
OwnDroidTheme(materialYou.value, blackTheme.value){
Home(materialYou, blackTheme)
}
}
}
}
}
@SuppressLint("UnrememberedMutableState")