feat(access): 공통 접근 실행기를 추가한다
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
package kr.co.vividnext.sodalive.v2.access
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.google.gson.Gson
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.common.Constants
|
||||
import kr.co.vividnext.sodalive.mypage.MyPageViewModel
|
||||
import kr.co.vividnext.sodalive.mypage.auth.Auth
|
||||
import kr.co.vividnext.sodalive.mypage.auth.AuthVerifyRequest
|
||||
import kr.co.vividnext.sodalive.mypage.auth.BootpayResponse
|
||||
import kr.co.vividnext.sodalive.settings.ContentSettingsActivity
|
||||
import kr.co.vividnext.sodalive.splash.SplashActivity
|
||||
import kr.co.vividnext.sodalive.user.login.LoginActivity
|
||||
import kr.co.vividnext.sodalive.v2.components.modal.V2ModalDialog
|
||||
import org.koin.android.ext.android.get
|
||||
|
||||
class V2AccessGuard(
|
||||
private val policy: AccessPolicy = AccessPolicy(),
|
||||
private val stateProvider: AccessStateProvider = SharedPreferenceAccessStateProvider()
|
||||
) {
|
||||
fun ensureAccess(
|
||||
activity: Activity,
|
||||
requirement: AccessRequirement,
|
||||
onAllowed: () -> Unit = {}
|
||||
): Boolean {
|
||||
return handleDecision(
|
||||
activity = activity,
|
||||
requirement = requirement,
|
||||
startLogin = { startLoginActivity(activity) },
|
||||
onAllowed = onAllowed
|
||||
)
|
||||
}
|
||||
|
||||
fun ensureAccess(
|
||||
fragment: Fragment,
|
||||
requirement: AccessRequirement,
|
||||
onAllowed: () -> Unit = {}
|
||||
): Boolean {
|
||||
val activity = fragment.requireActivity()
|
||||
return handleDecision(
|
||||
activity = activity,
|
||||
requirement = requirement,
|
||||
startLogin = { startLoginActivity(activity) },
|
||||
onAllowed = onAllowed
|
||||
)
|
||||
}
|
||||
|
||||
fun isAllowed(requirement: AccessRequirement): Boolean {
|
||||
return policy.decide(requirement, stateProvider.current()) == AccessDecision.Allowed
|
||||
}
|
||||
|
||||
private fun handleDecision(
|
||||
activity: Activity,
|
||||
requirement: AccessRequirement,
|
||||
startLogin: () -> Unit,
|
||||
onAllowed: () -> Unit
|
||||
): Boolean {
|
||||
return executeAccessDecision(
|
||||
decision = policy.decide(requirement, stateProvider.current()),
|
||||
onAllowed = onAllowed,
|
||||
onLoginRequired = startLogin,
|
||||
onAdultVerificationRequired = { showAuthDialog(activity) },
|
||||
onAdultContentSettingRequired = { startContentSettings(activity) }
|
||||
)
|
||||
}
|
||||
|
||||
private fun startLoginActivity(activity: Activity) {
|
||||
activity.startActivity(
|
||||
Intent(activity, LoginActivity::class.java).apply {
|
||||
putExtra(Constants.EXTRA_DATA, activity.intent.extras)
|
||||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun showAuthDialog(activity: Activity) {
|
||||
V2ModalDialog(
|
||||
activity = activity,
|
||||
layoutInflater = activity.layoutInflater,
|
||||
title = activity.getString(R.string.auth_title),
|
||||
desc = activity.getString(R.string.auth_desc_live),
|
||||
confirmButtonTitle = activity.getString(R.string.auth_go),
|
||||
confirmButtonClick = { startAuthFlow(activity) },
|
||||
cancelButtonTitle = activity.getString(R.string.cancel),
|
||||
cancelButtonClick = {}
|
||||
).show(activity.resources.displayMetrics.widthPixels)
|
||||
}
|
||||
|
||||
private fun startAuthFlow(activity: Activity) {
|
||||
val myPageViewModel: MyPageViewModel = activity.get()
|
||||
Auth.auth(activity, activity) { json ->
|
||||
val bootpayResponse = Gson().fromJson(json, BootpayResponse::class.java)
|
||||
val request = AuthVerifyRequest(receiptId = bootpayResponse.data.receiptId)
|
||||
activity.runOnUiThread {
|
||||
if (activity.isFinishing || activity.isDestroyed) return@runOnUiThread
|
||||
myPageViewModel.authVerify(request) {
|
||||
if (activity.isFinishing || activity.isDestroyed) return@authVerify
|
||||
activity.startActivity(
|
||||
Intent(activity, SplashActivity::class.java).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
)
|
||||
activity.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startContentSettings(activity: Activity) {
|
||||
activity.startActivity(
|
||||
Intent(activity, ContentSettingsActivity::class.java).apply {
|
||||
putExtra(Constants.EXTRA_SHOW_SENSITIVE_CONTENT_GUIDE, true)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun executeAccessDecision(
|
||||
decision: AccessDecision,
|
||||
onAllowed: () -> Unit,
|
||||
onLoginRequired: () -> Unit,
|
||||
onAdultVerificationRequired: () -> Unit,
|
||||
onAdultContentSettingRequired: () -> Unit
|
||||
): Boolean {
|
||||
return when (decision) {
|
||||
AccessDecision.Allowed -> {
|
||||
onAllowed()
|
||||
true
|
||||
}
|
||||
AccessDecision.LoginRequired -> {
|
||||
onLoginRequired()
|
||||
false
|
||||
}
|
||||
AccessDecision.AdultVerificationRequired -> {
|
||||
onAdultVerificationRequired()
|
||||
false
|
||||
}
|
||||
AccessDecision.AdultContentSettingRequired -> {
|
||||
onAdultContentSettingRequired()
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Activity.ensureV2Access(
|
||||
requirement: AccessRequirement,
|
||||
onAllowed: () -> Unit = {}
|
||||
): Boolean = V2AccessGuard().ensureAccess(this, requirement, onAllowed)
|
||||
|
||||
fun Fragment.ensureV2Access(
|
||||
requirement: AccessRequirement,
|
||||
onAllowed: () -> Unit = {}
|
||||
): Boolean = V2AccessGuard().ensureAccess(this, requirement, onAllowed)
|
||||
|
||||
fun isV2AccessAllowed(requirement: AccessRequirement): Boolean {
|
||||
return V2AccessGuard().isAllowed(requirement)
|
||||
}
|
||||
Reference in New Issue
Block a user