diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index db92ee97..ebe24ec8 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -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") diff --git a/android/app/src/main/java/com/goliath/emojihub/models/User.kt b/android/app/src/main/java/com/goliath/emojihub/models/User.kt index bf2f9fa7..55bfaae4 100644 --- a/android/app/src/main/java/com/goliath/emojihub/models/User.kt +++ b/android/app/src/main/java/com/goliath/emojihub/models/User.kt @@ -6,12 +6,16 @@ import com.google.gson.annotations.SerializedName class User( dto: UserDto ) { + var token: String = dto.token var likedEmojiList: Array = dto.likedEmojiList var createdEmojiList: Array = dto.createdEmojiList val userId: String = dto.userId } class UserDto( + @SerializedName("token") + val token: String, + @SerializedName("user_id") val userId: String, @@ -20,4 +24,11 @@ class UserDto( @SerializedName("created_emoji_list") val createdEmojiList: Array -) \ No newline at end of file +) + +val dummyUser = User(UserDto( + token = "dummy", + userId = "1", + likedEmojiList = arrayOf(), + createdEmojiList = arrayOf() +)) \ No newline at end of file diff --git a/android/app/src/main/java/com/goliath/emojihub/repositories/RepositoryModule.kt b/android/app/src/main/java/com/goliath/emojihub/repositories/RepositoryModule.kt new file mode 100644 index 00000000..f70c57f5 --- /dev/null +++ b/android/app/src/main/java/com/goliath/emojihub/repositories/RepositoryModule.kt @@ -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 +} \ No newline at end of file diff --git a/android/app/src/main/java/com/goliath/emojihub/usecases/LoginUseCase.kt b/android/app/src/main/java/com/goliath/emojihub/usecases/LoginUseCase.kt deleted file mode 100644 index 716df704..00000000 --- a/android/app/src/main/java/com/goliath/emojihub/usecases/LoginUseCase.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.goliath.emojihub.usecases - -import com.goliath.emojihub.models.User -import com.goliath.emojihub.models.UserDto -import com.goliath.emojihub.viewmodels.Resource - -class LoginUseCase { - fun login(username: String, password: String): Resource? { - val dummyUser = User(UserDto( - userId = "1", - likedEmojiList = arrayOf(), - createdEmojiList = arrayOf() - )) - return Resource.Success(dummyUser); - } -} \ No newline at end of file diff --git a/android/app/src/main/java/com/goliath/emojihub/usecases/UserUseCase.kt b/android/app/src/main/java/com/goliath/emojihub/usecases/UserUseCase.kt index b04f6671..4c7d6ec9 100644 --- a/android/app/src/main/java/com/goliath/emojihub/usecases/UserUseCase.kt +++ b/android/app/src/main/java/com/goliath/emojihub/usecases/UserUseCase.kt @@ -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 ( @@ -12,4 +19,8 @@ class UserUseCaseImpl ( override fun fetchUser(id: String) { repository.fetchUser(id) } + + override fun registerUser(id: String, password: String): User { + return dummyUser + } } \ No newline at end of file diff --git a/android/app/src/main/java/com/goliath/emojihub/viewmodels/LoginViewModel.kt b/android/app/src/main/java/com/goliath/emojihub/viewmodels/LoginViewModel.kt index c72463da..5adb5fd1 100644 --- a/android/app/src/main/java/com/goliath/emojihub/viewmodels/LoginViewModel.kt +++ b/android/app/src/main/java/com/goliath/emojihub/viewmodels/LoginViewModel.kt @@ -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>() - val loginState: LiveData> = _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 { object Loading : Resource() diff --git a/android/app/src/main/java/com/goliath/emojihub/viewmodels/UserViewModel.kt b/android/app/src/main/java/com/goliath/emojihub/viewmodels/UserViewModel.kt index 280c972e..fa35a6ba 100644 --- a/android/app/src/main/java/com/goliath/emojihub/viewmodels/UserViewModel.kt +++ b/android/app/src/main/java/com/goliath/emojihub/viewmodels/UserViewModel.kt @@ -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(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) } } } \ No newline at end of file