Skip to content

Commit

Permalink
🔀 Merge pull request #64 from snuhcs-course/feat/delete-user-info-log…
Browse files Browse the repository at this point in the history
…out-withdraw

feat/delete-user-info-logout-withdraw
  • Loading branch information
yjeong-k authored Nov 26, 2023
2 parents 9b0213d + a48fb07 commit b0b0867
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ interface SymbolDao {
@Upsert
suspend fun upsertAll(symbolEntities: List<SymbolEntity>)

@Query("DELETE FROM symbols")
suspend fun deleteAllSymbols()
@Query("DELETE FROM symbols WHERE isMine = 1")
suspend fun deleteAllMySymbols()

@Query("UPDATE symbols SET isFavorite = 0 WHERE isFavorite = 1")
suspend fun resetFavoriteSymbols()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ interface UserDao {

@Query("DELETE FROM users WHERE id = :id")
suspend fun deleteUserById(id: Int)

@Query("DELETE FROM users")
suspend fun deleteUserInfo()
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ interface WeightRowDao {
@Upsert
suspend fun upsertAll(weightRowEntities: List<WeightRowEntity>)

@Query("DELETE FROM weighttable")
suspend fun deleteAllWeightRows()
@Query("DELETE FROM weighttable WHERE id > 500")
suspend fun deleteMySymbolsWeightRows()

@Query("UPDATE weighttable SET weights = :resetWeights WHERE id < 501")
suspend fun resetOriginalSymbolsWeightRows(resetWeights: List<Int>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SessionManager {
}
}

fun logout() {
fun deleteToken() {
CoroutineScope(Dispatchers.Main).launch {
_cachedToken.value = null
_userId.value = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ import com.example.speechbuddy.service.AuthService
import com.example.speechbuddy.utils.Resource
import com.example.speechbuddy.utils.ResponseCode
import com.example.speechbuddy.utils.ResponseHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import retrofit2.Response
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -141,9 +138,6 @@ class AuthRepository @Inject constructor(
val refreshToken = sessionManager.cachedToken.value!!.refreshToken!!
val result =
authService.logout(getAuthHeader(), AuthRefreshRequest(refreshToken))
CoroutineScope(Dispatchers.IO).launch {
authTokenPrefsManager.clearAuthToken()
}
emit(result)
} catch (e: Exception) {
emit(responseHandler.getConnectionErrorResponse())
Expand All @@ -156,9 +150,6 @@ class AuthRepository @Inject constructor(
val refreshToken = sessionManager.cachedToken.value!!.refreshToken!!
val result =
authService.withdraw(getAuthHeader(), AuthRefreshRequest(refreshToken))
CoroutineScope(Dispatchers.IO).launch {
authTokenPrefsManager.clearAuthToken()
}
emit(result)
} catch (e: Exception) {
emit(responseHandler.getConnectionErrorResponse())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class SettingsRepository @Inject constructor(
}
}

suspend fun resetSettings() {
settingsPrefManager.resetSettings()
}

suspend fun displayBackup(): Flow<Response<Void>> =
flow {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ class SymbolRepository @Inject constructor(
fun getNextSymbolId() =
symbolDao.getLastSymbol().map { symbol -> symbol.id + 1 }

fun clearAllMySymbols() {
/* TODO: 내가 만든 상징들 모두 삭제 */
suspend fun clearAllMySymbols() {
symbolDao.deleteAllMySymbols()
}

suspend fun resetFavoriteSymbols() {
symbolDao.resetFavoriteSymbols()
}

suspend fun insertSymbol(symbol: Symbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class UserRepository @Inject constructor(
}
}

suspend fun deleteUserInfo() {
userDao.deleteUserInfo()
}

private fun <T> returnUnknownError(): Resource<T> {
return Resource.error(
"Unknown error", null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class WeightTableRepository @Inject constructor(
}
}

suspend fun deleteAllWeightRows() {
weightRowDao.deleteAllWeightRows()
suspend fun resetAllWeightRows() {
weightRowDao.deleteMySymbolsWeightRows()
weightRowDao.resetOriginalSymbolsWeightRows(List(500) { 0 })
}

suspend fun replaceWeightTable(weightRowList: List<WeightRow>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.speechbuddy.data.local.AuthTokenPrefsManager
import com.example.speechbuddy.domain.SessionManager
import com.example.speechbuddy.repository.AuthRepository
import com.example.speechbuddy.repository.SettingsRepository
import com.example.speechbuddy.repository.SymbolRepository
import com.example.speechbuddy.repository.UserRepository
import com.example.speechbuddy.repository.WeightTableRepository
import com.example.speechbuddy.ui.models.AccountSettingsAlert
import com.example.speechbuddy.ui.models.AccountSettingsUiState
import com.example.speechbuddy.utils.ResponseCode
Expand All @@ -25,8 +28,11 @@ import javax.inject.Inject
class AccountSettingsViewModel @Inject internal constructor(
private val authRepository: AuthRepository,
private val settingsRepository: SettingsRepository,
private val weightTableRepository: WeightTableRepository,
private val symbolRepository: SymbolRepository,
private val userRepository: UserRepository,
private val sessionManager: SessionManager
private val sessionManager: SessionManager,
private val authTokenPrefsManager: AuthTokenPrefsManager
) : ViewModel() {

private val _uiState = MutableStateFlow(AccountSettingsUiState())
Expand Down Expand Up @@ -68,8 +74,13 @@ class AccountSettingsViewModel @Inject internal constructor(
authRepository.logout().collect { result ->
when (result.code()) {
ResponseCode.SUCCESS.value -> {
/* TODO: 디바이스에 저장돼 있는 유저 정보 초기화(토큰 말고) */
sessionManager.logout()
settingsRepository.resetSettings()
weightTableRepository.resetAllWeightRows()
symbolRepository.clearAllMySymbols()
symbolRepository.resetFavoriteSymbols()
userRepository.deleteUserInfo()
sessionManager.deleteToken()
authTokenPrefsManager.clearAuthToken()
hideAlert()
}

Expand All @@ -87,8 +98,13 @@ class AccountSettingsViewModel @Inject internal constructor(
authRepository.withdraw().collect { result ->
when (result.code()) {
ResponseCode.SUCCESS.value -> {
/* TODO: 디바이스에 저장돼 있는 유저 정보 초기화(토큰 말고) */
sessionManager.logout()
settingsRepository.resetSettings()
weightTableRepository.resetAllWeightRows()
symbolRepository.clearAllMySymbols()
symbolRepository.resetFavoriteSymbols()
userRepository.deleteUserInfo()
sessionManager.deleteToken()
authTokenPrefsManager.clearAuthToken()
hideAlert()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ResetPasswordViewModel @Inject internal constructor(
).collect { result ->
when (result.code()) {
ResponseCode.SUCCESS.value -> {
sessionManager.logout()
sessionManager.deleteToken()
onSuccess()
}

Expand Down

0 comments on commit b0b0867

Please sign in to comment.