Skip to content

Commit ab92318

Browse files
committed
notify user when new update is available
1 parent f8c1f29 commit ab92318

File tree

14 files changed

+597
-115
lines changed

14 files changed

+597
-115
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.raival.compose.file.explorer.common.icons
2+
3+
import androidx.compose.ui.graphics.Color
4+
import androidx.compose.ui.graphics.SolidColor
5+
import androidx.compose.ui.graphics.vector.ImageVector
6+
import androidx.compose.ui.graphics.vector.path
7+
import androidx.compose.ui.unit.dp
8+
9+
val PrismIcons.Upgrade: ImageVector by lazy {
10+
ImageVector.Builder(
11+
name = "Upgrade",
12+
defaultWidth = 24.dp,
13+
defaultHeight = 24.dp,
14+
viewportWidth = 24f,
15+
viewportHeight = 24f
16+
).apply {
17+
path(fill = SolidColor(Color.White)) {
18+
moveTo(12f, 1f)
19+
curveTo(5.92f, 1f, 1f, 5.92f, 1f, 12f)
20+
reflectiveCurveToRelative(4.92f, 11f, 11f, 11f)
21+
reflectiveCurveToRelative(11f, -4.92f, 11f, -11f)
22+
reflectiveCurveTo(18.08f, 1f, 12f, 1f)
23+
close()
24+
moveTo(16.29f, 12.71f)
25+
lineTo(13f, 9.41f)
26+
verticalLineTo(18f)
27+
horizontalLineToRelative(-2f)
28+
verticalLineTo(9.41f)
29+
lineToRelative(-3.29f, 3.29f)
30+
lineToRelative(-1.41f, -1.41f)
31+
lineTo(12f, 5.59f)
32+
lineToRelative(5.71f, 5.71f)
33+
lineTo(16.29f, 12.71f)
34+
close()
35+
}
36+
}.build()
37+
}

app/src/main/java/com/raival/compose/file/explorer/screen/main/MainActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class MainActivity : BaseActivity() {
9494
}
9595

