generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat/#19: week7 compose 과제 구현 #20
Open
youjin09222
wants to merge
27
commits into
develop-compose
Choose a base branch
from
feat/week7_compose-(#19)
base: develop-compose
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a6263c4
chore/#11: 인터넷 권한 추가
youjin09222 ee48933
chore/#11: 라이브러리 추가 및 BASE_URL 숨기기
youjin09222 991c7e0
add/#11: Dto 추가
youjin09222 415a9f8
add/#11: AuthService 추가
youjin09222 ce8733d
add/#11: UserService 추가
youjin09222 6f175a6
add/#11: UserData 추가
youjin09222 6ab7a9e
mod/#11: Friend data 변경
youjin09222 230598d
feat/#11: UserPreference 추가
youjin09222 f51e011
feat/#11: ApiFactory 추가
youjin09222 47c12ff
feat/#11: 회원가입 서버 통신
youjin09222 1938b2e
feat/#11: 로그인 서버 통신
youjin09222 18d2d22
feat/#11: 회원 정보 조회
youjin09222 012559a
feat/#11: 회원 정보 적용
youjin09222 3846b36
refactor/#11: Application Context 사용하여 SharedPreferences 초기화
youjin09222 9223db8
refactor/#11: shared preferences의 edit 사용
youjin09222 3c28258
refactor/#11: shared preferences 상수 사용
youjin09222 a998575
refactor/#11: @Serializable로 변경
youjin09222 5bdae0e
refactor/#11: LocalContext.current 사용
youjin09222 bd73dad
refactor/#11: isInitialized로 초기화 확인
youjin09222 d23af01
#15 [FEAT] AUTH_BASE_URL 네이밍 변경
youjin09222 5991e25
Merge pull request #16 from NOW-SOPT-ANDROID/feat/week6_compose
youjin09222 5bcf94e
FEAT/#19: SignUpRepository 구현
youjin09222 c0efb02
REFACTOR/#19: SignUpViewModel에서 SignUpRepository 사용
youjin09222 02d5575
FEAT/#19: LoginRepository 구현
youjin09222 544dc83
REFACTOR/#19: LoginViewModel에서 LoginRepository 사용
youjin09222 13ea9e4
FEAT/#19: UserProfileRepository 구현
youjin09222 f63ae10
REFACTOR/#19: UserProfileViewModel에서 UserProfileRepository 사용
youjin09222 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.sopt.now.compose.data | ||
|
||
import android.util.Log | ||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import com.sopt.now.BuildConfig | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Response | ||
import retrofit2.Retrofit | ||
import java.io.IOException | ||
|
||
object ApiFactory { | ||
private const val BASE_URL: String = BuildConfig.AUTH_BASE_URL | ||
lateinit var userPreference: UserPreference | ||
var isInitialized = false | ||
private set | ||
|
||
fun initializeUserPreference(userPreference: UserPreference) { | ||
ApiFactory.userPreference = userPreference | ||
} | ||
|
||
val retrofit: Retrofit by lazy { | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(provideOkHttpClient()) | ||
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
} | ||
|
||
private fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder() | ||
.addInterceptor(HeaderInterceptor()) | ||
.build() | ||
|
||
class HeaderInterceptor : Interceptor { | ||
@Throws(IOException::class) | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
if (!ApiFactory::userPreference.isInitialized) { | ||
throw IllegalStateException("UserPreference is not initialized") | ||
} | ||
|
||
val newRequest = chain.request().newBuilder() | ||
.addHeader("memberId", userPreference.getUserId().toString()) | ||
.build() | ||
Log.d("userPreference API", "${userPreference.getUserId().toString()}") | ||
return chain.proceed(newRequest) | ||
} | ||
} | ||
|
||
inline fun <reified T> create(): T = retrofit.create(T::class.java) | ||
} | ||
|
||
object ServicePool { | ||
val authService = ApiFactory.create<AuthService>() | ||
val userService = ApiFactory.create<UserService>() | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/sopt/now/compose/data/AuthService.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 com.sopt.now.compose.data | ||
|
||
import com.sopt.now.compose.data.dto.request.RequestLoginDto | ||
import com.sopt.now.compose.data.dto.request.RequestSignUpDto | ||
import com.sopt.now.compose.data.dto.response.ResponseAuthDto | ||
import retrofit2.Call | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface AuthService { | ||
@POST("member/join") | ||
fun signUp( | ||
@Body request: RequestSignUpDto, | ||
): Call<ResponseAuthDto> | ||
|
||
@POST("member/login") | ||
fun login( | ||
@Body request: RequestLoginDto, | ||
): Call<ResponseAuthDto> | ||
} |
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,12 @@ | ||
package com.sopt.now.compose.data | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class BaseState( | ||
@SerialName("isSuccess") | ||
val isSuccess: Boolean, | ||
@SerialName("message") | ||
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,14 @@ | ||
package com.sopt.now.compose.data | ||
|
||
import androidx.annotation.DrawableRes | ||
|
||
data class Friend( | ||
@DrawableRes val profileImage: Int, | ||
val name: String, | ||
val phone: String, | ||
) { | ||
companion object { | ||
const val TYPE_USER = 0 | ||
const val TYPE_FRIEND = 1 | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
app/src/main/java/com/sopt/now/compose/data/UserPreference.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,55 @@ | ||
package com.sopt.now.compose.data | ||
|
||
import android.content.Context | ||
|
||
import androidx.core.content.edit | ||
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.KEY_USER_ID | ||
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.KEY_USER_NAME | ||
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.KEY_USER_PHONE | ||
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.PREF_USER_DATA | ||
|
||
class UserPreference(context: Context) { | ||
private val sharedPreferences = context.applicationContext.getSharedPreferences(PREF_USER_DATA, Context.MODE_PRIVATE) | ||
|
||
// 사용자 아이디 저장 | ||
fun saveUserId(userId: String) { | ||
sharedPreferences.edit { | ||
putString(KEY_USER_ID, userId) | ||
} | ||
} | ||
|
||
// 사용자 아이디 가져오기 | ||
fun getUserId(): String? { | ||
return sharedPreferences.getString(KEY_USER_ID, null) | ||
} | ||
|
||
// 사용자 데이터 저장 | ||
fun saveUserData(userData: UserData) { | ||
sharedPreferences.edit { | ||
putString(KEY_USER_ID, userData.userId) | ||
putString(KEY_USER_NAME, userData.userName) | ||
putString(KEY_USER_PHONE, userData.userPhone) | ||
} | ||
} | ||
|
||
// 사용자 데이터 가져오기 | ||
fun getUserData(): UserData? { | ||
with(sharedPreferences){ | ||
val userId = getString("userId", null) | ||
val userName = getString("userName", null) | ||
val userPhone = getString("userPhone", null) | ||
|
||
return if (userId != null && userName != null && userPhone != null) { | ||
UserData(userId, userName, userPhone) | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
object UserPreferenceKeys { | ||
const val PREF_USER_DATA = "userData" | ||
const val KEY_USER_ID = "userId" | ||
const val KEY_USER_NAME = "userName" | ||
const val KEY_USER_PHONE = "userPhone" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/sopt/now/compose/data/UserService.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,11 @@ | ||
package com.sopt.now.compose.data | ||
|
||
import com.sopt.now.compose.data.dto.response.ResponseUserInfoDto | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
interface UserService { | ||
@GET("member/info") | ||
fun userInfo( | ||
): Call<ResponseUserInfoDto> | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/sopt/now/compose/data/dto/request/RequestLoginDto.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,12 @@ | ||
package com.sopt.now.compose.data.dto.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestLoginDto( | ||
@SerialName("authenticationId") | ||
val authenticationId: String, | ||
@SerialName("password") | ||
val password: String | ||
) |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/sopt/now/compose/data/dto/request/ResponseSignUpDto.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,16 @@ | ||
package com.sopt.now.compose.data.dto.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestSignUpDto( | ||
@SerialName("authenticationId") | ||
val authenticationId: String, | ||
@SerialName("password") | ||
val password: String, | ||
@SerialName("nickname") | ||
val nickname: String, | ||
@SerialName("phone") | ||
val phone: String, | ||
) |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/sopt/now/compose/data/dto/response/ResponseAuthDto.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,12 @@ | ||
package com.sopt.now.compose.data.dto.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponseAuthDto( | ||
@SerialName("code") | ||
val code: Int, | ||
@SerialName("message") | ||
val message: String, | ||
) |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/sopt/now/compose/data/dto/response/ResponseUserInfoDto.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,24 @@ | ||
package com.sopt.now.compose.data.dto.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponseUserInfoDto( | ||
@SerialName("code") | ||
val code: Int, | ||
@SerialName("message") | ||
val message: String, | ||
@SerialName("data") | ||
val data: UserInfo | ||
) | ||
|
||
@Serializable | ||
data class UserInfo( | ||
@SerialName("authenticationId") | ||
val authenticationId: String, | ||
@SerialName("nickname") | ||
val nickname: String, | ||
@SerialName("phone") | ||
val phone: String | ||
) |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/sopt/now/compose/data/repository/LoginRepository.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 com.sopt.now.compose.data.repository | ||
|
||
import com.sopt.now.compose.core.view.UiState | ||
import com.sopt.now.compose.data.ApiFactory | ||
import com.sopt.now.compose.data.ServicePool.authService | ||
import com.sopt.now.compose.data.dto.request.RequestLoginDto | ||
import com.sopt.now.compose.data.dto.response.ResponseAuthDto | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class LoginRepository { | ||
suspend fun postLogin(request: RequestLoginDto): UiState<ResponseAuthDto> { | ||
return withContext(Dispatchers.IO) { | ||
runCatching { | ||
authService.postLogin(request) | ||
}.fold( | ||
{ response -> | ||
val userId = response.headers()["location"] | ||
if (userId != null) { | ||
ApiFactory.userPreference.saveUserId(userId) | ||
} | ||
UiState.Success( | ||
response.body() ?: ResponseAuthDto( | ||
-1, | ||
"Empty response body" | ||
) | ||
) | ||
}, | ||
{ UiState.Failure(it.message.toString()) } | ||
) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/sopt/now/compose/data/repository/SignUpRepository.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,21 @@ | ||
package com.sopt.now.compose.data.repository | ||
|
||
import com.sopt.now.compose.core.view.UiState | ||
import com.sopt.now.compose.data.ServicePool | ||
import com.sopt.now.compose.data.dto.request.RequestSignUpDto | ||
import com.sopt.now.compose.data.dto.response.ResponseAuthDto | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class SignUpRepository { | ||
suspend fun postSignUp(requestSignUp: RequestSignUpDto): UiState<ResponseAuthDto> { | ||
return withContext(Dispatchers.IO) { | ||
runCatching { | ||
ServicePool.authService.postSignUp(requestSignUp) | ||
}.fold( | ||
{ UiState.Success(it) }, | ||
{ UiState.Failure(it.message.toString()) } | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 fold는 처음 보는데요? runCatching은 onSuccess/onFailure만 알고있었는데 이런 방법도 있군요!