Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #312

Merged
merged 6 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

defaultConfig {
applicationId = "com.joeloewi.croissant"
versionCode = 42
versionCode = 43
versionName = "1.2.1"
targetSdk = 34

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.joeloewi.croissant.receiver

import android.app.Application
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
Expand Down Expand Up @@ -37,28 +36,23 @@ import javax.inject.Inject

@AndroidEntryPoint
class ResinStatusWidgetProvider : AppWidgetProvider() {
private val _processLifecycleOwner by lazy { ProcessLifecycleOwner.get() }
private val _coroutineContext = Dispatchers.IO + CoroutineExceptionHandler { _, throwable ->
Firebase.crashlytics.apply {
log(this@ResinStatusWidgetProvider.javaClass.simpleName)
recordException(throwable)
}
}
private val _processLifecycleScope by lazy { ProcessLifecycleOwner.get().lifecycleScope }

@Inject
lateinit var powerManager: PowerManager

@Inject
lateinit var application: Application

@Inject
lateinit var getOneByAppWidgetIdResinStatusWidgetUseCase: ResinStatusWidgetUseCase.GetOneByAppWidgetId

@Inject
lateinit var deleteByAppWidgetIdResinStatusWidgetUseCase: ResinStatusWidgetUseCase.DeleteByAppWidgetId

private val _processLifecycleOwner by lazy { ProcessLifecycleOwner.get() }

override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
Expand All @@ -71,20 +65,20 @@ class ResinStatusWidgetProvider : AppWidgetProvider() {
appWidgetIds.map { appWidgetId ->
async(Dispatchers.IO) {
if (powerManager.isPowerSaveMode && !powerManager.isIgnoringBatteryOptimizationsCompat(
application
context
)
) {
RemoteViews(
application.packageName,
context.packageName,
R.layout.widget_resin_status_battery_optimization_enabled
).apply {
setOnClickPendingIntent(
R.id.button_retry,
PendingIntent.getBroadcast(
application,
context,
appWidgetId,
Intent(
application,
context,
ResinStatusWidgetProvider::class.java
).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
Expand All @@ -101,7 +95,7 @@ class ResinStatusWidgetProvider : AppWidgetProvider() {
setOnClickPendingIntent(
R.id.button_change_setting,
PendingIntent.getActivity(
application,
context,
appWidgetId,
Intent(
Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
Expand Down Expand Up @@ -134,7 +128,7 @@ class ResinStatusWidgetProvider : AppWidgetProvider() {
)
.build()

WorkManager.getInstance(application).enqueueUniqueWork(
WorkManager.getInstance(context).enqueueUniqueWork(
it.resinStatusWidget.id.toString(),
ExistingWorkPolicy.APPEND_OR_REPLACE,
oneTimeWorkRequest
Expand All @@ -160,7 +154,7 @@ class ResinStatusWidgetProvider : AppWidgetProvider() {
getOneByAppWidgetIdResinStatusWidgetUseCase.runCatching {
invoke(appWidgetId)
}.onSuccess {
WorkManager.getInstance(application)
WorkManager.getInstance(context)
.cancelUniqueWork(it.resinStatusWidget.refreshGenshinResinStatusWorkerName.toString())

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ fun CreateAttendanceContent(

HorizontalPager(
modifier = Modifier.padding(innerPadding),
state = pagerState
state = pagerState,
userScrollEnabled = false
) { page ->
when (page) {
0 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,29 @@ fun SelectGames(
}

LaunchedEffect(checkedGames().isEmpty()) {
val isEmpty = checkedGames().isEmpty()
withContext(Dispatchers.IO) {
snapshotFlow(connectedGames).catch { }.collect {
when (it) {
is LCE.Content -> {
val isEmpty = checkedGames().isEmpty()

if (isEmpty) {
snackbarHostState.apply {
currentSnackbarData?.dismiss()
showSnackbar(
message = chooseAtLeastOneGame,
duration = SnackbarDuration.Indefinite
)
if (isEmpty) {
snackbarHostState.apply {
currentSnackbarData?.dismiss()
showSnackbar(
message = chooseAtLeastOneGame,
duration = SnackbarDuration.Indefinite
)
}
} else {
snackbarHostState.currentSnackbarData?.dismiss()
}
}
else -> {

}
}
}
} else {
snackbarHostState.currentSnackbarData?.dismiss()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private fun RedemptionCodeListItem(
) {
Column {
TopAppBar(
colors = TopAppBarDefaults.smallTopAppBarColors(
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
),
navigationIcon = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.joeloewi.croissant.ui.navigation.main.settings.screen

import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.exclude
Expand All @@ -11,6 +15,8 @@ import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.selection.toggleable
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AppRegistration
import androidx.compose.material.icons.filled.BatteryFull
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.ExperimentalMaterial3Api
Expand All @@ -29,12 +35,17 @@ import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.semantics.Role
import androidx.core.content.IntentCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
import com.joeloewi.croissant.R
import com.joeloewi.croissant.ui.navigation.main.CroissantNavigation
import com.joeloewi.croissant.util.LocalActivity
import com.joeloewi.croissant.util.SpecialPermission
import com.joeloewi.croissant.util.rememberSpecialPermissionState
import com.joeloewi.croissant.viewmodel.SettingsViewModel

@Composable
Expand All @@ -43,22 +54,34 @@ fun SettingsScreen(
onDeveloperInfoClick: () -> Unit
) {
val darkThemeEnabled by settingsViewModel.darkThemeEnabled.collectAsStateWithLifecycle()
val isUnusedAppRestrictionEnabled by settingsViewModel.isUnusedAppRestrictionEnabled.collectAsStateWithLifecycle()

SettingsContent(
darkThemeEnabled = { darkThemeEnabled },
isUnusedAppRestrictionEnabled = { isUnusedAppRestrictionEnabled },
onDarkThemeEnabled = settingsViewModel::setDarkThemeEnabled,
onDeveloperInfoClick = onDeveloperInfoClick
)
}

@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class)
@Composable
fun SettingsContent(
darkThemeEnabled: () -> Boolean,
isUnusedAppRestrictionEnabled: () -> Result<Boolean>,
onDarkThemeEnabled: (Boolean) -> Unit,
onDeveloperInfoClick: () -> Unit
) {
val activity = LocalActivity.current
val activityResult = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult(),
onResult = {}
)
val ignoreBatteryOptimizations =
rememberSpecialPermissionState(
specialPermission = SpecialPermission.IgnoreBatteryOptimization,
onPermissionResult = {}
)

Scaffold(
topBar = {
Expand Down Expand Up @@ -120,6 +143,99 @@ fun SettingsContent(
)
}

item(
key = "maintenance"
) {
ListItem(
headlineContent = {
Text(
text = stringResource(id = R.string.maintenance),
color = MaterialTheme.colorScheme.primary
)
}
)
}

item(
key = "ignoreBatteryOptimization"
) {
ListItem(
modifier = Modifier.toggleable(
value = ignoreBatteryOptimizations.status.isGranted,
role = Role.Switch,
onValueChange = {
if (ignoreBatteryOptimizations.status.isGranted) {
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", activity.packageName, null)
}.let {
activity.startActivity(it)
}
}
ignoreBatteryOptimizations.launchPermissionRequest()
}
),
leadingContent = {
Icon(
imageVector = Icons.Default.BatteryFull,
contentDescription = Icons.Default.BatteryFull.name
)
},
headlineContent = {
Text(text = stringResource(id = R.string.ignore_battery_optimizations))
},
supportingContent = {
Text(
text = stringResource(id = R.string.ignore_battery_optimizations_description)
)
},
trailingContent = {
Switch(
checked = ignoreBatteryOptimizations.status.isGranted,
onCheckedChange = null
)
}
)
}

item(
key = "unusedAppRestriction"
) {
ListItem(
modifier = Modifier.toggleable(
value = !isUnusedAppRestrictionEnabled().getOrDefault(false),
role = Role.Switch,
onValueChange = {
activityResult.launch(
IntentCompat.createManageUnusedAppRestrictionsIntent(
activity,
activity.packageName
)
)
}
),
leadingContent = {
Icon(
imageVector = Icons.Default.AppRegistration,
contentDescription = Icons.Default.AppRegistration.name
)
},
headlineContent = {
Text(text = stringResource(id = R.string.disable_app_hibernation))
},
supportingContent = {
Text(
text = stringResource(id = R.string.disable_app_hibernation_description)
)
},
trailingContent = {
Switch(
checked = !isUnusedAppRestrictionEnabled().getOrDefault(false),
onCheckedChange = null
)
}
)
}

item(
key = "othersHeader"
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.joeloewi.croissant.util

import android.app.Application
import android.content.Context
import android.os.Build
import android.os.PowerManager

fun PowerManager.isIgnoringBatteryOptimizationsCompat(application: Application) =
fun PowerManager.isIgnoringBatteryOptimizationsCompat(context: Context) =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
isIgnoringBatteryOptimizations(application.packageName)
isIgnoringBatteryOptimizations(context.packageName)
} else {
true
}
Loading