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.
- Loading branch information
1 parent
f1f4111
commit a531a0f
Showing
7 changed files
with
191 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.sopt.and.api | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.sopt.and.BuildConfig | ||
import org.sopt.and.api.service.UserService | ||
import retrofit2.Retrofit | ||
|
||
object ApiFactory { | ||
private const val BASE_URL: String = BuildConfig.BASE_URL | ||
|
||
private val loggingInterceptor = HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
|
||
private val client = OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.build() | ||
|
||
val retrofit: Retrofit by lazy { | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(client) | ||
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
} | ||
|
||
inline fun <reified T> create(): T = retrofit.create(T::class.java) | ||
} | ||
|
||
object ServicePool { | ||
val userService = ApiFactory.create<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,10 @@ | ||
package org.sopt.and.api.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponseErrorDto( | ||
@SerialName("code") | ||
val code: 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,26 @@ | ||
package org.sopt.and.api.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponseUserSuccessDto( | ||
@SerialName("result") | ||
val result: UserData | ||
) | ||
|
||
@Serializable | ||
data class UserData( | ||
@SerialName("no") | ||
val no: Int | ||
) | ||
|
||
@Serializable | ||
data class RequestUserDto( | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("password") | ||
val password: String, | ||
@SerialName("hobby") | ||
val hobby: 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 org.sopt.and.api.service | ||
|
||
import okhttp3.ResponseBody | ||
import org.sopt.and.api.dto.RequestUserDto | ||
import retrofit2.Call | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface UserService { | ||
@POST("/user") | ||
fun postUser( | ||
@Body requestUser: RequestUserDto | ||
): Call<ResponseBody> | ||
} |
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
97 changes: 75 additions & 22 deletions
97
app/src/main/java/org/sopt/and/ui/signup/SignUpViewModel.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,34 +1,87 @@ | ||
package org.sopt.and.ui.signup | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.ResponseBody | ||
import org.sopt.and.api.ServicePool | ||
import org.sopt.and.api.dto.RequestUserDto | ||
import org.sopt.and.api.dto.ResponseErrorDto | ||
import org.sopt.and.api.dto.ResponseUserSuccessDto | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
|
||
class SignUpViewModel: ViewModel() { | ||
val PASSWORD_MIN_LENGTH = 8 | ||
val PASSWORD_MAX_LENGTH = 20 | ||
val PASSWORD_REGEX = Regex("^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@\$!%*?&])[A-Za-z\\d@\$!%*?&]{$PASSWORD_MIN_LENGTH,$PASSWORD_MAX_LENGTH}\$") | ||
class SignUpViewModel : ViewModel() { | ||
private val userService by lazy { ServicePool.userService } | ||
|
||
var sharedPreferences: SharedPreferences? = null | ||
// val PASSWORD_MIN_LENGTH = 8 | ||
// val PASSWORD_MAX_LENGTH = 20 | ||
// val PASSWORD_REGEX = | ||
// Regex("^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@\$!%*?&])[A-Za-z\\d@\$!%*?&]{$PASSWORD_MIN_LENGTH,$PASSWORD_MAX_LENGTH}\$") | ||
|
||
fun initializePreferences(context: Context){ | ||
sharedPreferences = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE) | ||
} | ||
// var sharedPreferences: SharedPreferences? = null | ||
// | ||
// fun initializePreferences(context: Context) { | ||
// sharedPreferences = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE) | ||
// } | ||
// | ||
// fun saveUserInfo(id: String, password: String) { | ||
// sharedPreferences?.edit()?.apply() { | ||
// putString("userId", id) | ||
// putString("userPassWord", password) | ||
// apply() | ||
// } | ||
// } | ||
|
||
fun saveUserInfo(id: String, password: String){ | ||
sharedPreferences?.edit()?.apply(){ | ||
putString("userId", id) | ||
putString("userPassWord", password) | ||
apply() | ||
} | ||
} | ||
// fun isAbleEmail(email: String): Boolean { | ||
// return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() | ||
// } | ||
// | ||
// fun isAblePassword(password: String): Boolean { | ||
// return PASSWORD_REGEX.matches(password) | ||
// } | ||
|
||
fun isAbleEmail(email: String): Boolean{ | ||
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() | ||
} | ||
fun signUpUser( | ||
username: String, | ||
password: String, | ||
hobby: String, | ||
onSuccess: () -> Unit, | ||
onFailure: (String) -> Unit | ||
) { | ||
val requestUserDto = RequestUserDto(username = username, password = password, hobby = hobby) | ||
|
||
userService.postUser(requestUserDto).enqueue(object : Callback<ResponseBody> { | ||
override fun onResponse( | ||
call: Call<ResponseBody>, | ||
response: Response<ResponseBody> | ||
) { | ||
if (response.isSuccessful) { | ||
val successBody = response.body()?.string() | ||
val successDto = Json.decodeFromString<ResponseUserSuccessDto>(successBody ?: "") | ||
onSuccess() | ||
} else { | ||
val errorBody = response.errorBody()?.string() | ||
val errorDto = errorBody?.let { Json.decodeFromString<ResponseErrorDto>(it) } | ||
val errorMessage = when (response.code()) { | ||
400 -> when (errorDto?.code) { | ||
"01" -> "닉네임, 비밀번호, 취미가 8자를 넘기면 안됩니다." | ||
else -> "잘못된 요청입니다." | ||
} | ||
409 -> when (errorDto?.code) { | ||
"00" -> "닉네임이 중복됩니다." | ||
else -> "충돌이 발생했습니다." | ||
} | ||
else -> "알 수 없는 오류가 발생했습니다." | ||
} | ||
onFailure(errorMessage) | ||
} | ||
} | ||
|
||
fun isAblePassword(password: String): Boolean{ | ||
return PASSWORD_REGEX.matches(password) | ||
override fun onFailure(call: Call<ResponseBody>, t: Throwable) { | ||
Log.e("SignUpViewModel", "Failure: ${t.message}") | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
<string name="signup_id">[email protected]</string> | ||
<string name="signup_id_explain">로그인, 비밀번호 찾기, 알림에 사용되니 정확한 이메일을 입력해주세요.</string> | ||
<string name="signup_password">Wavve 비밀번호 설정</string> | ||
<string name="signup_hobby">취미 설정</string> | ||
<string name="signup_password_explain">비밀번호는 8-20자 이내로 영문 대소문자, 숫자, 특수문자 중 3가지 이상 혼용하여 입력해 주세요.</string> | ||
<string name="signup_button">Wavve 회원가입</string> | ||
<string name="signup_success">회원가입 성공</string> | ||
|