9696
LaunchedEffect(Unit) {
97+
mainActivityManager.checkForUpdate()
9798
if (hasIntent()) {
9899
handleIntent()
99100
} else {
@@ -110,6 +111,7 @@ class MainActivity : BaseActivity() {
110111

111112
AppInfoDialog(
112113
show = mainActivityState.showAppInfoDialog,
114+
hasNewUpdate = mainActivityState.hasNewUpdate,
113115
onDismiss = { mainActivityManager.toggleAppInfoDialog(false) }
114116
)
115117

@@ -130,6 +132,7 @@ class MainActivity : BaseActivity() {
130132
Toolbar(
131133
title = mainActivityState.title,
132134
subtitle = mainActivityState.subtitle,
135+
hasNewUpdate = mainActivityState.hasNewUpdate,
133136
onToggleAppInfoDialog = { mainActivityManager.toggleAppInfoDialog(it) }
134137
)
135138
TabLayout(

app/src/main/java/com/raival/compose/file/explorer/screen/main/MainActivityManager.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.raival.compose.file.explorer.screen.main
22

33
import android.content.Context
4+
import com.google.gson.Gson
5+
import com.google.gson.reflect.TypeToken
46
import com.raival.compose.file.explorer.App.Companion.globalClass
7+
import com.raival.compose.file.explorer.App.Companion.logger
8+
import com.raival.compose.file.explorer.R
59
import com.raival.compose.file.explorer.common.fromJson
610
import com.raival.compose.file.explorer.common.isNot
11+
import com.raival.compose.file.explorer.common.printFullStackTrace
12+
import com.raival.compose.file.explorer.common.showMsg
13+
import com.raival.compose.file.explorer.screen.main.model.GithubRelease
714
import com.raival.compose.file.explorer.screen.main.startup.StartupTabType
815
import com.raival.compose.file.explorer.screen.main.startup.StartupTabs
916
import com.raival.compose.file.explorer.screen.main.tab.Tab
@@ -19,6 +26,11 @@ import kotlinx.coroutines.flow.asStateFlow
1926
import kotlinx.coroutines.flow.update
2027
import kotlinx.coroutines.launch
2128
import java.io.File
29+
import java.io.InputStreamReader
30+
import java.net.ConnectException
31+
import java.net.HttpURLConnection
32+
import java.net.URL
33+
import java.net.UnknownHostException
2234
import kotlin.math.max
2335
import kotlin.math.min
2436

@@ -27,6 +39,8 @@ class MainActivityManager {
2739
private val _state = MutableStateFlow(MainActivityState())
2840
val state = _state.asStateFlow()
2941

42+
var newUpdate: GithubRelease? = null
43+
3044
/**\
3145
* Loads available storage devices (Internal Storage, SD cards, etc)
3246
*/
@@ -313,6 +327,62 @@ class MainActivityManager {
313327
}
314328
}
315329

330+
fun checkForUpdate() {
331+
fetchGithubReleases { releases ->
332+
val latestRelease = releases.firstOrNull()
333+
if (latestRelease != null && latestRelease.tagName != "v${
334+
globalClass.packageManager.getPackageInfo(
335+
globalClass.packageName,
336+
0
337+
).versionName
338+
}"
339+
) {
340+
newUpdate = latestRelease
341+
_state.update { it.copy(hasNewUpdate = true) }
342+
showMsg(globalClass.getString(R.string.new_update_available))
343+
}
344+
}
345+
}
346+
347+
fun fetchGithubReleases(
348+
onResult: (List<GithubRelease>) -> Unit
349+
) {
350+
CoroutineScope(Dispatchers.IO).launch {
351+
val url = "https://api.github.com/repos/Raival-e/Prism-File-Explorer/releases"
352+
var releases = emptyList<GithubRelease>()
353+
354+
try {
355+
val connection = URL(url).openConnection() as HttpURLConnection
356+
connection.apply {
357+
requestMethod = "GET"
358+
connectTimeout = 10000
359+
readTimeout = 5000
360+
setRequestProperty("Accept", "application/vnd.github.v3+json")
361+
setRequestProperty("User-Agent", "Prism-File-Explorer")
362+
}
363+
364+
if (connection.responseCode == HttpURLConnection.HTTP_OK) {
365+
connection.inputStream.use { inputStream ->
366+
releases = Gson().fromJson(
367+
InputStreamReader(
368+
inputStream
369+
),
370+
object : TypeToken<List<GithubRelease>>() {}.type
371+
)
372+
}
373+
}
374+
} catch (_: UnknownHostException) {
375+
logger.logInfo(globalClass.getString(R.string.check_for_updates_failed_no_internet_connection))
376+
} catch (_: ConnectException) {
377+
logger.logInfo(globalClass.getString(R.string.check_for_updates_failed_failed_to_connect_to_server))
378+
} catch (e: Exception) {
379+
logger.logError(e.printFullStackTrace())
380+
}
381+
382+
onResult(releases)
383+
}
384+
}
385+
316386
fun toggleJumpToPathDialog(show: Boolean) {
317387
_state.update {
318388
it.copy(

app/src/main/java/com/raival/compose/file/explorer/screen/main/MainActivityState.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ data class MainActivityState(
1818
val selectedTabIndex: Int = 0,
1919
val storageDevices: List<StorageDevice> = emptyList(),
2020
val tabs: List<Tab> = emptyList(),
21-
val tabLayoutState: LazyListState = LazyListState()
21+
val tabLayoutState: LazyListState = LazyListState(),
22+
val hasNewUpdate: Boolean = false
2223
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.raival.compose.file.explorer.screen.main.model
2+
3+
import com.google.gson.annotations.SerializedName
4+
import java.util.Date
5+
6+
data class GithubReleaseAsset(
7+
@SerializedName("name")
8+
val name: String,
9+
@SerializedName("browser_download_url")
10+
val browserDownloadUrl: String
11+
)
12+
13+
data class GithubRelease(
14+
@SerializedName("html_url")
15+
val htmlUrl: String,
16+
@SerializedName("tag_name")
17+
val tagName: String,
18+
@SerializedName("body")
19+
val body: String,
20+
@SerializedName("published_at")
21+
val publishedAt: Date,
22+
@SerializedName("assets")
23+
val assets: List<GithubReleaseAsset>
24+
)

0 commit comments

Comments
 (0)