Skip to content

Commit

Permalink
#121 / 인앱 업데이트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sub101 committed Feb 18, 2024
1 parent 9b96f12 commit f37a9c9
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,6 @@ dependencies {
// Splash Screen
implementation 'androidx.core:core-splashscreen:1.0.1'

// In App Update
implementation 'com.google.android.play:app-update-ktx:2.1.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ object SparkleStorage {
)
}

fun setUpdateAvailableBoolean(key: String, value: Boolean) {
pref.edit { putBoolean(key, value) }
}

fun getUpdateAvailableBoolean(key: String): Boolean {
return pref.getBoolean(key, false)
}

var accessToken: String?
get() = pref.getString(ACCESS_TOKEN, null)
set(value) = pref.edit { putString(ACCESS_TOKEN, value).apply() }
Expand Down
68 changes: 68 additions & 0 deletions app/src/main/java/sopt/uni/presentation/SplashActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package sopt.uni.presentation

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.install.model.AppUpdateType
import com.google.android.play.core.install.model.UpdateAvailability
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import sopt.uni.R
import sopt.uni.data.datasource.local.SparkleStorage
import sopt.uni.databinding.ActivitySplashBinding
import sopt.uni.presentation.home.HomeActivity
import sopt.uni.presentation.invite.NickNameActivity
import sopt.uni.presentation.onboarding.OnBoardingActivity
import sopt.uni.util.binding.BindingActivity
import sopt.uni.util.extension.startActivity

@AndroidEntryPoint
class SplashActivity : BindingActivity<ActivitySplashBinding>(R.layout.activity_splash) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
isUpdateAvailable()

lifecycleScope.launch {
delay(2000)
if (SparkleStorage.accessToken != null) {
if (SparkleStorage.partnerId != -1) {
startActivity<HomeActivity>()
} else {
startActivity<NickNameActivity>()
}
} else {
if (SparkleStorage.partnerId != -1) {
startActivity<HomeActivity>()
} else {
startActivity<OnBoardingActivity>()
}
}
overridePendingTransition(0, 0)
finish()
}
}

private fun isUpdateAvailable() {
val appUpdateManager = AppUpdateManagerFactory.create(this)
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(
AppUpdateType.IMMEDIATE,
)
) {
SparkleStorage.setUpdateAvailableBoolean(IS_UPDATE_AVAILABLE, true)
Log.e("subin", "${SparkleStorage.getUpdateAvailableBoolean(IS_UPDATE_AVAILABLE)}")
} else {
SparkleStorage.setUpdateAvailableBoolean(IS_UPDATE_AVAILABLE, false)
Log.e("subin", "${SparkleStorage.getUpdateAvailableBoolean(IS_UPDATE_AVAILABLE)}")
}
}
}

companion object {
const val IS_UPDATE_AVAILABLE = "IS_UPDATE_AVAILABLE"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package sopt.uni.presentation.home

import android.app.Activity.RESULT_OK
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.appupdate.AppUpdateOptions
import com.google.android.play.core.install.model.AppUpdateType
import com.google.android.play.core.install.model.UpdateAvailability
import dagger.hilt.android.AndroidEntryPoint
import sopt.uni.R
import sopt.uni.data.datasource.local.SparkleStorage
import sopt.uni.databinding.TitleAction2DialogBinding
import sopt.uni.presentation.BindingDialogFragment
import sopt.uni.util.extension.setOnSingleClickListener

@AndroidEntryPoint
class UpdateDialogFragment :
BindingDialogFragment<TitleAction2DialogBinding>(R.layout.title_action2_dialog) {

private val startForResult =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
if (it.resultCode != RESULT_OK) {
Toast.makeText(requireContext(), "업데이트에 실패했습니다. 다시 시도해주세요.", Toast.LENGTH_SHORT)
.show()
} else {
SparkleStorage.setUpdateAvailableBoolean(IS_UPDATE_AVAILABLE, false)
dismiss()
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

with(binding) {
dialogTitle.setText(getString(R.string.update_dialog_title))
dialogBody.setText(getString(R.string.update_dialog_body))
btnRight.setText(getString(R.string.update_dialog_ok))
btnLeft.setOnSingleClickListener {
dismiss()
}
btnRight.setOnSingleClickListener {
requestUpdate()
}
}
}

private fun requestUpdate() {
val appUpdateManager = AppUpdateManagerFactory.create(requireContext())
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(
AppUpdateType.IMMEDIATE,
)
) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
startForResult,
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build(),
)
}
}
}

companion object {
const val IS_UPDATE_AVAILABLE = "IS_UPDATE_AVAILABLE"
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@
<string name="dialog_ok_text">확인</string>
<string name="dialog_title_text">Title</string>

<!-- updateDialog -->
<string name="update_dialog_title">새로운 버전이 업데이트 되었어요</string>
<string name="update_dialog_body">새로운 버전으로 업데이트 하시겠습니까?</string>
<string name="update_dialog_ok">업데이트</string>

<!-- createShortGame -->
<string name="create_short_game_title">한판승부</string>
<string name="create_short_game_select_mission">미션 카테고리 선택하기</string>
Expand Down

0 comments on commit f37a9c9

Please sign in to comment.