This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: "kotlin-kapt" | |
dependencies { | |
implementation 'androidx.room:room-runtime:2.2.5' | |
implementation 'androidx.room:room-coroutines:2.1.0-alpha04' | |
kapt 'android.arch.persistence.room:compiler:1.1.1' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "users_table") | |
data class CountDown( | |
@PrimaryKey(autoGenerate = true) | |
var long: Id, | |
@ColumnInfo(name = "name") | |
var name:String, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface UserDao{ | |
@Query("SELECT * FROM users_table") | |
fun getAll():List<User> | |
@Query("SELECT * FROM users_table WHERE Id = :Id") | |
fun getUser(Id:Long): User | |
@Insert(onConflict = OnConflictStrategy.REPLACE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Database(entities = arrayOf(User::class), version = 1, exportSchema = false) | |
abstract class UsersDatabase: RoomDatabase() { | |
abstract fun getDao(): UserDao | |
//Singleton to prevent creation ot multiple database instances | |
companion object{ | |
private var INSTANCE: CountDownDatabase? = null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Repository(val dao: UserDao) { | |
//Retrieving methods are thread-safe | |
val allUsers = dao.getAll() | |
// invoke on coroutine or other suspend function only | |
fun insert(user: User) = GlobalScope.launch(Dispatchers.IO) { | |
dao.insert(user) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginViewModel(val repository: AuthRepository): ViewMode() { | |
private val _state = mutableStateOf(LoginUiState()) | |
val state = _state.asStateFlow() | |
fun onLoginAttemp(user: User) { | |
viewModelScope.launch { | |
repository.isRegistered(user).collect { registered -> | |
when (registered) { | |
is Result.Loading -> { | |
_state.update { it.copy(loading = true) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class LoginUiState( | |
val loading: Boolean = false, | |
val navigateToHome = false, | |
val navigateToRegistration = false, | |
val errorMessages = listOf<String>() | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface AuthRepository{ | |
fun isSignedIn(): Flow<Result<Boolean>> | |
fun isRegistered(token: String): Flow<Result<Boolean>> | |
fun signIn(user: User): Flow<Result<Boolean>> | |
fun register(user: User): Flow<Result<Boolean>> | |
} | |
class AuthRepoImpl(val remoteService: MyService){ | |
override fun isSignedIn(): Flow<Result<Boolean>> = | |
callbackFlow { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class User(val id: String, val name:String, val token: String){ | |
fun toJson(): Map<String, String> = mapOf { | |
"id" to id, | |
"name" to name, | |
"token" to token, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Result<out T>{ | |
object Loading(): Result<Nothing>() | |
class Success<T>(val value: T): Result<T>() | |
class Error(val message:String): Result<Nothing>() | |
} |
OlderNewer