generated from AND-SOPT-ANDROID/and-sopt-android-template
-
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.
refactor #11 - SignInScreen MVI 리팩토링
- Loading branch information
Showing
19 changed files
with
576 additions
and
230 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
app/src/main/java/org/sopt/and/core/utils/PreferenceUtil.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,33 @@ | ||
package org.sopt.and.core.utils | ||
|
||
import android.content.Context | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
|
||
|
||
class PreferenceUtil @Inject constructor( | ||
@ApplicationContext private val context: Context | ||
) { | ||
private val sharedPreferences = | ||
context.getSharedPreferences("wavve_prefs", Context.MODE_PRIVATE) | ||
|
||
fun saveUserToken(token: String) { | ||
sharedPreferences.edit().putString(USER_TOKEN, token).apply() | ||
} | ||
|
||
fun getUserToken(): String? { | ||
return sharedPreferences.getString(USER_TOKEN, null) | ||
} | ||
|
||
fun clearUserToken() { | ||
sharedPreferences.edit().remove(USER_TOKEN).apply() | ||
} | ||
|
||
fun clearAll() { | ||
sharedPreferences.edit().clear().apply() | ||
} | ||
|
||
companion object { | ||
private const val USER_TOKEN = "user_token" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/org/sopt/and/core/utils/SnackBarUtils.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,33 @@ | ||
package org.sopt.and.core.utils | ||
|
||
import androidx.compose.material3.SnackbarDuration | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.SnackbarResult | ||
|
||
object SnackBarUtils { | ||
private lateinit var snackBarHostState: SnackbarHostState | ||
|
||
fun init(snackBarHostState: SnackbarHostState) { | ||
SnackBarUtils.snackBarHostState = snackBarHostState | ||
} | ||
|
||
suspend fun showSnackBar( | ||
message: String, | ||
actionLabel: String? = null, | ||
onActionClick: (() -> Unit)? = null, | ||
duration: SnackbarDuration = SnackbarDuration.Short, | ||
) { | ||
if (this::snackBarHostState.isInitialized) { | ||
val result = snackBarHostState.showSnackbar( | ||
message = message, | ||
actionLabel = actionLabel, | ||
duration = duration | ||
) | ||
if (result == SnackbarResult.ActionPerformed) { | ||
onActionClick?.invoke() | ||
} | ||
} else { | ||
throw UninitializedPropertyAccessException("SnackBarHostState가 초기화 되지 않았습니다. init()을 먼저 호출하세요") | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...in/java/org/sopt/and/core/util/UiState.kt → ...n/java/org/sopt/and/core/utils/UiState.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
20 changes: 20 additions & 0 deletions
20
app/src/main/java/org/sopt/and/data/di/PreferenceModule.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,20 @@ | ||
package org.sopt.and.data.di | ||
|
||
import android.content.Context | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import org.sopt.and.core.utils.PreferenceUtil | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object PreferenceModule { | ||
@Provides | ||
@Singleton | ||
fun providePreferenceUtil(@ApplicationContext context: Context): PreferenceUtil { | ||
return PreferenceUtil(context) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/org/sopt/and/data/di/RepositoryModule.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,39 @@ | ||
package org.sopt.and.data.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.sopt.and.data.datasource.MyHobbyDataSource | ||
import org.sopt.and.data.datasource.SignInDataSource | ||
import org.sopt.and.data.datasource.SignUpDataSource | ||
import org.sopt.and.data.repositoryimpl.MyHobbyRepositoryImpl | ||
import org.sopt.and.data.repositoryimpl.SignInRepositoryImpl | ||
import org.sopt.and.data.repositoryimpl.SignUpRepositoryImpl | ||
import org.sopt.and.domain.repository.MyHobbyRepository | ||
import org.sopt.and.domain.repository.SignUpRepository | ||
import org.sopt.and.domain.repository.SignInRepository | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RepositoryModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideUserRegisterRepository(userService: SignUpDataSource): SignUpRepository { | ||
return SignUpRepositoryImpl(userService) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideUserLoginRepository(userService: SignInDataSource): SignInRepository { | ||
return SignInRepositoryImpl(userService) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideGetMyHobbyRepository(userService: MyHobbyDataSource): MyHobbyRepository { | ||
return MyHobbyRepositoryImpl(userService) | ||
} | ||
} |
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,45 @@ | ||
package org.sopt.and.data.di | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.sopt.and.BuildConfig.BASE_URL | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RetrofitModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideLoggingInterceptor(): HttpLoggingInterceptor { | ||
return HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideOkHttpClient(loggingInterceptor: HttpLoggingInterceptor): OkHttpClient { | ||
return OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.build() | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit { | ||
return Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(okHttpClient) | ||
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
} | ||
} |
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,36 @@ | ||
package org.sopt.and.data.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.sopt.and.domain.repository.MyHobbyRepository | ||
import org.sopt.and.domain.repository.SignUpRepository | ||
import org.sopt.and.domain.repository.SignInRepository | ||
import org.sopt.and.domain.usecase.MyHobbyUseCase | ||
import org.sopt.and.domain.usecase.SignInUseCase | ||
import org.sopt.and.domain.usecase.SignUpUseCase | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object UseCaseModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRegisterUserUseCase(userRepository: SignUpRepository): SignUpUseCase { | ||
return SignUpUseCase(userRepository) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideLoginUserUseCase(userRepository: SignInRepository): SignInUseCase { | ||
return SignInUseCase(userRepository) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideGetMyHobbyUseCase(userRepository: MyHobbyRepository): MyHobbyUseCase { | ||
return MyHobbyUseCase(userRepository) | ||
} | ||
} |
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,9 @@ | ||
package org.sopt.and.domain.entity | ||
|
||
sealed class BaseResult<out T> { | ||
data class Success<out T>(val data: T) : BaseResult<T>() | ||
data class Error( | ||
val message: String, | ||
val errorCode: String? = null | ||
) : BaseResult<Nothing>() | ||
} |
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,7 @@ | ||
package org.sopt.and.domain.entity | ||
|
||
data class UserData( | ||
val username: String, | ||
val password: String, | ||
val hobby: String | ||
) |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/org/sopt/and/domain/entity/UserLoginResult.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,5 @@ | ||
package org.sopt.and.domain.entity | ||
|
||
data class UserLoginResult( | ||
val token: String | ||
) |
21 changes: 5 additions & 16 deletions
21
app/src/main/java/org/sopt/and/domain/repository/SignInRepository.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 |
---|---|---|
@@ -1,21 +1,10 @@ | ||
package org.sopt.and.domain.repository | ||
|
||
import org.sopt.and.data.datasource.SignInDataSource | ||
import org.sopt.and.data.repositoryimpl.SignInRepositoryImpl | ||
import org.sopt.and.data.service.ServicePool | ||
import org.sopt.and.domain.model.SignInInformationEntity | ||
import org.sopt.and.domain.model.SignInResponseEntity | ||
|
||
interface SignInRepository { | ||
suspend fun signIn(request: SignInInformationEntity): Result<SignInResponseEntity> | ||
import org.sopt.and.domain.entity.BaseResult | ||
import org.sopt.and.domain.entity.UserLoginResult | ||
|
||
|
||
companion object { | ||
fun create(): SignInRepositoryImpl { | ||
return SignInRepositoryImpl( | ||
SignInDataSource( | ||
ServicePool.userService | ||
) | ||
) | ||
} | ||
} | ||
interface SignInRepository { | ||
suspend fun loginUser(user: org.sopt.and.domain.entity.UserData): BaseResult<UserLoginResult> | ||
} |
15 changes: 9 additions & 6 deletions
15
app/src/main/java/org/sopt/and/domain/usecase/SignInUseCase.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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package org.sopt.and.domain.usecase | ||
|
||
import org.sopt.and.domain.model.SignInInformationEntity | ||
import org.sopt.and.domain.model.SignInResponseEntity | ||
import org.sopt.and.domain.entity.BaseResult | ||
import org.sopt.and.domain.entity.UserData | ||
import org.sopt.and.domain.entity.UserLoginResult | ||
import org.sopt.and.domain.repository.SignInRepository | ||
import javax.inject.Inject | ||
|
||
class SignInUseCase( | ||
private val signInRepository: SignInRepository | ||
class SignInUseCase @Inject constructor( | ||
private val userLoginRepository: SignInRepository | ||
) { | ||
suspend operator fun invoke(request: SignInInformationEntity): Result<SignInResponseEntity> = | ||
signInRepository.signIn(request = request) | ||
suspend operator fun invoke(user: UserData): BaseResult<UserLoginResult> { | ||
return userLoginRepository.loginUser(user) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/sopt/and/presentation/signin/SignInContract.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,29 @@ | ||
package org.sopt.and.presentation.auth.signin | ||
|
||
import org.sopt.and.presentation.util.UiEffect | ||
import org.sopt.and.presentation.util.UiEvent | ||
import org.sopt.and.presentation.util.UiState | ||
|
||
class SignInContract { | ||
data class SignInUiState( | ||
val username: String = "", | ||
val password: String = "", | ||
val isLoading: Boolean = false, | ||
val errorMessage: String? = null | ||
) : UiState | ||
|
||
sealed class SignInUiEvent : UiEvent { | ||
data class UpdateUserName(val username: String) : SignInUiEvent() | ||
data class UpdatePassword(val password: String) : SignInUiEvent() | ||
data object SignInFormSubmit : SignInUiEvent() | ||
data object NavigateUp : SignInUiEvent() | ||
} | ||
|
||
sealed class SignInUiEffect : UiEffect { | ||
data object ShowSuccessSnackBar : SignInUiEffect() | ||
data class ShowErrorSnackBar(val message: String) : SignInUiEffect() | ||
data object NavigateToSignUp : SignInUiEffect() | ||
data object NavigateToMy : SignInUiEffect() | ||
data object NavigateUp : SignInUiEffect() | ||
} | ||
} |
Oops, something went wrong.