Login/Logout Improvements
This commit is contained in:
@@ -38,6 +38,8 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.helcel.cowspent.R
|
||||
import net.helcel.cowspent.android.main.MainConstants
|
||||
import net.helcel.cowspent.persistence.CowspentSQLiteOpenHelper
|
||||
import net.helcel.cowspent.persistence.CowspentServerSyncHelper
|
||||
import net.helcel.cowspent.theme.ThemeUtils
|
||||
import net.helcel.cowspent.util.CospendClientUtil
|
||||
import net.helcel.cowspent.util.CospendClientUtil.LoginStatus
|
||||
@@ -188,6 +190,12 @@ class AccountActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun startInitialAccountSync() {
|
||||
if (!CowspentServerSyncHelper.isNextcloudAccountConfigured(applicationContext)) return
|
||||
CowspentSQLiteOpenHelper.getInstance(applicationContext)
|
||||
.cowspentServerSyncHelper.runAccountProjectsSync()
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
@@ -207,6 +215,8 @@ class AccountActivity : AppCompatActivity() {
|
||||
viewModel.serverUrl = ssoAccount.url
|
||||
viewModel.username = ssoAccount.userId
|
||||
|
||||
startInitialAccountSync()
|
||||
|
||||
val resultData = Intent()
|
||||
resultData.putExtra(MainConstants.CREDENTIALS_CHANGED, CREDENTIALS_CHANGED)
|
||||
setResult(RESULT_OK, resultData)
|
||||
@@ -312,6 +322,8 @@ class AccountActivity : AppCompatActivity() {
|
||||
remove(SETTINGS_KEY_LAST_MODIFIED)
|
||||
}
|
||||
|
||||
startInitialAccountSync()
|
||||
|
||||
val data = Intent()
|
||||
data.putExtra(MainConstants.CREDENTIALS_CHANGED, CREDENTIALS_CHANGED)
|
||||
setResult(RESULT_OK, data)
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.helcel.cowspent.android.account
|
||||
|
||||
import android.app.Application
|
||||
import android.content.SharedPreferences
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
@@ -12,6 +13,7 @@ import androidx.preference.PreferenceManager
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.helcel.cowspent.persistence.CowspentSQLiteOpenHelper
|
||||
import net.helcel.cowspent.util.CospendClientUtil
|
||||
import net.helcel.cowspent.util.SecureStorage
|
||||
|
||||
@@ -88,8 +90,28 @@ class AccountViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
private fun forgetProjectsTheAccountProvided() {
|
||||
try {
|
||||
val db = CowspentSQLiteOpenHelper.getInstance(getApplication())
|
||||
val offered = db.accountProjects
|
||||
val cospendPath = "/index.php/apps/cospend"
|
||||
for (project in db.projects) {
|
||||
val matches = offered.any {
|
||||
it.remoteId == project.remoteId &&
|
||||
project.serverUrl?.replace("/+$".toRegex(), "") ==
|
||||
it.ncUrl.replace("/+$".toRegex(), "") + cospendPath
|
||||
}
|
||||
if (matches) db.deleteProject(project.id)
|
||||
}
|
||||
db.clearAccountProjects()
|
||||
} catch (e: Exception) {
|
||||
Log.e("AccountViewModel", "Could not remove the account's projects on logout", e)
|
||||
}
|
||||
}
|
||||
|
||||
fun logout() {
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) { forgetProjectsTheAccountProvided() }
|
||||
SecureStorage.removePassword(getApplication(), AccountActivity.SETTINGS_PASSWORD)
|
||||
}
|
||||
preferences.edit {
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.os.Bundle
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
@@ -25,6 +26,14 @@ import net.helcel.cowspent.util.ColorUtils
|
||||
*/
|
||||
class PreferencesActivity : AppCompatActivity() {
|
||||
|
||||
private val accountLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == RESULT_OK) {
|
||||
setResult(RESULT_OK, result.data)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
enableEdgeToEdge()
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -59,7 +68,7 @@ class PreferencesActivity : AppCompatActivity() {
|
||||
SettingsScreen(
|
||||
onBack = { NavUtils.navigateUpFromSameTask(this) },
|
||||
onAccountSettingsClick = {
|
||||
startActivity(Intent(this, AccountActivity::class.java))
|
||||
accountLauncher.launch(Intent(this, AccountActivity::class.java))
|
||||
},
|
||||
onAboutClick = {
|
||||
startActivity(Intent(this, AboutActivity::class.java))
|
||||
|
||||
Reference in New Issue
Block a user