Skip to content

Commit

Permalink
Merge pull request #271 from mash-up-kr/feature/minuk/264-update-reco…
Browse files Browse the repository at this point in the history
…mmend

[Feature] 앱 업데이트 유도 기능 추가
  • Loading branch information
jaeryo2357 authored Apr 8, 2023
2 parents 90eda51 + bed91be commit 9f85053
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 35 deletions.
7 changes: 1 addition & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ dependencies {
implementation project(":core:testing")
implementation project(":core:common")
implementation project(":core:datastore")
implementation project(":core:firebase")
implementation project(":feature:setting")

// ml Kit
Expand All @@ -106,12 +107,6 @@ dependencies {
implementation "androidx.camera:camera-lifecycle:$cameraVersion"
implementation "androidx.camera:camera-view:$cameraVersion"

// google firebase
implementation platform("com.google.firebase:firebase-bom:$firebaseVersion")
implementation "com.google.firebase:firebase-analytics-ktx"
implementation "com.google.firebase:firebase-crashlytics-ktx"
implementation "com.google.firebase:firebase-messaging-ktx"

// hilt
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-compiler:$hiltVersion"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

<queries>
<package android:name="com.android.vending" />
<intent>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="https" android:host="play.google.com" />
</intent>
</queries>

<application
android:name=".MashUpApplication"
android:icon="@mipmap/ic_launcher"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/mashup/base/BaseViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ abstract class BaseViewModel : ViewModel() {
}
}

abstract fun handleErrorCode(code: String)
open fun handleErrorCode(code: String) {}
}
18 changes: 0 additions & 18 deletions app/src/main/java/com/mashup/data/repository/FirebaseRepository.kt

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/java/com/mashup/ui/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.mashup.ui.login

import com.mashup.base.BaseViewModel
import com.mashup.core.common.model.Validation
import com.mashup.core.firebase.FirebaseRepository
import com.mashup.core.model.Platform
import com.mashup.data.repository.FirebaseRepository
import com.mashup.data.repository.MemberRepository
import com.mashup.datastore.data.repository.UserPreferenceRepository
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/mashup/ui/signup/SignUpViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.mashup.ui.signup

import com.mashup.base.BaseViewModel
import com.mashup.core.common.model.Validation
import com.mashup.core.firebase.FirebaseRepository
import com.mashup.core.model.Platform
import com.mashup.data.repository.FirebaseRepository
import com.mashup.data.repository.MemberRepository
import com.mashup.datastore.data.repository.UserPreferenceRepository
import com.mashup.ui.signup.state.CodeState
Expand Down
62 changes: 57 additions & 5 deletions app/src/main/java/com/mashup/ui/splash/SplashActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.mashup.ui.splash

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.mashup.R
import com.mashup.base.BaseActivity
import com.mashup.core.common.extensions.setStatusBarColorRes
import com.mashup.core.common.extensions.setStatusBarDarkTextColor
import com.mashup.core.common.widget.CommonDialog
import com.mashup.databinding.ActivitySplashBinding
import com.mashup.datastore.data.repository.UserPreferenceRepository
import com.mashup.ui.login.LoginActivity
import com.mashup.util.AnalyticsManager
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -27,14 +33,13 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>() {
@Inject
lateinit var userPreferenceRepository: UserPreferenceRepository

private val splashViewModel: SplashViewModel by viewModels()

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

lifecycleScope.launchWhenResumed {
delay(2000)
startActivity(LoginActivity.newIntent(this@SplashActivity))
}
setUi()
observeViewModel()
initAnalyticsManager()
}

Expand All @@ -43,6 +48,53 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>() {
setStatusBarDarkTextColor(false)
}

private fun observeViewModel() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
splashViewModel.onLowerAppVersion.collectLatest {
CommonDialog(this@SplashActivity).apply {
setTitle("업데이트")
setMessage("정상적인 이용을 위해\n업데이트가 필요합니다.")
setNegativeButton(text = "확인") {
moveNextScreen()
}
setPositiveButton(text = "업데이트 하기") {
moveGooglePayForUpdate()
}
show()
}
}
}
launch {
splashViewModel.onFinishInit.collectLatest {
moveNextScreen()
}
}
}
}
splashViewModel.checkAppVersion(this)
}

