<lambda>null1 package com.android.healthconnect.controller.migration
2 
3 import android.content.Context
4 import android.content.SharedPreferences
5 import android.os.Bundle
6 import android.view.View
7 import androidx.fragment.app.viewModels
8 import com.android.healthconnect.controller.R
9 import com.android.healthconnect.controller.migration.api.MigrationRestoreState
10 import com.android.healthconnect.controller.migration.api.MigrationRestoreState.DataRestoreUiState
11 import com.android.healthconnect.controller.migration.api.MigrationRestoreState.MigrationUiState
12 import com.android.healthconnect.controller.shared.Constants.USER_ACTIVITY_TRACKER
13 import com.android.healthconnect.controller.shared.preference.HealthPreferenceFragment
14 import com.android.healthconnect.controller.utils.NavigationUtils
15 import dagger.hilt.android.AndroidEntryPoint
16 import javax.inject.Inject
17 
18 @AndroidEntryPoint(HealthPreferenceFragment::class)
19 class MigrationNavigationFragment : Hilt_MigrationNavigationFragment() {
20 
21     @Inject lateinit var navigationUtils: NavigationUtils
22 
23     private val migrationViewModel: MigrationViewModel by viewModels()
24     private lateinit var sharedPreference: SharedPreferences
25 
26     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
27         super.onCreatePreferences(savedInstanceState, rootKey)
28         setPreferencesFromResource(R.xml.empty_preference_screen, rootKey)
29     }
30 
31     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
32         super.onViewCreated(view, savedInstanceState)
33         sharedPreference =
34             requireActivity().getSharedPreferences(USER_ACTIVITY_TRACKER, Context.MODE_PRIVATE)
35 
36         migrationViewModel.migrationState.observe(viewLifecycleOwner) { migrationState ->
37             when (migrationState) {
38                 is MigrationViewModel.MigrationFragmentState.Loading -> {
39                     setLoading(true)
40                 }
41                 is MigrationViewModel.MigrationFragmentState.WithData -> {
42                     setLoading(false)
43                     updateFragment(migrationState.migrationRestoreState)
44                 }
45                 is MigrationViewModel.MigrationFragmentState.Error -> {
46                     setError(true)
47                 }
48             }
49         }
50     }
51 
52     private fun updateFragment(migrationRestoreState: MigrationRestoreState) {
53         val (migrationUiState, dataRestoreUiState, dataErrorState) = migrationRestoreState
54 
55         if (dataRestoreUiState == DataRestoreUiState.IN_PROGRESS) {
56             showDataRestoreInProgressFragment()
57         } else if (migrationUiState in
58             listOf(MigrationUiState.ALLOWED_NOT_STARTED, MigrationUiState.ALLOWED_PAUSED)) {
59             showMigrationPausedFragment()
60         } else if (migrationUiState == MigrationUiState.APP_UPGRADE_REQUIRED) {
61             showAppUpdateRequiredFragment()
62         } else if (migrationUiState == MigrationUiState.MODULE_UPGRADE_REQUIRED) {
63             showModuleUpdateRequiredFragment()
64         } else if (migrationUiState == MigrationUiState.IN_PROGRESS) {
65             showMigrationInProgressFragment()
66         } else {
67             navigateToHomeFragment()
68         }
69     }
70 
71     private fun showDataRestoreInProgressFragment() {
72         navigationUtils.navigate(
73             this, R.id.action_migrationNavigationFragment_to_dataRestoreInProgressFragment)
74     }
75 
76     private fun showMigrationInProgressFragment() {
77         navigationUtils.navigate(
78             this, R.id.action_migrationNavigationFragment_to_migrationInProgressFragment)
79     }
80 
81     private fun showAppUpdateRequiredFragment() {
82         navigationUtils.navigate(
83             this, R.id.action_migrationNavigationFragment_to_migrationAppUpdateNeededFragment)
84     }
85 
86     private fun showModuleUpdateRequiredFragment() {
87         navigationUtils.navigate(
88             this, R.id.action_migrationNavigationFragment_to_migrationModuleUpdateNeededFragment)
89     }
90 
91     private fun showMigrationPausedFragment() {
92         navigationUtils.navigate(
93             this, R.id.action_migrationNavigationFragment_to_migrationPausedFragment)
94     }
95 
96     private fun navigateToHomeFragment() {
97         navigationUtils.navigate(this, R.id.action_migrationNavigationFragment_to_homeFragment)
98     }
99 }
100