Skip to content

Commit

Permalink
[feat] #8 MY 뷰 - 취미 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
HAJIEUN02 committed Nov 5, 2024
1 parent c3bfeda commit bb06ca5
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.and.data.datasource.remote

import org.sopt.and.data.model.response.ResponseHobbyDto
import org.sopt.and.presentation.util.NullableBaseRespone

interface MyRemoteDataSource {
suspend fun getMyHobby(): NullableBaseRespone<ResponseHobbyDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.and.data.datasourceImpl.remote

import org.sopt.and.data.datasource.remote.MyRemoteDataSource
import org.sopt.and.data.model.response.ResponseHobbyDto
import org.sopt.and.data.service.MyService
import org.sopt.and.presentation.util.NullableBaseRespone
import javax.inject.Inject

class MyRemoteDataSourceImpl @Inject constructor(
private val myService: MyService
) : MyRemoteDataSource {
override suspend fun getMyHobby(): NullableBaseRespone<ResponseHobbyDto> =
myService.getMyHobby()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.sopt.and.data.repositoryImpl

import org.sopt.and.data.datasource.remote.MyRemoteDataSource
import org.sopt.and.domain.model.UserHobbyEntity
import org.sopt.and.domain.repository.MyRepository
import javax.inject.Inject

class MyRepositoryImpl @Inject constructor(
private val myRemoteDataSource: MyRemoteDataSource
) : MyRepository {
override suspend fun getMyHobby(): Result<UserHobbyEntity> =
runCatching {
myRemoteDataSource.getMyHobby().result.toUserHobbyEntity()
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/org/sopt/and/data/service/MyService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.sopt.and.data.service

import org.sopt.and.data.model.response.ResponseHobbyDto
import org.sopt.and.data.service.AuthService.Companion.USER
import org.sopt.and.presentation.util.NullableBaseRespone
import retrofit2.http.GET

interface MyService {
@GET("/$USER/$HOBBY")
suspend fun getMyHobby(): NullableBaseRespone<ResponseHobbyDto>

companion object {
const val HOBBY = "my-hobby"
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/org/sopt/and/di/DataSourceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.and.data.datasource.local.WaveLocalDataSource
import org.sopt.and.data.datasource.remote.AuthRemoteDataSource
import org.sopt.and.data.datasource.remote.MyRemoteDataSource
import org.sopt.and.data.datasourceImpl.local.WaveLocalDataSourceImpl
import org.sopt.and.data.datasourceImpl.remote.AuthRemoteDataSourceImpl
import org.sopt.and.data.datasourceImpl.remote.MyRemoteDataSourceImpl
import javax.inject.Singleton

@Module
Expand All @@ -20,4 +22,8 @@ abstract class DataSourceModule {
@Binds
@Singleton
abstract fun bindsAuthRemoteDataSource(authRemoteDataSourceImpl: AuthRemoteDataSourceImpl): AuthRemoteDataSource

@Binds
@Singleton
abstract fun bindsMyRemoteDataSource(myRemoteDataSourceImpl: MyRemoteDataSourceImpl): MyRemoteDataSource
}
6 changes: 6 additions & 0 deletions app/src/main/java/org/sopt/and/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.and.data.repositoryImpl.AuthRepositoryImpl
import org.sopt.and.data.repositoryImpl.MyRepositoryImpl
import org.sopt.and.domain.repository.AuthRepository
import org.sopt.and.domain.repository.MyRepository
import javax.inject.Singleton

@Module
Expand All @@ -14,4 +16,8 @@ abstract class RepositoryModule {
@Binds
@Singleton
abstract fun bindsAuthRepository(authRepositoryImpl: AuthRepositoryImpl): AuthRepository

@Binds
@Singleton
abstract fun bindsMyRepository(myRepositoryImpl: MyRepositoryImpl): MyRepository
}
6 changes: 6 additions & 0 deletions app/src/main/java/org/sopt/and/di/ServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.and.data.service.AuthService
import org.sopt.and.data.service.MyService
import retrofit2.Retrofit
import javax.inject.Singleton

Expand All @@ -15,4 +16,9 @@ object ServiceModule {
@Singleton
fun providesAuthService(@Wave retrofit: Retrofit): AuthService =
retrofit.create(AuthService::class.java)

@Provides
@Singleton
fun providesMyService(@Wave retrofit: Retrofit): MyService =
retrofit.create(MyService::class.java)
}
11 changes: 9 additions & 2 deletions app/src/main/java/org/sopt/and/di/UseCaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.and.domain.repository.AuthRepository
import org.sopt.and.domain.repository.MyRepository
import org.sopt.and.domain.usecase.GetUserHobbyUseCase
import org.sopt.and.domain.usecase.PostUserLoginUseCase
import org.sopt.and.domain.usecase.PostUserRegisterUseCase
import javax.inject.Singleton
Expand All @@ -14,11 +16,16 @@ import javax.inject.Singleton
class UseCaseModule {
@Provides
@Singleton
fun providesUserRegisterUseCase(authRepository: AuthRepository): PostUserRegisterUseCase =
fun providesPostUserRegisterUseCase(authRepository: AuthRepository): PostUserRegisterUseCase =
PostUserRegisterUseCase(authRepository = authRepository)

@Provides
@Singleton
fun providesUserLoginUseCase(authRepository: AuthRepository): PostUserLoginUseCase =
fun providesPostUserLoginUseCase(authRepository: AuthRepository): PostUserLoginUseCase =
PostUserLoginUseCase(authRepository = authRepository)

@Provides
@Singleton
fun providesGetUserHobbyUseCase(myRepository: MyRepository): GetUserHobbyUseCase =
GetUserHobbyUseCase(myRepository = myRepository)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sopt.and.domain.repository

import org.sopt.and.domain.model.UserHobbyEntity

interface MyRepository {
suspend fun getMyHobby(): Result<UserHobbyEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sopt.and.domain.usecase

import org.sopt.and.domain.model.UserHobbyEntity
import org.sopt.and.domain.repository.MyRepository

class GetUserHobbyUseCase(
private val myRepository: MyRepository
) {
suspend operator fun invoke(): Result<UserHobbyEntity> =
myRepository.getMyHobby()
}
6 changes: 5 additions & 1 deletion app/src/main/java/org/sopt/and/presentation/ui/my/MyRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package org.sopt.and.presentation.ui.my
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle

@Composable
fun MyRoute(
paddingValues: PaddingValues,
myViewModel: MyViewModel = hiltViewModel()
) {
val userHobby by myViewModel.userHobby.collectAsStateWithLifecycle()

MyScreen(
modifier = Modifier.padding(paddingValues),
userName = myViewModel.getLocalUserMail()
userHobby = userHobby
)
}
10 changes: 5 additions & 5 deletions app/src/main/java/org/sopt/and/presentation/ui/my/MyScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.sopt.and.ui.theme.White

@Composable
fun MyScreen(
userName: String,
userHobby: String,
modifier: Modifier = Modifier
) {
Column(
Expand All @@ -44,7 +44,7 @@ fun MyScreen(
.padding(horizontal = 15.dp, vertical = 20.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
MyProfile(userEmail = userName)
MyProfile(userHobby = userHobby)

MyPurchaseBox(information = stringResource(R.string.my_purchase_event))

Expand All @@ -69,7 +69,7 @@ fun MyScreen(
}

@Composable
fun MyProfile(userEmail: String) {
fun MyProfile(userHobby: String) {
Row(
modifier = Modifier
.fillMaxWidth(),
Expand All @@ -83,7 +83,7 @@ fun MyProfile(userEmail: String) {
)

Text(
text = stringResource(R.string.my_nickname, userEmail),
text = stringResource(R.string.my_hobby, userHobby),
modifier = Modifier
.padding(start = 8.dp),
color = White
Expand Down Expand Up @@ -178,6 +178,6 @@ fun MyListBox(title: String, description: String) {
@Composable
fun MyPreview() {
ANDANDROIDTheme {
MyScreen(userName = "[email protected]")
MyScreen(userHobby = "swim")
}
}
23 changes: 20 additions & 3 deletions app/src/main/java/org/sopt/and/presentation/ui/my/MyViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package org.sopt.and.presentation.ui.my

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import org.sopt.and.data.datasource.local.WaveLocalDataSource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.sopt.and.domain.usecase.GetUserHobbyUseCase
import javax.inject.Inject

@HiltViewModel
class MyViewModel @Inject constructor(
private val localDataSource: WaveLocalDataSource
private val getUserHobbyUseCase: GetUserHobbyUseCase
) : ViewModel() {
fun getLocalUserMail(): String = localDataSource.userEmail
private val _userHobby = MutableStateFlow<String>("")
val userHobby: StateFlow<String> = _userHobby

init {
getUserHobby()
}

private fun getUserHobby() {
viewModelScope.launch {
getUserHobbyUseCase().onSuccess { data ->
_userHobby.value = data.hobby
}
}
}
}

0 comments on commit bb06ca5

Please sign in to comment.