This commit is contained in:
@@ -17,4 +17,9 @@ class MainActivity : ComponentActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent { YpChatRoot(viewModel) }
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
viewModel.reconnectIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,10 +32,25 @@ class ProfileStore(context: Context) {
|
||||
preferences.edit().clear().apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents an existing server session from silently logging a user back in
|
||||
* after they explicitly chose to log out on this device.
|
||||
*/
|
||||
fun markLocallyLoggedOut() {
|
||||
preferences.edit().putBoolean(KEY_LOGGED_OUT, true).apply()
|
||||
}
|
||||
|
||||
fun clearLocalLogoutMarker() {
|
||||
preferences.edit().remove(KEY_LOGGED_OUT).apply()
|
||||
}
|
||||
|
||||
fun wasLocallyLoggedOut(): Boolean = preferences.getBoolean(KEY_LOGGED_OUT, false)
|
||||
|
||||
private companion object {
|
||||
const val KEY_NICKNAME = "nickname"
|
||||
const val KEY_GENDER = "gender"
|
||||
const val KEY_AGE = "age"
|
||||
const val KEY_COUNTRY = "country"
|
||||
const val KEY_LOGGED_OUT = "logged_out"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ class ChatRepository(
|
||||
_state.value = _state.value.copy(savedProfile = profileStore.read())
|
||||
loadCountries()
|
||||
loadFeedbackAdminStatus()
|
||||
if (profileStore.wasLocallyLoggedOut()) return
|
||||
runCatching { restApi.session() }
|
||||
.onSuccess { session ->
|
||||
_state.value = _state.value.copy(
|
||||
@@ -99,6 +100,7 @@ class ChatRepository(
|
||||
}
|
||||
|
||||
suspend fun login(userName: String, gender: String, age: Int, country: String) {
|
||||
profileStore.clearLocalLogoutMarker()
|
||||
profileStore.write(SavedProfile(userName, gender, age, country))
|
||||
val session = runCatching { restApi.session() }.getOrNull()
|
||||
val sessionId = session?.sessionId ?: _state.value.expressSessionId
|
||||
@@ -111,6 +113,7 @@ class ChatRepository(
|
||||
}
|
||||
|
||||
suspend fun logout() {
|
||||
profileStore.markLocallyLoggedOut()
|
||||
runCatching { restApi.logout() }
|
||||
socketClient.disconnect()
|
||||
cookieJar.clear()
|
||||
@@ -123,6 +126,12 @@ class ChatRepository(
|
||||
expressSessionId?.let { socketClient.setSessionId(it) }
|
||||
}
|
||||
|
||||
fun reconnectIfNeeded() {
|
||||
if (_state.value.isLoggedIn && !socketClient.isConnected) {
|
||||
connectSocket()
|
||||
}
|
||||
}
|
||||
|
||||
fun openConversation(userName: String) {
|
||||
_state.value = _state.value.copy(
|
||||
currentConversation = userName,
|
||||
|
||||
@@ -37,6 +37,8 @@ class ChatViewModel(private val repository: ChatRepository) : ViewModel() {
|
||||
viewModelScope.launch { repository.logout() }
|
||||
}
|
||||
|
||||
fun reconnectIfNeeded() = repository.reconnectIfNeeded()
|
||||
|
||||
fun openConversation(userName: String) = repository.openConversation(userName)
|
||||
fun closeConversation() = repository.closeConversation()
|
||||
fun sendMessage(text: String) = repository.sendMessage(text)
|
||||
|
||||
Reference in New Issue
Block a user