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"
minSdk = 24
targetSdk = 33
versionCode = 1020
versionName = "1.0.2"
versionCode = 1030
versionName = "1.0.3"
buildConfigField("String", "GIT_BRANCH", "\"${getCurrentBranch()}\"")
buildConfigField("String", "GIT_COMMIT", "\"${getLatestCommit()}\"")

View file

@ -47,6 +47,15 @@
</intent-filter>
</activity>
<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>
</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.os.IBinder
import dev.beefers.vendetta.manager.R
import dev.beefers.vendetta.manager.domain.manager.InstallManager
import dev.beefers.vendetta.manager.utils.showToast
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class InstallService : Service(), KoinComponent {
private val installManager: InstallManager by inject()
class InstallService : Service() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val isInstall = intent.action == "vendetta.actions.ACTION_INSTALL"
@ -25,10 +20,7 @@ class InstallService : Service(), KoinComponent {
startActivity(confirmationIntent)
}
PackageInstaller.STATUS_SUCCESS -> {
if (isInstall) showToast(R.string.installer_success)
installManager.getInstalled()
}
PackageInstaller.STATUS_SUCCESS -> if (isInstall) showToast(R.string.installer_success)
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 = {},
onError: () -> Unit = {}
) {
when(this) {
when (this) {
is ApiResponse.Success -> onSuccess(data)
is ApiResponse.Error,
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.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
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.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import androidx.paging.compose.itemsIndexed
import cafe.adriel.voyager.koin.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
@ -103,7 +101,7 @@ class HomeScreen : ManagerTab {
) {
AnimatedVisibility(visible = viewModel.discordVersions != null) {
Text(
text = "Latest: ${viewModel.discordVersions!![prefs.channel]!!}",
text = "Latest: ${viewModel.discordVersions?.get(prefs.channel)}",
style = MaterialTheme.typography.labelLarge,
color = LocalContentColor.current.copy(alpha = 0.5f),
textAlign = TextAlign.Center
@ -112,7 +110,7 @@ class HomeScreen : ManagerTab {
AnimatedVisibility(visible = viewModel.installManager.current != null) {
Text(
text = "Current: ${viewModel.installManager.current!!.versionName}",
text = "Current: ${viewModel.installManager.current?.versionName}",
style = MaterialTheme.typography.labelLarge,
color = LocalContentColor.current.copy(alpha = 0.5f),
textAlign = TextAlign.Center
@ -167,8 +165,10 @@ class HomeScreen : ManagerTab {
.fillMaxSize()
) {
val commits = viewModel.commits.collectAsLazyPagingItems()
val loading = 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
val loading =
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 {
itemsIndexed(
@ -178,7 +178,7 @@ class HomeScreen : ManagerTab {
if (commit != null) {
Column {
Commit(commit = commit)
if(i < commits.itemSnapshotList.lastIndex) {
if (i < commits.itemSnapshotList.lastIndex) {
Divider(
thickness = 0.5.dp,
color = MaterialTheme.colorScheme.outline.copy(alpha = 0.3f),
@ -190,7 +190,7 @@ class HomeScreen : ManagerTab {
}
}
if(loading) {
if (loading) {
item {
Box(
contentAlignment = Alignment.TopCenter,
@ -206,7 +206,7 @@ class HomeScreen : ManagerTab {
}
}
if(failed) {
if (failed) {
item {
Column(
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.utils.ApiResponse
import dev.beefers.vendetta.manager.network.utils.dataOrNull
import dev.beefers.vendetta.manager.network.utils.fold
import dev.beefers.vendetta.manager.utils.DiscordVersion
import kotlinx.coroutines.launch
@ -44,12 +43,13 @@ class HomeViewModel(
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Commit> {
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(
data = response.data,
prevKey = if (page > 0) page - 1 else null,
nextKey = if (response.data.isNotEmpty()) page + 1 else null
)
is ApiResponse.Failure -> LoadResult.Error(response.error)
is ApiResponse.Error -> LoadResult.Error(response.error)
}