Integrate shared preferences

Half-done user info receiving
This commit is contained in:
Nymos 2025-02-19 11:57:45 +03:00
parent 507e076eca
commit 5c3e7bcdee
7 changed files with 64 additions and 4 deletions

View File

@ -4,4 +4,7 @@ import com.nto.data.models.LoginResult
interface DataRepository {
suspend fun auth(login: String, password: String): LoginResult
suspend fun saveToken(token: String)
suspend fun getToken(): String
suspend fun getInfo()
}

View File

@ -1,17 +1,34 @@
package com.nto.data.repository
import android.content.Context
import android.content.Context.MODE_PRIVATE
import com.nto.data.models.LoginResult
import com.nto.data.utils.Provider
import dagger.hilt.android.qualifiers.ApplicationContext
import okhttp3.Credentials
import javax.inject.Inject
class DataRepositoryImpl @Inject constructor() : DataRepository {
class DataRepositoryImpl @Inject constructor(@ApplicationContext private val context: Context) :
DataRepository {
override suspend fun auth(login: String, password: String): LoginResult {
val token = Credentials.basic(login, password)
val result = Provider.provideRetrofit().auth(
Credentials.basic(login, password)
token
).execute()
if (result.isSuccessful) saveToken(token)
return LoginResult(result.isSuccessful, result.message())
}
override suspend fun saveToken(token: String) {
context.getSharedPreferences("auth", MODE_PRIVATE).edit().putString("token", token).apply()
}
override suspend fun getToken(): String {
return context.getSharedPreferences("auth", MODE_PRIVATE).getString("token", "")!!
}
override suspend fun getInfo(){
}
}

View File

@ -4,4 +4,7 @@ import com.nto.data.models.LoginResult
interface DomainRepository {
suspend fun auth(email: String, password: String): LoginResult
suspend fun saveToken(token: String)
suspend fun getToken(): String
suspend fun getInfo()
}

View File

@ -17,4 +17,16 @@ class DomainRepositoryImpl @Inject constructor(private val dataRepositoryImpl: D
LoginResult(false, "IO exception was thrown: ${e.message}")
}
}
override suspend fun saveToken(token: String) {
dataRepositoryImpl.saveToken(token)
}
override suspend fun getToken(): String {
return dataRepositoryImpl.getToken()
}
override suspend fun getInfo(){
val result = dataRepositoryImpl.getInfo()
}
}

View File

@ -12,4 +12,5 @@ class LoginUseCase @Inject constructor(private val domainRepositoryImpl: DomainR
suspend fun auth(email: String, password: String): LoginResult {
return domainRepositoryImpl.auth(email, password)
}
}

View File

@ -0,0 +1,10 @@
package com.nto.domain.usecase
import com.nto.domain.repository.DomainRepositoryImpl
import javax.inject.Inject
class ProfileUseCase @Inject constructor(private val domainRepositoryImpl: DomainRepositoryImpl) {
suspend fun getInfo(){
domainRepositoryImpl.getInfo()
}
}

View File

@ -1,21 +1,31 @@
package com.nto.presentation.screens.profileScreen
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavController
import com.nto.data.utils.Destinations
import com.nto.domain.usecase.ProfileUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class ProfileViewModel @Inject constructor() : ViewModel() {
class ProfileViewModel @Inject constructor(private val useCase: ProfileUseCase) : ViewModel() {
private val _state = MutableStateFlow(ProfileState())
val state: StateFlow<ProfileState>
get() = _state.asStateFlow()
fun updateInfo(){
viewModelScope.launch(Dispatchers.IO) {
val result = useCase.getInfo()
}
}
fun admin(navController: NavController) {
//TODO
}
@ -31,4 +41,8 @@ class ProfileViewModel @Inject constructor() : ViewModel() {
fun option(navController: NavController) {
navController.navigate(Destinations.Options.toString())
}
init {
}
}