@@ -61,7 +61,7 @@ class ReleaseManager private constructor(
61
61
sealed class ReleaseManagerState {
62
62
data object InUpdateCheck : ReleaseManagerState ()
63
63
data class Failure (val exception : Exception ) : ReleaseManagerState()
64
- data class InDownload (val downloaded : Long , val outOf : Long ) : ReleaseManagerState()
64
+ data class InDownload (val downloaded : Long , val outOf : Long? ) : ReleaseManagerState()
65
65
data class Finished (val hasUpdated : Boolean = false ) : ReleaseManagerState()
66
66
}
67
67
@@ -82,7 +82,7 @@ class ReleaseManager private constructor(
82
82
val isInUpdate: Boolean
83
83
get() = _uiState .value !is ReleaseManagerState .Failure && _uiState .value !is ReleaseManagerState .Finished
84
84
85
- private val _availableLauncherUpdate = MutableStateFlow <Release ?>(null )
85
+ private val _availableLauncherUpdate = MutableStateFlow <DownloadableLauncherRelease ?>(null )
86
86
val availableLauncherUpdate = _availableLauncherUpdate .asStateFlow()
87
87
88
88
private fun sendError (e : Exception ) {
@@ -106,24 +106,21 @@ class ReleaseManager private constructor(
106
106
}
107
107
}
108
108
109
- private fun getBestReleaseForGameVersion (): String? {
109
+ private fun getBestReleaseForGameVersion (gameVersion : Long ): String? = when {
110
+ gameVersion >= 40L -> mapSelectedReleaseToTag()
111
+ gameVersion == 39L -> " v3.9.2"
112
+ gameVersion == 38L -> " v2.0.0-beta.27"
113
+ gameVersion == 37L -> " v2.0.0-beta.4"
114
+ else -> null
115
+ }
116
+
117
+ private suspend fun getLatestRelease (): Downloadable ? {
110
118
if (! GamePackageUtils .isGameInstalled(applicationContext.packageManager)) {
111
119
return null
112
120
}
113
121
114
122
val gameVersion = GamePackageUtils .getGameVersionCode(applicationContext.packageManager)
115
-
116
- return when {
117
- gameVersion >= 40L -> mapSelectedReleaseToTag()
118
- gameVersion == 39L -> " v3.9.2"
119
- gameVersion == 38L -> " v2.0.0-beta.27"
120
- gameVersion == 37L -> " v2.0.0-beta.4"
121
- else -> null
122
- }
123
- }
124
-
125
- private suspend fun getLatestRelease (): Release ? {
126
- val targetTag = getBestReleaseForGameVersion() ? : return null
123
+ val targetTag = getBestReleaseForGameVersion(gameVersion) ? : return null
127
124
128
125
return when (targetTag) {
129
126
TAG_LATEST -> releaseRepository.getLatestGeodeRelease()
@@ -132,23 +129,23 @@ class ReleaseManager private constructor(
132
129
}
133
130
}
134
131
135
- private suspend fun downloadLauncherUpdate (release : Release ) {
136
- val download = release.getLauncherDownload () ? : return
132
+ private suspend fun downloadLauncherUpdate (release : Downloadable ) {
133
+ val download = release.getDownload () ? : return
137
134
138
135
val outputDirectory = LaunchUtils .getBaseDirectory(applicationContext)
139
- val outputFile = File (outputDirectory, download.name )
136
+ val outputFile = File (outputDirectory, download.filename )
140
137
141
138
if (outputFile.exists()) {
142
139
// only download the apk once
143
140
return
144
141
}
145
142
146
- _uiState .value = ReleaseManagerState .InDownload (0 , download.size.toLong() )
143
+ _uiState .value = ReleaseManagerState .InDownload (0 , download.size)
147
144
148
145
try {
149
146
val fileStream = DownloadUtils .downloadStream(
150
147
httpClient,
151
- download.browserDownloadUrl
148
+ download.url
152
149
) { progress, outOf ->
153
150
_uiState .value = ReleaseManagerState .InDownload (progress, outOf)
154
151
}
@@ -160,8 +157,8 @@ class ReleaseManager private constructor(
160
157
}
161
158
}
162
159
163
- private suspend fun performUpdate (release : Release ) {
164
- val releaseAsset = release.getGeodeDownload ()
160
+ private suspend fun performUpdate (release : Downloadable ) {
161
+ val releaseAsset = release.getDownload ()
165
162
if (releaseAsset == null ) {
166
163
val noAssetException = Exception (" missing Android download" )
167
164
_uiState .value = ReleaseManagerState .Failure (noAssetException)
@@ -170,12 +167,12 @@ class ReleaseManager private constructor(
170
167
}
171
168
172
169
// set an initial download size
173
- _uiState .value = ReleaseManagerState .InDownload (0 , releaseAsset.size.toLong() )
170
+ _uiState .value = ReleaseManagerState .InDownload (0 , releaseAsset.size)
174
171
175
172
try {
176
173
val fileStream = DownloadUtils .downloadStream(
177
174
httpClient,
178
- releaseAsset.browserDownloadUrl
175
+ releaseAsset.url
179
176
) { progress, outOf ->
180
177
_uiState .value = ReleaseManagerState .InDownload (progress, outOf)
181
178
}
@@ -223,8 +220,8 @@ class ReleaseManager private constructor(
223
220
return originalFileHash != currentFileHash
224
221
}
225
222
226
- private fun checkLauncherUpdate (launcherUpdate : Release ) {
227
- if (launcherUpdate.tagName != BuildConfig .VERSION_NAME ) {
223
+ private fun checkLauncherUpdate (launcherUpdate : DownloadableLauncherRelease ) {
224
+ if (launcherUpdate.release. tagName != BuildConfig .VERSION_NAME ) {
228
225
_availableLauncherUpdate .value = launcherUpdate
229
226
}
230
227
}
@@ -286,7 +283,7 @@ class ReleaseManager private constructor(
286
283
performUpdate(release)
287
284
}
288
285
289
- private fun updatePreferences (release : Release ) {
286
+ private fun updatePreferences (release : Downloadable ) {
290
287
val sharedPreferences = PreferenceUtils .get(applicationContext)
291
288
292
289
sharedPreferences.setString(
0 commit comments