Feat: Listen for package installs and bump version

This commit is contained in:
wingio 2023-03-20 14:56:04 -04:00
parent 3bfeb662f0
commit 4520f2a690
7 changed files with 45 additions and 24 deletions

View file

@ -14,8 +14,8 @@ android {
applicationId = "dev.beefers.vendetta.manager" applicationId = "dev.beefers.vendetta.manager"
minSdk = 24 minSdk = 24
targetSdk = 33 targetSdk = 33
versionCode = 1020 versionCode = 1030
versionName = "1.0.2" versionName = "1.0.3"
buildConfigField("String", "GIT_BRANCH", "\"${getCurrentBranch()}\"") buildConfigField("String", "GIT_BRANCH", "\"${getCurrentBranch()}\"")
buildConfigField("String", "GIT_COMMIT", "\"${getLatestCommit()}\"") buildConfigField("String", "GIT_COMMIT", "\"${getLatestCommit()}\"")

View file

@ -47,6 +47,15 @@
</intent-filter> </intent-filter>
</activity> </activity>
<service android:name=".installer.service.InstallService" /> <service android:name=".installer.service.InstallService" />
<receiver android:name=".domain.receiver.InstallReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application> </application>
</manifest> </manifest>

View file

@ -0,0 +1,20 @@
package dev.beefers.vendetta.manager.domain.receiver
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import dev.beefers.vendetta.manager.domain.manager.InstallManager
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class InstallReceiver : BroadcastReceiver(), KoinComponent {
private val installManager: InstallManager by inject()
@SuppressLint("UnsafeProtectedBroadcastReceiver")
override fun onReceive(context: Context?, intent: Intent?) {
installManager.getInstalled()
}
}

View file

@ -5,14 +5,9 @@ import android.content.Intent
import android.content.pm.PackageInstaller import android.content.pm.PackageInstaller
import android.os.IBinder import android.os.IBinder
import dev.beefers.vendetta.manager.R import dev.beefers.vendetta.manager.R
import dev.beefers.vendetta.manager.domain.manager.InstallManager
import dev.beefers.vendetta.manager.utils.showToast import dev.beefers.vendetta.manager.utils.showToast
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class InstallService : Service(), KoinComponent { class InstallService : Service() {
private val installManager: InstallManager by inject()
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val isInstall = intent.action == "vendetta.actions.ACTION_INSTALL" val isInstall = intent.action == "vendetta.actions.ACTION_INSTALL"
@ -25,10 +20,7 @@ class InstallService : Service(), KoinComponent {
startActivity(confirmationIntent) startActivity(confirmationIntent)
} }
PackageInstaller.STATUS_SUCCESS -> { PackageInstaller.STATUS_SUCCESS -> if (isInstall) showToast(R.string.installer_success)
if (isInstall) showToast(R.string.installer_success)
installManager.getInstalled()
}
PackageInstaller.STATUS_FAILURE_ABORTED -> if (isInstall) showToast(R.string.installer_aborted) PackageInstaller.STATUS_FAILURE_ABORTED -> if (isInstall) showToast(R.string.installer_aborted)

View file

@ -29,7 +29,7 @@ fun <D> ApiResponse<D>.fold(
onSuccess: (D) -> Unit = {}, onSuccess: (D) -> Unit = {},
onError: () -> Unit = {} onError: () -> Unit = {}
) { ) {
when(this) { when (this) {
is ApiResponse.Success -> onSuccess(data) is ApiResponse.Success -> onSuccess(data)
is ApiResponse.Error, is ApiResponse.Error,
is ApiResponse.Failure -> onError() is ApiResponse.Failure -> onError()

View file

@ -12,7 +12,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
@ -39,7 +38,6 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.paging.LoadState import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import androidx.paging.compose.itemsIndexed import androidx.paging.compose.itemsIndexed
import cafe.adriel.voyager.koin.getScreenModel import cafe.adriel.voyager.koin.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.LocalNavigator
@ -103,7 +101,7 @@ class HomeScreen : ManagerTab {
) { ) {
AnimatedVisibility(visible = viewModel.discordVersions != null) { AnimatedVisibility(visible = viewModel.discordVersions != null) {
Text( Text(
text = "Latest: ${viewModel.discordVersions!![prefs.channel]!!}", text = "Latest: ${viewModel.discordVersions?.get(prefs.channel)}",
style = MaterialTheme.typography.labelLarge, style = MaterialTheme.typography.labelLarge,
color = LocalContentColor.current.copy(alpha = 0.5f), color = LocalContentColor.current.copy(alpha = 0.5f),
textAlign = TextAlign.Center textAlign = TextAlign.Center
@ -112,7 +110,7 @@ class HomeScreen : ManagerTab {
AnimatedVisibility(visible = viewModel.installManager.current != null) { AnimatedVisibility(visible = viewModel.installManager.current != null) {
Text( Text(
text = "Current: ${viewModel.installManager.current!!.versionName}", text = "Current: ${viewModel.installManager.current?.versionName}",
style = MaterialTheme.typography.labelLarge, style = MaterialTheme.typography.labelLarge,
color = LocalContentColor.current.copy(alpha = 0.5f), color = LocalContentColor.current.copy(alpha = 0.5f),
textAlign = TextAlign.Center textAlign = TextAlign.Center
@ -167,8 +165,10 @@ class HomeScreen : ManagerTab {
.fillMaxSize() .fillMaxSize()
) { ) {
val commits = viewModel.commits.collectAsLazyPagingItems() val commits = viewModel.commits.collectAsLazyPagingItems()
val loading = commits.loadState.append is LoadState.Loading || commits.loadState.refresh is LoadState.Loading val loading =
val failed = commits.loadState.append is LoadState.Error || commits.loadState.refresh is LoadState.Error commits.loadState.append is LoadState.Loading || commits.loadState.refresh is LoadState.Loading
val failed =
commits.loadState.append is LoadState.Error || commits.loadState.refresh is LoadState.Error
LazyColumn { LazyColumn {
itemsIndexed( itemsIndexed(
@ -178,7 +178,7 @@ class HomeScreen : ManagerTab {
if (commit != null) { if (commit != null) {
Column { Column {
Commit(commit = commit) Commit(commit = commit)
if(i < commits.itemSnapshotList.lastIndex) { if (i < commits.itemSnapshotList.lastIndex) {
Divider( Divider(
thickness = 0.5.dp, thickness = 0.5.dp,
color = MaterialTheme.colorScheme.outline.copy(alpha = 0.3f), color = MaterialTheme.colorScheme.outline.copy(alpha = 0.3f),
@ -190,7 +190,7 @@ class HomeScreen : ManagerTab {
} }
} }
if(loading) { if (loading) {
item { item {
Box( Box(
contentAlignment = Alignment.TopCenter, contentAlignment = Alignment.TopCenter,
@ -206,7 +206,7 @@ class HomeScreen : ManagerTab {
} }
} }
if(failed) { if (failed) {
item { item {
Column( Column(
verticalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(12.dp),

View file

@ -20,7 +20,6 @@ import dev.beefers.vendetta.manager.domain.repository.RestRepository
import dev.beefers.vendetta.manager.network.dto.Commit import dev.beefers.vendetta.manager.network.dto.Commit
import dev.beefers.vendetta.manager.network.utils.ApiResponse import dev.beefers.vendetta.manager.network.utils.ApiResponse
import dev.beefers.vendetta.manager.network.utils.dataOrNull import dev.beefers.vendetta.manager.network.utils.dataOrNull
import dev.beefers.vendetta.manager.network.utils.fold
import dev.beefers.vendetta.manager.utils.DiscordVersion import dev.beefers.vendetta.manager.utils.DiscordVersion
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -44,12 +43,13 @@ class HomeViewModel(
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Commit> { override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Commit> {
val page = params.key ?: 0 val page = params.key ?: 0
return when(val response = repo.getCommits("Vendetta", page)) { return when (val response = repo.getCommits("Vendetta", page)) {
is ApiResponse.Success -> LoadResult.Page( is ApiResponse.Success -> LoadResult.Page(
data = response.data, data = response.data,
prevKey = if (page > 0) page - 1 else null, prevKey = if (page > 0) page - 1 else null,
nextKey = if (response.data.isNotEmpty()) page + 1 else null nextKey = if (response.data.isNotEmpty()) page + 1 else null
) )
is ApiResponse.Failure -> LoadResult.Error(response.error) is ApiResponse.Failure -> LoadResult.Error(response.error)
is ApiResponse.Error -> LoadResult.Error(response.error) is ApiResponse.Error -> LoadResult.Error(response.error)
} }