generated from NOW-SOPT-ANDROID/now-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.
- Loading branch information
1 parent
422db96
commit 2cf484b
Showing
33 changed files
with
635 additions
and
15 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
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
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
39 changes: 39 additions & 0 deletions
39
core/data/src/main/java/org/sopt/data/paging/UserPagingSource.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.data.paging | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import org.sopt.model.ReqresUser | ||
import org.sopt.network.api.ReqresApi | ||
import org.sopt.network.model.response.toReqresUser | ||
import retrofit2.HttpException | ||
import java.io.IOException | ||
import javax.inject.Inject | ||
|
||
class UserPagingSource @Inject constructor( | ||
private val reqresApi: ReqresApi, | ||
) : PagingSource<Int, ReqresUser>() { | ||
override fun getRefreshKey(state: PagingState<Int, ReqresUser>): Int? { | ||
return state.anchorPosition | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ReqresUser> { | ||
return try { | ||
val currentPage = params.key ?: 1 | ||
val users = toReqresUser( | ||
reqresApi.getUsers( | ||
currentPage | ||
).data | ||
) | ||
|
||
LoadResult.Page( | ||
data = users, | ||
prevKey = if (currentPage == 1) null else currentPage - 1, | ||
nextKey = if (users.isEmpty()) null else currentPage + 1 | ||
) | ||
} catch (exception: IOException) { | ||
return LoadResult.Error(exception) | ||
} catch (exception: HttpException) { | ||
return LoadResult.Error(exception) | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
core/data/src/main/java/org/sopt/data/repository/AuthRepositoryImpl.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,87 @@ | ||
package org.sopt.data.repository | ||
|
||
import kotlinx.serialization.json.Json | ||
import org.sopt.domain.repo.AuthRepository | ||
import org.sopt.model.Base | ||
import org.sopt.model.Member | ||
import org.sopt.network.api.AuthApi | ||
import org.sopt.network.model.request.RequestPatchPasswordDto | ||
import org.sopt.network.model.request.RequestPostSigninDto | ||
import org.sopt.network.model.request.RequestPostSignupDto | ||
import org.sopt.network.model.response.base.ApiError | ||
import org.sopt.network.model.response.base.BaseResponse | ||
import org.sopt.network.model.response.base.toCoreModel | ||
import org.sopt.network.model.response.base.toCoreModelNothingType | ||
import org.sopt.network.model.response.toCoreModel | ||
import retrofit2.HttpException | ||
import javax.inject.Inject | ||
|
||
class AuthRepositoryImpl @Inject constructor( | ||
private val authApi: AuthApi, | ||
) : AuthRepository { | ||
override suspend fun postSignup(member: Member, pw: String): Result<Base<Nothing>> { | ||
val result = runCatching { | ||
authApi.postSignup( | ||
RequestPostSignupDto( | ||
authenticationId = member.id, | ||
nickname = member.nickname, | ||
password = pw, | ||
phone = member.phone | ||
) | ||
) | ||
} | ||
return handleResult(result) | ||
} | ||
|
||
override suspend fun getUserinfo(): Result<Member> = runCatching { | ||
authApi.getUserinfo().data!!.toCoreModel() | ||
} | ||
|
||
override suspend fun postSignin(id: String, pw: String): Result<Base<Nothing>> { | ||
val result = runCatching { | ||
authApi.postSignin( | ||
RequestPostSigninDto( | ||
id, | ||
pw | ||
) | ||
) | ||
} | ||
return handleResult(result) | ||
} | ||
|
||
override suspend fun patchPassword( | ||
previousPassword: String, | ||
newPassword: String, | ||
newPasswordVerification: String, | ||
): Result<Base<Nothing>> { | ||
val result = runCatching { | ||
authApi.patchPassword( | ||
RequestPatchPasswordDto( | ||
previousPassword = previousPassword, | ||
newPassword = newPassword, | ||
newPasswordVerification = newPasswordVerification | ||
) | ||
) | ||
} | ||
return handleResult(result) | ||
} | ||
|
||
private fun handleResult(result: Result<BaseResponse<Unit>>) = | ||
when (val exception = result.exceptionOrNull()) { | ||
null -> result.map { | ||
it.toCoreModelNothingType() | ||
} | ||
|
||
is HttpException -> { | ||
val json = exception.response()?.errorBody()?.string() | ||
val api = Json.decodeFromString<ApiError>(json!!) | ||
Result.success( | ||
api.toCoreModel() | ||
) | ||
} | ||
|
||
else -> { | ||
Result.failure(exception) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
core/data/src/main/java/org/sopt/data/repository/UserRepositoryImpl.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.data.repository | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import kotlinx.coroutines.flow.Flow | ||
import org.sopt.data.paging.UserPagingSource | ||
import org.sopt.domain.repo.UserRepository | ||
import org.sopt.model.ReqresUser | ||
import javax.inject.Inject | ||
|
||
class UserRepositoryImpl @Inject constructor( | ||
private val userPagingSource: UserPagingSource, | ||
) : UserRepository { | ||
override fun getUser(): Flow<PagingData<ReqresUser>> { | ||
return Pager(PagingConfig(6, 2)) { | ||
userPagingSource | ||
}.flow | ||
} | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
core/domain/src/main/java/org/sopt/domain/repo/AuthRepository.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,18 @@ | ||
package org.sopt.domain.repo | ||
|
||
import org.sopt.model.Base | ||
import org.sopt.model.Member | ||
|
||
interface AuthRepository { | ||
suspend fun postSignup(member: Member, pw: String): Result<Base<Nothing>> | ||
|
||
suspend fun getUserinfo(): Result<Member?> | ||
|
||
suspend fun postSignin(id: String, pw: String): Result<Base<Nothing>> | ||
|
||
suspend fun patchPassword( | ||
previousPassword: String, | ||
newPassword: String, | ||
newPasswordVerification: String, | ||
): Result<Base<Nothing>> | ||
} |
9 changes: 9 additions & 0 deletions
9
core/domain/src/main/java/org/sopt/domain/repo/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,9 @@ | ||
package org.sopt.domain.repo | ||
|
||
import androidx.paging.PagingData | ||
import kotlinx.coroutines.flow.Flow | ||
import org.sopt.model.ReqresUser | ||
|
||
interface UserRepository { | ||
fun getUser(): Flow<PagingData<ReqresUser>> | ||
} |
18 changes: 18 additions & 0 deletions
18
core/domain/src/main/java/org/sopt/domain/usecase/ValidatePasswordUseCase.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,18 @@ | ||
package org.sopt.domain.usecase | ||
|
||
import org.sopt.model.ValidateResult | ||
import org.sopt.model.getResult | ||
import javax.inject.Inject | ||
|
||
class ValidatePasswordUseCase @Inject constructor() { | ||
operator fun invoke(password: String): ValidateResult { | ||
if (password.isBlank()) return ValidateResult.EmptyError | ||
return getResult(passwordPattern.matches(password)) | ||
} | ||
|
||
companion object { | ||
val passwordPattern = Regex( | ||
"""^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!@#$%^&*()-_=+{};:,<>/~`|'"\[\]\\?\.]).{8,}$""" | ||
) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
core/domain/src/main/java/org/sopt/domain/usecase/ValidatePhoneNumberUseCase.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,18 @@ | ||
package org.sopt.domain.usecase | ||
|
||
import org.sopt.model.ValidateResult | ||
import org.sopt.model.getResult | ||
import javax.inject.Inject | ||
|
||
class ValidatePhoneNumberUseCase @Inject constructor() { | ||
operator fun invoke(phone: String): ValidateResult { | ||
if (phone.isBlank()) return ValidateResult.EmptyError | ||
return getResult(phoneNumberPattern.matches(phone)) | ||
} | ||
|
||
companion object { | ||
val phoneNumberPattern = Regex( | ||
"""^01[0-9]-[0-9]{3,4}-[0-9]{4}$""" | ||
) | ||
} | ||
} |
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,8 @@ | ||
package org.sopt.model | ||
|
||
data class Base<T>( | ||
val code: Int, | ||
val data: T? = null, | ||
val message: String, | ||
) | ||
|
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.model | ||
|
||
data class Member( | ||
val id: String, | ||
val nickname: String, | ||
val phone: String, | ||
) |
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.model | ||
|
||
data class ReqresUser( | ||
val id: Int? = null, | ||
val email: String? = null, | ||
val firstName: String? = null, | ||
val lastName: String? = null, | ||
val avatar: String? = null, | ||
) |
Oops, something went wrong.