-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from snuhcs-course/refactor/strategy
Strategy 패턴 적용
- Loading branch information
Showing
6 changed files
with
171 additions
and
146 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
23 changes: 23 additions & 0 deletions
23
frontend/app/src/main/java/com/example/speechbuddy/viewmodel/strategy/NavigationSendCode.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,23 @@ | ||
package com.example.speechbuddy.viewmodel.strategy | ||
|
||
import com.example.speechbuddy.repository.AuthRepository | ||
import com.example.speechbuddy.utils.ResponseHandler | ||
import com.example.speechbuddy.viewmodel.EmailVerificationViewModel | ||
|
||
class NavigationSendCode { | ||
private lateinit var sendCodeStrategy: SendCodeStrategy | ||
private lateinit var source : String | ||
|
||
fun setSource(source: String) { | ||
this.source = source | ||
if(source=="signup") { | ||
sendCodeStrategy = SendCodeForSignupStrategy() | ||
} else if(source=="reset_password") { | ||
sendCodeStrategy = SendCodeForResetPasswordStrategy() | ||
} | ||
} | ||
|
||
fun sendCode(viewModel: EmailVerificationViewModel, repository: AuthRepository, responseHandler: ResponseHandler) { | ||
sendCodeStrategy.sendCode(viewModel, repository, responseHandler) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../main/java/com/example/speechbuddy/viewmodel/strategy/SendCodeForResetPasswordStrategy.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,49 @@ | ||
package com.example.speechbuddy.viewmodel.strategy | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import com.example.speechbuddy.R | ||
import com.example.speechbuddy.data.remote.requests.AuthSendCodeRequest | ||
import com.example.speechbuddy.repository.AuthRepository | ||
import com.example.speechbuddy.utils.ResponseCode | ||
import com.example.speechbuddy.utils.ResponseHandler | ||
import com.example.speechbuddy.viewmodel.EmailVerificationViewModel | ||
import kotlinx.coroutines.launch | ||
|
||
class SendCodeForResetPasswordStrategy: SendCodeStrategy { | ||
override fun sendCode(viewModel: EmailVerificationViewModel, repository: AuthRepository, responseHandler: ResponseHandler) { | ||
viewModel.changeLoadingState() | ||
viewModel.viewModelScope.launch { | ||
repository.sendCodeForResetPassword( | ||
AuthSendCodeRequest( | ||
email = viewModel.emailInput | ||
) | ||
).collect { result -> | ||
viewModel.changeLoadingState() | ||
when (result.code()) { | ||
ResponseCode.SUCCESS.value -> { | ||
viewModel.handleSendCodeSuccess() | ||
} | ||
|
||
ResponseCode.BAD_REQUEST.value -> { | ||
val errorMessageId = | ||
when (responseHandler.parseErrorResponse(result.errorBody()!!).key) { | ||
"email" -> R.string.wrong_email | ||
"no_user" -> R.string.unregistered_email | ||
else -> R.string.unknown_error | ||
} | ||
viewModel.handleSendCodeBadRequest(errorMessageId) | ||
} | ||
|
||
ResponseCode.NO_INTERNET_CONNECTION.value -> { | ||
viewModel.handleSendCodeNoInternetConnection() | ||
} | ||
|
||
else -> { | ||
viewModel.handleSendCodeUnknownError() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
...app/src/main/java/com/example/speechbuddy/viewmodel/strategy/SendCodeForSignupStrategy.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,48 @@ | ||
package com.example.speechbuddy.viewmodel.strategy | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import com.example.speechbuddy.R | ||
import com.example.speechbuddy.data.remote.requests.AuthSendCodeRequest | ||
import com.example.speechbuddy.repository.AuthRepository | ||
import com.example.speechbuddy.utils.ResponseCode | ||
import com.example.speechbuddy.utils.ResponseHandler | ||
import com.example.speechbuddy.viewmodel.EmailVerificationViewModel | ||
import kotlinx.coroutines.launch | ||
|
||
class SendCodeForSignupStrategy: SendCodeStrategy { | ||
override fun sendCode(viewModel: EmailVerificationViewModel, repository: AuthRepository, responseHandler: ResponseHandler) { | ||
viewModel.changeLoadingState() | ||
viewModel.viewModelScope.launch { | ||
repository.sendCodeForSignup( | ||
AuthSendCodeRequest( | ||
email = viewModel.emailInput | ||
) | ||
).collect { result -> | ||
viewModel.changeLoadingState() | ||
when (result.code()) { | ||
ResponseCode.SUCCESS.value -> { | ||
viewModel.handleSendCodeSuccess() | ||
} | ||
|
||
ResponseCode.BAD_REQUEST.value -> { | ||
val errorMessageId = | ||
when (responseHandler.parseErrorResponse(result.errorBody()!!).key) { | ||
"email" -> R.string.wrong_email | ||
"already_taken" -> R.string.email_already_taken | ||
else -> R.string.unknown_error | ||
} | ||
viewModel.handleSendCodeBadRequest(errorMessageId) | ||
} | ||
|
||
ResponseCode.NO_INTERNET_CONNECTION.value -> { | ||
viewModel.handleSendCodeNoInternetConnection() | ||
} | ||
|
||
else -> { | ||
viewModel.handleSendCodeUnknownError() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
frontend/app/src/main/java/com/example/speechbuddy/viewmodel/strategy/SendCodeStrategy.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 com.example.speechbuddy.viewmodel.strategy | ||
|
||
import com.example.speechbuddy.repository.AuthRepository | ||
import com.example.speechbuddy.utils.ResponseHandler | ||
import com.example.speechbuddy.viewmodel.EmailVerificationViewModel | ||
|
||
interface SendCodeStrategy { | ||
fun sendCode(viewModel: EmailVerificationViewModel, repository: AuthRepository, responseHandler: ResponseHandler) | ||
} |
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