private fun moveNextScreen() {
startActivity(LoginActivity.newIntent(this@SplashActivity))
finish()
}

private fun moveGooglePayForUpdate() {
try {
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(
"https://play.google.com/store/apps/details?id=com.mashup"
)
setPackage("com.android.vending")
}
startActivity(intent)
finish()
} catch (ignore: Exception) {
}
}

private fun initAnalyticsManager() {
AnalyticsManager.init(this)
lifecycleScope.launch {
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/com/mashup/ui/splash/SplashViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mashup.ui.splash

import android.content.Context
import androidx.core.content.pm.PackageInfoCompat
import com.mashup.base.BaseViewModel
import com.mashup.core.firebase.FirebaseRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import javax.inject.Inject


@HiltViewModel
class SplashViewModel @Inject constructor(
private val firebaseRepository: FirebaseRepository
) : BaseViewModel() {

private val _onLowerAppVersion = MutableSharedFlow<Unit>()
val onLowerAppVersion: SharedFlow<Unit> = _onLowerAppVersion

private val _onFinishInit = MutableSharedFlow<Unit>()
val onFinishInit: SharedFlow<Unit> = _onFinishInit

fun checkAppVersion(context: Context) = mashUpScope {
try {
val localVersionCode = PackageInfoCompat.getLongVersionCode(
context.packageManager.getPackageInfo(context.packageName, 0)
)
val resentAppVersion = firebaseRepository.getRecentAppVersion()
if (localVersionCode < resentAppVersion) {
_onLowerAppVersion.emit(Unit)
} else {
delay(2000L)
_onFinishInit.emit(Unit)
}
} catch (cancelException: CancellationException) {
throw cancelException
} catch (ignore: Exception) {
}
}
}
4 changes: 1 addition & 3 deletions core/common/src/main/res/layout/dialog_common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
app:layout_constraintEnd_toStartOf="@id/btn_positive"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_message"
app:text_button='@{"취소"}'
app:style_button="@{ButtonStyle.DEFAULT}" />

<com.mashup.core.common.widget.ButtonView
Expand All @@ -70,8 +69,7 @@
app:layout_constraintStart_toEndOf="@id/btn_negative"
app:layout_constraintTop_toBottomOf="@id/tv_message"
app:layout_goneMarginStart="20dp"
app:style_button="@{ButtonStyle.PRIMARY}"
app:text_button='@{"확인"}' />
app:style_button="@{ButtonStyle.PRIMARY}" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1 change: 1 addition & 0 deletions core/firebase/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
37 changes: 37 additions & 0 deletions core/firebase/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
namespace 'com.mashup.core.firebase'
compileSdk compileVersion

defaultConfig {
minSdk minVersion
targetSdk targetVersion
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
// hilt
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-compiler:$hiltVersion"

// google firebase
api platform("com.google.firebase:firebase-bom:$firebaseVersion")
api "com.google.firebase:firebase-analytics-ktx"
api "com.google.firebase:firebase-crashlytics-ktx"
api "com.google.firebase:firebase-messaging-ktx"
api 'com.google.firebase:firebase-firestore-ktx'
}
4 changes: 4 additions & 0 deletions core/firebase/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mashup.core.firebase

import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import com.google.firebase.messaging.FirebaseMessaging
import kotlinx.coroutines.suspendCancellableCoroutine
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class FirebaseRepository @Inject constructor() {
suspend fun getFcmToken(): String = suspendCancellableCoroutine { continuation ->
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
if (task.isSuccessful) {
continuation.resume(task.result) {}
} else {
continuation.cancel()
}
}
}

suspend fun getRecentAppVersion(): Long = suspendCancellableCoroutine { continuation ->
Firebase.firestore.document("app/config").get()
.addOnSuccessListener { document ->
val version = document?.getLong("version")
if (version != null) {
continuation.resume(version) {}
} else {
continuation.cancel()
}
}
.addOnFailureListener { _ ->
continuation.cancel()
}
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ include ':core:common'
include ':core:testing'
include ':core:model'
include ':core:datastore'
include ':core:firebase'
include ':feature:setting'

0 comments on commit 9f85053

Please sign in to comment.