-
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/#9: week4 compose 과제 구현 #12
base: develop-compose
Are you sure you want to change the base?
Changes from all commits
a6263c4
ee48933
991c7e0
415a9f8
ce8733d
6f175a6
6ab7a9e
230598d
f51e011
47c12ff
1938b2e
18d2d22
012559a
3846b36
9223db8
3c28258
a998575
5bdae0e
bd73dad
d23af01
5991e25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 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>() | ||
} |
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> | ||
} |
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 | ||
) | ||
Comment on lines
+6
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 데이터는 UI에서 사용되는 것으로 알고 있는데 이 데이터 클래스에 Serializable 붙인 이유를 알 수 있을까요? |
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 | ||
} | ||
} |
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) | ||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 상수들도 따로 빼둔다면 추후 수정이 용이해집니당 |
||
|
||
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" | ||
} | ||
} |
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> | ||
} |
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 | ||
) |
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, | ||
) |
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, | ||
) |
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 | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.sopt.now.compose.data | ||
|
||
import android.os.Parcelable | ||
import kotlinx.android.parcel.Parcelize | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class UserData( | ||
val userId: String, | ||
val userName: String, | ||
val userPhone: String | ||
) |
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.
ApiFactory가 userPreferences를 가지고 있어야 하는 이유가 무엇일까요? ApiFactory라는 클래스(오브젝트)는 어떤 역할을 수행하고 있을까요?