Skip to content

Commit

Permalink
#121 / 업데이트 로직 보수
Browse files Browse the repository at this point in the history
  • Loading branch information
taeheeL authored and sub101 committed Feb 18, 2024
1 parent 4b3f561 commit e85ec61
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 87 deletions.
21 changes: 21 additions & 0 deletions app/src/main/java/sopt/uni/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sopt.uni.di

import android.content.Context
import com.google.android.play.core.appupdate.AppUpdateManager
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

@Provides
@Singleton
fun provideAppUpdateManager(@ApplicationContext context: Context): AppUpdateManager =
AppUpdateManagerFactory.create(context)
}
28 changes: 28 additions & 0 deletions app/src/main/java/sopt/uni/presentation/IntroActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.google.android.play.core.appupdate.AppUpdateManager
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.data.datasource.local.SparkleStorage
import sopt.uni.data.repository.shortgame.ShortGameRepository
import sopt.uni.presentation.home.HomeActivity
import sopt.uni.presentation.home.UpdateDialogFragment
import sopt.uni.presentation.invite.NickNameActivity
import sopt.uni.presentation.invite.ShareInviteCodeActivity
import sopt.uni.presentation.onboarding.OnBoardingActivity
Expand All @@ -19,16 +23,40 @@ import javax.inject.Inject
class IntroActivity : AppCompatActivity() {
@Inject
lateinit var shortGameRepository: ShortGameRepository

@Inject
lateinit var appUpdateManager: AppUpdateManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()

checkUpdateAvailability()
checkUserStatus()

// 메모장 초기화
shortGameRepository.setMemoText("")
}

private fun checkUpdateAvailability() {
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(
AppUpdateType.IMMEDIATE,
)
) {
showUpdateDialog()
}
}
}

private fun showUpdateDialog() {
UpdateDialogFragment().show(
supportFragmentManager,
"UpdateDialog",
)
}

private fun checkUserStatus() {
if (SparkleStorage.accessToken != null) {
Log.e("accessToken", SparkleStorage.accessToken.toString())
Expand Down
61 changes: 0 additions & 61 deletions app/src/main/java/sopt/uni/presentation/SplashActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@ package sopt.uni.presentation.home

import android.app.Activity
import android.os.Bundle
import android.util.Log
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.AppUpdateManager
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 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
import javax.inject.Inject

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

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

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

Expand All @@ -39,27 +33,32 @@ class UpdateDialogFragment :
dismiss()
}
btnRight.setOnSingleClickListener {
requestUpdate()
updateSparkle()
}
}
}

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(),
private fun updateSparkle() {
appUpdateManager.startUpdateFlowForResult(
appUpdateManager.appUpdateInfo.result,
startForResult,
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build(),
)
}

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

companion object {
const val IS_UPDATE_AVAILABLE = "IS_UPDATE_AVAILABLE"
Expand Down

0 comments on commit e85ec61

Please sign in to comment.