@@ -51,6 +51,9 @@ class HomeViewModel(
5151 private val _homeScreenState = MutableStateFlow <HomeScreenState >(HomeScreenState .InitialLoading )
5252 val homeScreenState: StateFlow <HomeScreenState > = _homeScreenState .asStateFlow()
5353
54+ private val _isRefreshing = MutableStateFlow (false )
55+ val isRefreshing: StateFlow <Boolean > = _isRefreshing .asStateFlow()
56+
5457 private val deviceSupportedAbis: List <String > by lazy {
5558 deviceSupportedAbisForTesting ? : Build .SUPPORTED_ABIS ?.toList() ? : emptyList()
5659 }
@@ -108,66 +111,79 @@ class HomeViewModel(
108111 fun initialLoad () {
109112 viewModelScope.launch {
110113 _homeScreenState .value = HomeScreenState .InitialLoading
114+ _isRefreshing .value = true
111115 cacheManager.checkCacheStatus() // Initial check
116+ fetchData()
117+ _isRefreshing .value = false
118+ }
119+ }
112120
113- val appInfoMap = mapOf (
114- FENIX to mozillaPackageManager.fenix,
115- FOCUS to mozillaPackageManager.focus,
116- REFERENCE_BROWSER to mozillaPackageManager.referenceBrowser,
117- )
118-
119- _homeScreenState .update {
120- val currentCacheState = cacheManager.cacheState.value
121- val initialApps = appInfoMap.mapValues { (appName, appState) ->
122- AppUiModel (
123- name = appName,
124- packageName = appState.packageName,
125- installedVersion = appState.version,
126- installedDate = appState.formattedInstallDate,
127- apks = ApksResult .Loading ,
128- )
129- }
130- HomeScreenState .Loaded (
131- apps = initialApps,
132- cacheManagementState = currentCacheState,
133- isDownloadingAnyFile = false ,
134- )
135- }
121+ fun refreshData () {
122+ viewModelScope.launch {
123+ _isRefreshing .value = true
124+ fetchData()
125+ _isRefreshing .value = false
126+ }
127+ }
136128
137- val results = mapOf (
138- FENIX to mozillaArchiveRepository.getFenixNightlyBuilds(),
139- FOCUS to mozillaArchiveRepository.getFocusNightlyBuilds(),
140- REFERENCE_BROWSER to mozillaArchiveRepository.getReferenceBrowserNightlyBuilds(),
141- )
129+ private suspend fun fetchData () {
130+ val appInfoMap = mapOf (
131+ FENIX to mozillaPackageManager.fenix,
132+ FOCUS to mozillaPackageManager.focus,
133+ REFERENCE_BROWSER to mozillaPackageManager.referenceBrowser,
134+ )
142135
143- val newApps = results.mapValues { (appName, result) ->
144- val appState = appInfoMap[appName]
145- val apksResult = when (result) {
146- is NetworkResult .Success -> {
147- val latestApks = getLatestApks(result.data)
148- ApksResult .Success (convertParsedApksToUiModels(latestApks))
149- }
150- is NetworkResult .Error -> ApksResult .Error (" Error fetching $appName nightly builds: ${result.message} " )
151- }
136+ _homeScreenState .update {
137+ val currentCacheState = cacheManager.cacheState.value
138+ val initialApps = appInfoMap.mapValues { (appName, appState) ->
152139 AppUiModel (
153140 name = appName,
154- packageName = appState? .packageName ? : " " ,
155- installedVersion = appState? .version,
156- installedDate = appState? .formattedInstallDate,
157- apks = apksResult ,
141+ packageName = appState.packageName,
142+ installedVersion = appState.version,
143+ installedDate = appState.formattedInstallDate,
144+ apks = ApksResult . Loading ,
158145 )
159146 }
147+ HomeScreenState .Loaded (
148+ apps = initialApps,
149+ cacheManagementState = currentCacheState,
150+ isDownloadingAnyFile = false ,
151+ )
152+ }
160153
161- val isDownloading = newApps.values.any { app ->
162- (app.apks as ? ApksResult .Success )?.apks?.any { it.downloadState is DownloadState .InProgress } == true
163- }
154+ val results = mapOf (
155+ FENIX to mozillaArchiveRepository.getFenixNightlyBuilds(),
156+ FOCUS to mozillaArchiveRepository.getFocusNightlyBuilds(),
157+ REFERENCE_BROWSER to mozillaArchiveRepository.getReferenceBrowserNightlyBuilds(),
158+ )
164159
165- _homeScreenState .update {
166- if (it is HomeScreenState .Loaded ) {
167- it.copy(apps = newApps, isDownloadingAnyFile = isDownloading)
168- } else {
169- it
160+ val newApps = results.mapValues { (appName, result) ->
161+ val appState = appInfoMap[appName]
162+ val apksResult = when (result) {
163+ is NetworkResult .Success -> {
164+ val latestApks = getLatestApks(result.data)
165+ ApksResult .Success (convertParsedApksToUiModels(latestApks))
170166 }
167+ is NetworkResult .Error -> ApksResult .Error (" Error fetching $appName nightly builds: ${result.message} " )
168+ }
169+ AppUiModel (
170+ name = appName,
171+ packageName = appState?.packageName ? : " " ,
172+ installedVersion = appState?.version,
173+ installedDate = appState?.formattedInstallDate,
174+ apks = apksResult,
175+ )
176+ }
177+
178+ val isDownloading = newApps.values.any { app ->
179+ (app.apks as ? ApksResult .Success )?.apks?.any { it.downloadState is DownloadState .InProgress } == true
180+ }
181+
182+ _homeScreenState .update {
183+ if (it is HomeScreenState .Loaded ) {
184+ it.copy(apps = newApps, isDownloadingAnyFile = isDownloading)
185+ } else {
186+ it
171187 }
172188 }
173189 }
0 commit comments