-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Implemented Room database with get and add user methods and …
…shared viewmodel
- Loading branch information
1 parent
998bc38
commit 938c3c8
Showing
8 changed files
with
131 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/example/recipeappiti/auth/model/User.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.recipeappiti.auth.model | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "user") | ||
data class User( | ||
@PrimaryKey(autoGenerate = true) | ||
val id: Int = 0, | ||
val email: String, | ||
val username: String, | ||
val password: String | ||
) |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/example/recipeappiti/auth/model/UserDao.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.example.recipeappiti.auth.model | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
|
||
@Dao | ||
interface UserDao { | ||
@Insert(onConflict = OnConflictStrategy.IGNORE) | ||
suspend fun addUser(user: User) | ||
|
||
@Query("SELECT * FROM user WHERE email = :email AND password = :password LIMIT 1") | ||
fun getUser(email: String, password: String): LiveData<User?> | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/example/recipeappiti/auth/model/UserRepository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.recipeappiti.auth.model | ||
|
||
import androidx.lifecycle.LiveData | ||
|
||
class UserRepository(private val userDao: UserDao) { | ||
fun getUser(email: String, password: String): LiveData<User?> { | ||
val user = userDao.getUser(email, password) | ||
return user | ||
} | ||
|
||
suspend fun addUser(user: User) { | ||
userDao.addUser(user) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/example/recipeappiti/auth/viewmodel/UserViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.example.recipeappiti.auth.viewmodel | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.example.recipeappiti.auth.model.User | ||
import com.example.recipeappiti.auth.model.UserRepository | ||
import kotlinx.coroutines.launch | ||
|
||
class UserViewModel(private val userRepository: UserRepository): ViewModel() { | ||
private val _user = MutableLiveData<User?>() | ||
val user: LiveData<User?> get() = _user | ||
|
||
fun getUser(email: String, password: String) { | ||
_user.value = userRepository.getUser(email, password).value | ||
} | ||
|
||
fun addUser(user: User) { | ||
viewModelScope.launch { | ||
userRepository.addUser(user) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/example/recipeappiti/auth/viewmodel/UserViewModelFactory.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.example.recipeappiti.auth.viewmodel | ||
|
||
import android.content.Context | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.example.recipeappiti.auth.model.UserRepository | ||
|
||
class UserViewModelFactory(private val userRepository: UserRepository): ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(UserViewModel::class.java)) { | ||
return UserViewModel(userRepository) as T | ||
} else { | ||
throw IllegalArgumentException("Unknown ViewModel class") | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/example/recipeappiti/core/model/UserDatabase.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.recipeappiti.core.model | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import com.example.recipeappiti.auth.model.User | ||
import com.example.recipeappiti.auth.model.UserDao | ||
|
||
@Database(entities = [User::class], version = 1) | ||
abstract class UserDatabase: RoomDatabase() { | ||
abstract fun userDao(): UserDao | ||
|
||
companion object { | ||
@Volatile | ||
private var INSTANCE: UserDatabase? = null | ||
fun getDatabaseInstance(context: Context): UserDatabase { | ||
return INSTANCE ?: synchronized(this) { | ||
Room.databaseBuilder( | ||
context = context, | ||
klass = UserDatabase::class.java, | ||
name = "products_database" | ||
).build().also { INSTANCE = it } | ||
} | ||
} | ||
} | ||
} |