Apple is Apple

일일 회고

 

최종 발표 D - 1 !! 마지막까지 버그 수정하고 배포하느라 정말 바쁘다!! 마지막까지 힘내자!


오늘의 키워드

  • 인앱 업데이트

인앱 업데이트 표시

구글 플레이 디펜던시에서 인앱 업데이트를 확인 할 수 있도록 지원해준다. AppUpdateManager 활용

    // update check
    implementation 'com.google.android.play:app-update-ktx:2.1.0'

 

    // 앱 시작 시 앱 버전을 체크함
    private fun checkNewVersion() {
        val appUpdateManager = AppUpdateManagerFactory.create(requireActivity())
        homeViewModel.checkNewVersion(appUpdateManager)
    }
    
    
     fun checkNewVersion(appUpdateManager: AppUpdateManager) {
        runCatching {
            viewModelScope.launch(Dispatchers.IO) {
                val info = appUpdateManager.appUpdateInfo
                info.addOnSuccessListener { updateInfo ->
                    if (updateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
                        updateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
                    ) {
                        // 앱 업데이트가 있음
                        _isUpdateAvailable.postValue(true)
                        return@addOnSuccessListener
                    }
                    // 앱 업데이트가 없음
                    _isUpdateAvailable.postValue(false)
                }.addOnCanceledListener {
                    _isUpdateAvailable.postValue(false)
                }
            }
        }.onFailure {
            _isUpdateAvailable.postValue(false)
        }
    }
versionCode.observe(viewLifecycleOwner) { isUpdateAvailable ->
    // 같으면 아무동작안하고 리턴
    // 업데이트가 있다면 다이얼로그 실행해서 플레이스토어로 갈 수 있도록 유도
    if (isUpdateAvailable) {
        runUpdateDialog()
    }
}

    private fun runUpdateDialog() {
        AlertDialog.Builder(requireActivity())
            .setTitle(getString(R.string.new_update))
            .setMessage(getString(R.string.new_update_available))
            .setPositiveButton(getString(R.string.yes)) { _, _ ->
            	// 예 버튼을 누르면 플레이스토어로 이동
                startActivity(
                    Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(getString(R.string.playstore_url))
                    )
                )
            }.setNegativeButton(getString(R.string.no)) { _, _ -> }
            .create()
            .show()
    }
profile

Apple is Apple

@mjjjjjj