[feat] Optional self signed certificate

This commit is contained in:
acite
2025-09-10 23:51:13 +08:00
parent b48f8ce6b0
commit 10f316cb48
12 changed files with 246 additions and 111 deletions

View File

@@ -31,33 +31,19 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject
import com.acitelight.aether.service.*
import dagger.hilt.android.lifecycle.HiltViewModel
class HomeScreenViewModel(application: Application) : AndroidViewModel(application)
@HiltViewModel
class HomeScreenViewModel @Inject constructor(
private val settingsDataStoreManager: SettingsDataStoreManager
) : ViewModel()
{
private val dataStore = application.dataStore
private val USER_NAME_KEY = stringPreferencesKey("user_name")
private val PRIVATE_KEY = stringPreferencesKey("private_key")
private val URL_KEY = stringPreferencesKey("url")
private val CERT_KEY = stringPreferencesKey("cert")
val userNameFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[USER_NAME_KEY] ?: ""
}
val privateKeyFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[PRIVATE_KEY] ?: ""
}
val urlFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[URL_KEY] ?: ""
}
val certFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[CERT_KEY] ?: ""
}
var _init = false
var imageLoader: ImageLoader? = null;
val uss = settingsDataStoreManager.useSelfSignedFlow
@Composable
fun Init(){
@@ -79,15 +65,15 @@ class HomeScreenViewModel(application: Application) : AndroidViewModel(applicati
init {
viewModelScope.launch {
val u = userNameFlow.first()
val p = privateKeyFlow.first()
val ur = urlFlow.first()
val c = certFlow.first()
val u = settingsDataStoreManager.userNameFlow.first()
val p = settingsDataStoreManager.privateKeyFlow.first()
val ur = settingsDataStoreManager.urlFlow.first()
val c = settingsDataStoreManager.certFlow.first()
if(u=="" || p=="" || ur=="" || c=="") return@launch
if(u=="" || p=="" || ur=="") return@launch
try{
val usedUrl = ApiClient.apply(ur, c)
val usedUrl = ApiClient.apply(ur, if(uss.first()) c else "")
if (MediaManager.token == "null")
MediaManager.token = AuthManager.fetchToken(

View File

@@ -8,6 +8,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.acitelight.aether.Global
import com.acitelight.aether.dataStore
@@ -20,63 +21,52 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject
import com.acitelight.aether.service.*
import dagger.hilt.android.lifecycle.HiltViewModel
class MeScreenViewModel(application: Application) : AndroidViewModel(application) {
private val dataStore = application.dataStore
private val USER_NAME_KEY = stringPreferencesKey("user_name")
private val PRIVATE_KEY = stringPreferencesKey("private_key")
private val URL_KEY = stringPreferencesKey("url")
private val CERT_KEY = stringPreferencesKey("cert")
val userNameFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[USER_NAME_KEY] ?: ""
}
val privateKeyFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[PRIVATE_KEY] ?: ""
}
val urlFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[URL_KEY] ?: ""
}
val certFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[CERT_KEY] ?: ""
}
@HiltViewModel
class MeScreenViewModel @Inject constructor(
private val settingsDataStoreManager: SettingsDataStoreManager
) : ViewModel() {
val username = mutableStateOf("");
val privateKey = mutableStateOf("")
val url = mutableStateOf("");
val cert = mutableStateOf("")
val uss = settingsDataStoreManager.useSelfSignedFlow
init {
viewModelScope.launch {
username.value = userNameFlow.first()
privateKey.value = if (privateKeyFlow.first() == "") "" else "******"
url.value = urlFlow.first()
cert.value = certFlow.first()
username.value = settingsDataStoreManager.userNameFlow.first()
privateKey.value = if (settingsDataStoreManager.privateKeyFlow.first() == "") "" else "******"
url.value = settingsDataStoreManager.urlFlow.first()
cert.value = settingsDataStoreManager.certFlow.first()
}
}
fun onUseSelfSignedCheckedChange(isChecked: Boolean) {
viewModelScope.launch {
settingsDataStoreManager.saveUseSelfSigned(isChecked)
}
}
fun updateServer(u: String, c: String, context: Context)
{
viewModelScope.launch {
dataStore.edit { preferences ->
preferences[URL_KEY] = u
preferences[CERT_KEY] = c
}
settingsDataStoreManager.saveUrl(u)
settingsDataStoreManager.saveCert(c)
Global.loggedIn = false
val us = userNameFlow.first()
val u = urlFlow.first()
val c = certFlow.first()
val p = privateKeyFlow.first()
val us = settingsDataStoreManager.userNameFlow.first()
val p = settingsDataStoreManager.privateKeyFlow.first()
if (u == "" || c == "" || p == "" || us == "") return@launch
if (u == "" || p == "" || us == "") return@launch
try {
val usedUrl = ApiClient.apply(u, c)
val usedUrl = ApiClient.apply(u, if(uss.first()) c else "")
MediaManager.token = AuthManager.fetchToken(
us,
p
@@ -93,22 +83,18 @@ class MeScreenViewModel(application: Application) : AndroidViewModel(application
fun updateAccount(u: String, p: String, context: Context) {
viewModelScope.launch {
dataStore.edit { preferences ->
preferences[USER_NAME_KEY] = u
preferences[PRIVATE_KEY] = p
}
settingsDataStoreManager.saveUserName(u)
settingsDataStoreManager.savePrivateKey(p)
privateKey.value = "******"
Global.loggedIn = false
val u = userNameFlow.first()
val p = privateKeyFlow.first()
val u = settingsDataStoreManager.userNameFlow.first()
val p = settingsDataStoreManager.privateKeyFlow.first()
val ur = settingsDataStoreManager.urlFlow.first()
val ur = urlFlow.first()
val c = certFlow.first()
if (u == "" || p == "" || ur == "" || c == "") return@launch
if (u == "" || p == "" || ur == "") return@launch
try {
MediaManager.token = AuthManager.fetchToken(