Integrate shared preferences
Half-done user info receiving
This commit is contained in:
parent
507e076eca
commit
5c3e7bcdee
@ -4,4 +4,7 @@ import com.nto.data.models.LoginResult
|
|||||||
|
|
||||||
interface DataRepository {
|
interface DataRepository {
|
||||||
suspend fun auth(login: String, password: String): LoginResult
|
suspend fun auth(login: String, password: String): LoginResult
|
||||||
|
suspend fun saveToken(token: String)
|
||||||
|
suspend fun getToken(): String
|
||||||
|
suspend fun getInfo()
|
||||||
}
|
}
|
@ -1,17 +1,34 @@
|
|||||||
package com.nto.data.repository
|
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.models.LoginResult
|
||||||
import com.nto.data.utils.Provider
|
import com.nto.data.utils.Provider
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import okhttp3.Credentials
|
import okhttp3.Credentials
|
||||||
import javax.inject.Inject
|
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 {
|
override suspend fun auth(login: String, password: String): LoginResult {
|
||||||
|
val token = Credentials.basic(login, password)
|
||||||
val result = Provider.provideRetrofit().auth(
|
val result = Provider.provideRetrofit().auth(
|
||||||
Credentials.basic(login, password)
|
token
|
||||||
).execute()
|
).execute()
|
||||||
|
if (result.isSuccessful) saveToken(token)
|
||||||
return LoginResult(result.isSuccessful, result.message())
|
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(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -4,4 +4,7 @@ import com.nto.data.models.LoginResult
|
|||||||
|
|
||||||
interface DomainRepository {
|
interface DomainRepository {
|
||||||
suspend fun auth(email: String, password: String): LoginResult
|
suspend fun auth(email: String, password: String): LoginResult
|
||||||
|
suspend fun saveToken(token: String)
|
||||||
|
suspend fun getToken(): String
|
||||||
|
suspend fun getInfo()
|
||||||
}
|
}
|
@ -17,4 +17,16 @@ class DomainRepositoryImpl @Inject constructor(private val dataRepositoryImpl: D
|
|||||||
LoginResult(false, "IO exception was thrown: ${e.message}")
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
@ -12,4 +12,5 @@ class LoginUseCase @Inject constructor(private val domainRepositoryImpl: DomainR
|
|||||||
suspend fun auth(email: String, password: String): LoginResult {
|
suspend fun auth(email: String, password: String): LoginResult {
|
||||||
return domainRepositoryImpl.auth(email, password)
|
return domainRepositoryImpl.auth(email, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +1,31 @@
|
|||||||
package com.nto.presentation.screens.profileScreen
|
package com.nto.presentation.screens.profileScreen
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import com.nto.data.utils.Destinations
|
import com.nto.data.utils.Destinations
|
||||||
|
import com.nto.domain.usecase.ProfileUseCase
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class ProfileViewModel @Inject constructor() : ViewModel() {
|
class ProfileViewModel @Inject constructor(private val useCase: ProfileUseCase) : ViewModel() {
|
||||||
private val _state = MutableStateFlow(ProfileState())
|
private val _state = MutableStateFlow(ProfileState())
|
||||||
|
|
||||||
val state: StateFlow<ProfileState>
|
val state: StateFlow<ProfileState>
|
||||||
get() = _state.asStateFlow()
|
get() = _state.asStateFlow()
|
||||||
|
|
||||||
|
fun updateInfo(){
|
||||||
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
|
val result = useCase.getInfo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun admin(navController: NavController) {
|
fun admin(navController: NavController) {
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
@ -31,4 +41,8 @@ class ProfileViewModel @Inject constructor() : ViewModel() {
|
|||||||
fun option(navController: NavController) {
|
fun option(navController: NavController) {
|
||||||
navController.navigate(Destinations.Options.toString())
|
navController.navigate(Destinations.Options.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user