Skip to content

Commit

Permalink
feat: ViewModelの値に基づいてInAppReviewを表示するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Nov 22, 2023
1 parent 6c65008 commit c31ded3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.google.firebase.crashlytics.FirebaseCrashlytics
import jp.panta.misskeyandroidclient.MainActivity
import jp.panta.misskeyandroidclient.R
import jp.panta.misskeyandroidclient.databinding.ActivityMainBinding
import jp.panta.misskeyandroidclient.review.InAppReviewWrapper
import jp.panta.misskeyandroidclient.ui.main.viewmodel.MainViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -54,6 +55,7 @@ internal class MainActivityEventHandler(
private val configStore: SettingStore,
private val draftNoteService: DraftNoteService,
private val currentPageableTimelineViewModel: CurrentPageableTimelineViewModel,
private val inAppReviewWrapper: InAppReviewWrapper,
) {

class Factory @Inject constructor(
Expand All @@ -62,6 +64,7 @@ internal class MainActivityEventHandler(
private val configStore: SettingStore,
private val draftNoteService: DraftNoteService,
private val featureEnables: FeatureEnables,
private val inAppReviewWrapper: InAppReviewWrapper,
) {
fun create(
activity: MainActivity,
Expand All @@ -84,7 +87,8 @@ internal class MainActivityEventHandler(
ChangeNavMenuVisibilityFromAPIVersion(binding.navView, featureEnables),
configStore,
draftNoteService,
currentPageableTimelineViewModel
currentPageableTimelineViewModel,
inAppReviewWrapper
)
}
}
Expand Down Expand Up @@ -113,6 +117,7 @@ internal class MainActivityEventHandler(
collectDraftNoteSavedEvent()
collectCurrentPageableState()
collectEnableSafeSearchDescriptionState()
collectShowInAppReviewState()
}

private fun collectCrashlyticsCollectionState() {
Expand Down Expand Up @@ -352,4 +357,16 @@ internal class MainActivityEventHandler(

}.flowWithLifecycle(activity.lifecycle, Lifecycle.State.RESUMED).launchIn(lifecycleScope)
}

private fun collectShowInAppReviewState() {
lifecycleScope.launch {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
mainViewModel.isShowInAppReview.distinctUntilChanged().collect {
if (it) {
inAppReviewWrapper.showReview(activity)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import net.pantasystem.milktea.model.messaging.UnReadMessages
import net.pantasystem.milktea.model.notification.NotificationRepository
import net.pantasystem.milktea.model.notification.NotificationStreaming
import net.pantasystem.milktea.model.setting.LocalConfigRepository
import net.pantasystem.milktea.model.statistics.InAppPostCounterRepository
import javax.inject.Inject

@HiltViewModel
Expand All @@ -29,6 +30,7 @@ class MainViewModel @Inject constructor(
private val configRepository: LocalConfigRepository,
private val emojiEventHandler: EmojiEventHandler,
private val notificationStreaming: NotificationStreaming,
inAppPostCounterRepository: InAppPostCounterRepository,
settingStore: SettingStore
) : ViewModel() {
val logger by lazy {
Expand Down Expand Up @@ -80,6 +82,9 @@ class MainViewModel @Inject constructor(
!config.isConfirmedPostNotification
}.distinctUntilChanged().shareIn(viewModelScope, SharingStarted.WhileSubscribed())

val isShowInAppReview = combine(inAppPostCounterRepository.observe(), accountStore.observeAccounts) { count, accounts ->
accounts.size >= 2 && count >= 10
}.distinctUntilChanged().shareIn(viewModelScope, SharingStarted.WhileSubscribed(5_000))


val state: StateFlow<MainUiState> = combine(
Expand Down

0 comments on commit c31ded3

Please sign in to comment.