-
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/week6 essential #11
base: develop
Are you sure you want to change the base?
Changes from all commits
9998234
15ed22d
7ca105e
108913b
eb1e6b0
a45a98e
17d2e78
7d7c35a
387932c
aa1f61b
d29b3a3
3da6983
f8c7af2
b5522dd
5ea02c7
ab5e21f
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,49 @@ | ||
package org.sopt.dosopttemplate.api | ||
|
||
import android.util.Log | ||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.sopt.dosopttemplate.BuildConfig | ||
import retrofit2.Retrofit | ||
|
||
object ApiFactory { | ||
lateinit var url: String | ||
private fun getLogOkHttpClient(): Interceptor { | ||
val loggingInterceptor = HttpLoggingInterceptor { message -> | ||
Log.d("Retrofit2", "CONNECTION INFO -> $message") | ||
} | ||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | ||
return loggingInterceptor | ||
} | ||
|
||
val okHttpClient = OkHttpClient.Builder() | ||
.addInterceptor(getLogOkHttpClient()) | ||
.build() | ||
|
||
val retrofit: Retrofit by lazy { | ||
Retrofit.Builder() | ||
.baseUrl(url) | ||
.client(okHttpClient) | ||
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
} | ||
|
||
inline fun <reified T> create(url: String): T { | ||
this.url = url | ||
return retrofit.create<T>(T::class.java) | ||
} | ||
} | ||
|
||
object ServicePool { | ||
private const val BASE_URL = BuildConfig.AUTH_BASE_URL | ||
private const val REQRES_BASE_URL = BuildConfig.REQRES_BASE_URL | ||
|
||
val authService = ApiFactory.create<AuthService>(BASE_URL) | ||
val followerService = ApiFactory.create<FollowerService>(REQRES_BASE_URL) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.sopt.dosopttemplate.api | ||
|
||
import retrofit2.Call | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface AuthService { | ||
@POST("api/v1/members/sign-in") | ||
suspend fun login( | ||
@Body request: RequestLoginDto, | ||
): Response<ResponseLoginDto> | ||
|
||
@POST("api/v1/members") | ||
suspend fun signUp( | ||
@Body request: RequestSignUpDto, | ||
): Response<Unit> | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.sopt.dosopttemplate.api// 보μλ©΄ μκ² μ§λ§ LiveDataλ lifecycle λΌμ΄λΈλ¬λ¦¬μ λ€μ΄μμ΅λλ€! | ||
// μλΉμ€ κ°μ²΄λ₯Ό λ°λ‘ λ§λ€μ§ μκ³ λ°λ‘ λΆλ¬μμ΅λλ€. | ||
// μΈμ λ κ·Έλ λ― Callκ³Ό Callbackμ Retrofit2μ κ²μ μ¬μ©ν΄μΌ ν©λλ€. okhttp μλ μ£Όμ! | ||
import android.util.Log | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
import org.sopt.dosopttemplate.api.ServicePool.authService | ||
import org.sopt.dosopttemplate.util.UtilClass.isIdConditionSatisfied | ||
import org.sopt.dosopttemplate.util.UtilClass.isPasswordConditionSatisfied | ||
|
||
class AuthViewModel : ViewModel() { | ||
// MutableLiveDataλ₯Ό μ¬μ©νμ¬ login result κ°μ²΄λ₯Ό μμ±ν©λλ€. | ||
|
||
private val _loginResult: MutableLiveData<ResponseLoginDto> = MutableLiveData() | ||
val loginResult: MutableLiveData<ResponseLoginDto> get() = _loginResult | ||
|
||
private val _loginSuccess: MutableLiveData<Boolean> = MutableLiveData() | ||
val loginSuccess: MutableLiveData<Boolean> get() = _loginSuccess | ||
|
||
private val _signUpResult: MutableLiveData<Unit> = MutableLiveData() | ||
val signUpResult: MutableLiveData<Unit> get() = _signUpResult | ||
|
||
private val _signUpSuccess: MutableLiveData<Boolean> = MutableLiveData() | ||
val signUpSuccess: MutableLiveData<Boolean> get() = _signUpSuccess | ||
|
||
val isLoginButtonClicked: MutableLiveData<Boolean> = MutableLiveData(false) | ||
val idConditionSatisfied: MutableLiveData<Boolean> = MutableLiveData(false) | ||
val passwordConditionSatisfied: MutableLiveData<Boolean> = MutableLiveData(false) | ||
val loginConditionSatisfied: MutableLiveData<Boolean> = MutableLiveData(idConditionSatisfied.value?:false && passwordConditionSatisfied.value?:false) | ||
Comment on lines
+28
to
+31
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. μλ κ² λΌμ΄λΈλ°μ΄ν° μμνλ κ±°κ΅°μ!!
Comment on lines
+28
to
+31
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. μλ‘μ§λ€ λ°μ΄ν° λ°μΈλ©μ νμ©νμ λ€λ©΄ xmlλ‘ μ΄μ νλ λ°©λ²λ μλλ° λͺ¨λ₯΄μ ¨λ€λ©΄ νλ² μ¬μ©ν΄λ³΄λ λ°©λ²λ μ’μΈκ±° κ°μ΅λλ€. |
||
|
||
val _id: MutableLiveData<String> = MutableLiveData() | ||
val id: String get() = _id.value ?: "" | ||
val _password: MutableLiveData<String> = MutableLiveData() | ||
val password: String get() = _password.value ?: "" | ||
val _nickName: MutableLiveData<String> = MutableLiveData() | ||
val nickName: String get() = _nickName.value ?: "" | ||
Comment on lines
+33
to
+38
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. λ°λ³΅λλ μμ μ ν λ²μ μ²λ¦¬ν μλ μμκΉμ? ν λ² κ³ λ―Όν΄λ³΄μΈμ! |
||
|
||
fun login() { | ||
viewModelScope.launch { | ||
kotlin.runCatching { | ||
authService.login(RequestLoginDto(id, password)) | ||
}.onSuccess { | ||
if (it.isSuccessful) { | ||
loginResult.value = it.body() | ||
loginSuccess.value = true | ||
} else { | ||
loginSuccess.value = false | ||
} | ||
}.onFailure { | ||
// μλ¬ μ²λ¦¬ | ||
} | ||
} | ||
} | ||
|
||
fun signUp() { | ||
viewModelScope.launch { | ||
kotlin.runCatching { | ||
authService.signUp(RequestSignUpDto(id, nickName, password)) | ||
}.onSuccess { | ||
if (it.isSuccessful) { | ||
signUpResult.value = it.body() | ||
signUpSuccess.value = true | ||
} else { | ||
signUpSuccess.value = false | ||
} | ||
}.onFailure { | ||
// μλ¬ μ²λ¦¬ | ||
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. λ‘κ·Έ μΆλ ₯λ νλμ λ°©λ²μ λλ€! |
||
} | ||
} | ||
} | ||
Comment on lines
+58
to
+72
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. runCatchingμΌλ‘ μλ² μλ΅ νΈλ€λ§νμ€κ±°λΌλ©΄ λ°νκ°μ΄ Response κ΅¬μ‘°κ° μλ DTO κ΅¬μ‘°λ‘ λ³κ²½ν΄μΌν κ² κ°μ΅λλ€~!! |
||
|
||
|
||
fun onLoginButtonClick() { | ||
isLoginButtonClicked.value = true | ||
} | ||
|
||
fun onIDTextChanged() { | ||
idConditionSatisfied.value = isIdConditionSatisfied(id) | ||
loginConditionSatisfied.value = idConditionSatisfied.value?:false && passwordConditionSatisfied.value?:false | ||
} | ||
|
||
fun onPasswordTextChanged() { | ||
passwordConditionSatisfied.value = isPasswordConditionSatisfied(password) | ||
loginConditionSatisfied.value = idConditionSatisfied.value?:false && passwordConditionSatisfied.value?:false | ||
Log.v("logincondition", loginConditionSatisfied.value.toString()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.sopt.dosopttemplate.api | ||
|
||
import org.sopt.dosopttemplate.BuildConfig | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
private const val REQURES_BASE_URL = BuildConfig.REQRES_BASE_URL | ||
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. μλΆλΆ λ νΈλ‘νμΌλ‘ μ΄λν΄λ μ’μλ§ |
||
|
||
interface FollowerService { | ||
@GET(REQURES_BASE_URL) | ||
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. μ λΆλΆμ AuthServiceμ λ€λ₯΄κ² BASE_URLμ λ£μ μ΄μ κ° λκ°μ? BASE_URLμ μ΄ FollowerServiceλ₯Ό μ¬μ©νλ ServicePoolμμ λ£μ΄μ£Όλ κ²μΌλ‘ 보μ΄λλ°! |
||
fun getFollowerList(): Call<ResponseGetFollwerDto> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.sopt.dosopttemplate.api | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestLoginDto( | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("password") | ||
val password: String, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.sopt.dosopttemplate.api | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestSignUpDto( | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("nickname") | ||
val nickname: String, | ||
@SerialName("password") | ||
val password: String, | ||
) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,42 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
package org.sopt.dosopttemplate.api | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import kotlinx.serialization.SerialName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import kotlinx.serialization.Serializable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Serializable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data class ResponseGetFollwerDto( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
μ λ μ΄λ°μμΌλ‘ data class κ³μΈ΅μ μ£Όμ΄μ μ’ μλλλ‘ ννν΄λμμ΄μ! λ¬Όλ‘ μ΄κ²μ μ·¨ν₯μ°¨μ΄μ λλ€ ~~~ |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("page") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val page: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("per_page") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val perPage: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("total") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val total: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("total_pages") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val totalPages: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("data") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val data: List<ResponseData>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("support") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val support: ResponseSupport | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Serializable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data class ResponseSupport( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("url") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val url: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("text") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val text: String | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Serializable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data class ResponseData( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("id") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val id: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("email") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val email: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("first_name") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val firstName: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("last_name") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val lastName: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@SerialName("avatar") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val avatar: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. μ΄λ κ² ν λ²μ μ°λκΉ λ κΉλν κ² κ°μμ! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.sopt.dosopttemplate.api | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponseLoginDto( | ||
@SerialName("id") | ||
val id: Int, | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("nickname") | ||
val nickname: String, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.sopt.dosopttemplate.api | ||
|
||
sealed class SignupResponse { | ||
data class ResponseSignUpDto( | ||
val location: 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.
μ£Όμ μ²λ¦¬κ° μλͺ» λ κ² κ°μ΅λλ€..?