Skip to content

Commit

Permalink
fix: integrate LoginUseCase into UserUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
peng-u-0807 committed Oct 12, 2023
1 parent 5c46289 commit bc675ed
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 36 deletions.
3 changes: 3 additions & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ dependencies {
// navigation
implementation("androidx.navigation:navigation-compose:2.5.3")

// navigation with hilt
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")

// Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
Expand Down
13 changes: 12 additions & 1 deletion android/app/src/main/java/com/goliath/emojihub/models/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import com.google.gson.annotations.SerializedName
class User(
dto: UserDto
) {
var token: String = dto.token
var likedEmojiList: Array<String> = dto.likedEmojiList
var createdEmojiList: Array<String> = dto.createdEmojiList
val userId: String = dto.userId
}

class UserDto(
@SerializedName("token")
val token: String,

@SerializedName("user_id")
val userId: String,

Expand All @@ -20,4 +24,11 @@ class UserDto(

@SerializedName("created_emoji_list")
val createdEmojiList: Array<String>
)
)

val dummyUser = User(UserDto(
token = "dummy",
userId = "1",
likedEmojiList = arrayOf(),
createdEmojiList = arrayOf()
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.goliath.emojihub.repositories

import com.goliath.emojihub.repositories.remote.UserRepository
import com.goliath.emojihub.repositories.remote.UserRepositoryImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@InstallIn(SingletonComponent::class)
@Module
abstract class RepositoryModule {
@Binds
abstract fun bindsUserRepository(impl: UserRepositoryImpl): UserRepository
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.goliath.emojihub.usecases

import com.goliath.emojihub.models.User
import com.goliath.emojihub.models.dummyUser
import com.goliath.emojihub.repositories.remote.UserRepository
import com.goliath.emojihub.viewmodels.Resource
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject
import javax.inject.Singleton

sealed interface UserUseCase {
fun fetchUser(id: String)
fun registerUser(id: String, password: String): User
}

class UserUseCaseImpl (
Expand All @@ -12,4 +19,8 @@ class UserUseCaseImpl (
override fun fetchUser(id: String) {
repository.fetchUser(id)
}

override fun registerUser(id: String, password: String): User {
return dummyUser
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ package com.goliath.emojihub.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.goliath.emojihub.models.User
import com.goliath.emojihub.usecases.LoginUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
import com.goliath.emojihub.models.dummyUser
import com.goliath.emojihub.usecases.UserUseCase
import kotlinx.coroutines.flow.MutableStateFlow

@HiltViewModel
class LoginViewModel @Inject constructor(
private val loginUseCase: LoginUseCase
) {

private val _loginState = MutableLiveData<Resource<User>>()
val loginState: LiveData<Resource<User>> = _loginState

fun onLoginClicked(username: String, password: String) {
_loginState.value = loginUseCase.login(username, password) // Hand
}
}
//class LoginViewModel(
// private val userUseCase: UserUseCase
//): ViewModel() {
//
// private val _loginState = MutableStateFlow(Resource.Success(dummyUser))
// val loginState = _loginState
//
// fun registerUser(username: String, password: String) {
// _loginState.value = userUseCase.registerUser(username, password) // Hand
// }
//}

sealed class Resource<out T> {
object Loading : Resource<Nothing>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package com.goliath.emojihub.viewmodels

import androidx.lifecycle.ViewModel
import com.goliath.emojihub.models.User
import com.goliath.emojihub.usecases.UserUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject


class UserViewModel (
private val useCase: UserUseCase
private val userUseCase: UserUseCase
): ViewModel() {

private val _loginState = MutableStateFlow<User?>(null)
val loginState = _loginState.asStateFlow()

fun fetchUser(id: String) {
useCase.fetchUser(id)
userUseCase.fetchUser(id)
}

fun registerUser(username: String, password: String) {
_loginState.update { userUseCase.registerUser(username, password) }
}
}

0 comments on commit bc675ed

Please sign in to comment.