diff --git a/app/src/main/java/dev/arkbuilders/rate/di/AppComponent.kt b/app/src/main/java/dev/arkbuilders/rate/di/AppComponent.kt index 63766677c..744f71bce 100644 --- a/app/src/main/java/dev/arkbuilders/rate/di/AppComponent.kt +++ b/app/src/main/java/dev/arkbuilders/rate/di/AppComponent.kt @@ -16,6 +16,7 @@ import dev.arkbuilders.rate.domain.repo.NetworkStatus import dev.arkbuilders.rate.domain.repo.Prefs import dev.arkbuilders.rate.domain.usecase.CalcFrequentCurrUseCase import dev.arkbuilders.rate.domain.usecase.ConvertWithRateUseCase +import dev.arkbuilders.rate.domain.usecase.GetSortedPinnedQuickPairsUseCase import dev.arkbuilders.rate.presentation.pairalert.AddPairAlertViewModelFactory import dev.arkbuilders.rate.presentation.pairalert.PairAlertViewModelFactory import dev.arkbuilders.rate.presentation.portfolio.AddAssetViewModelFactory @@ -72,6 +73,8 @@ interface AppComponent { fun calcFrequentCurrUseCase(): CalcFrequentCurrUseCase + fun getSortedPinnedQuickPairsUseCase(): GetSortedPinnedQuickPairsUseCase + @Component.Factory interface Factory { fun create( diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/repo/QuickRepo.kt b/app/src/main/java/dev/arkbuilders/rate/domain/repo/QuickRepo.kt index 824f4e1e8..c271d7f04 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/repo/QuickRepo.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/repo/QuickRepo.kt @@ -10,6 +10,8 @@ interface QuickRepo { suspend fun getAll(): List + suspend fun getAllGroups(): List = getAll().groupBy { it.group }.map { it.key } + fun allFlow(): Flow> suspend fun delete(id: Long): Boolean diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/usecase/GetSortedPinnedQuickPairsUseCase.kt b/app/src/main/java/dev/arkbuilders/rate/domain/usecase/GetSortedPinnedQuickPairsUseCase.kt new file mode 100644 index 000000000..d516d73a3 --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/rate/domain/usecase/GetSortedPinnedQuickPairsUseCase.kt @@ -0,0 +1,31 @@ +package dev.arkbuilders.rate.domain.usecase + +import dev.arkbuilders.rate.domain.model.PinnedQuickPair +import dev.arkbuilders.rate.domain.model.QuickPair +import dev.arkbuilders.rate.domain.repo.QuickRepo +import java.time.OffsetDateTime +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class GetSortedPinnedQuickPairsUseCase @Inject constructor( + private val quickRepo: QuickRepo, + private val convertUseCase: ConvertWithRateUseCase, +) { + suspend operator fun invoke() = + quickRepo.getAll() + .filter { it.isPinned() } + .map { + mapPairToPinned(it) + } + .sortedByDescending { it.pair.pinnedDate } + + private suspend fun mapPairToPinned(pair: QuickPair): PinnedQuickPair { + val actualTo = + pair.to.map { to -> + val (amount, _) = convertUseCase.invoke(pair.from, pair.amount, to.code) + amount + } + return PinnedQuickPair(pair, actualTo, OffsetDateTime.now()) + } +} diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/MainScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/MainScreen.kt index a49936843..11dd34cf5 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/MainScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/MainScreen.kt @@ -19,23 +19,29 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.compose.currentBackStackEntryAsState import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi import com.ramcosta.composedestinations.DestinationsNavHost import com.ramcosta.composedestinations.animations.defaults.RootNavGraphDefaultAnimations import com.ramcosta.composedestinations.animations.rememberAnimatedNavHostEngine +import com.ramcosta.composedestinations.navigation.navigate import com.ramcosta.composedestinations.utils.startDestination import dev.arkbuilders.rate.di.DIManager +import dev.arkbuilders.rate.presentation.destinations.AddQuickScreenDestination import dev.arkbuilders.rate.presentation.destinations.PairAlertConditionScreenDestination import dev.arkbuilders.rate.presentation.destinations.PortfolioScreenDestination import dev.arkbuilders.rate.presentation.destinations.QuickScreenDestination import dev.arkbuilders.rate.presentation.destinations.SettingsScreenDestination +import dev.arkbuilders.rate.presentation.quick.glancewidget.action.AddNewPairAction.Companion.ADD_NEW_PAIR +import dev.arkbuilders.rate.presentation.quick.glancewidget.action.AddNewPairAction.Companion.ADD_NEW_PAIR_GROUP_KEY import dev.arkbuilders.rate.presentation.ui.AnimatedRateBottomNavigation import dev.arkbuilders.rate.presentation.ui.ConnectivityOfflineSnackbar import dev.arkbuilders.rate.presentation.ui.ConnectivityOfflineSnackbarVisuals import dev.arkbuilders.rate.presentation.ui.ConnectivityOnlineSnackbar import dev.arkbuilders.rate.presentation.ui.ConnectivityOnlineSnackbarVisuals +import dev.arkbuilders.rate.presentation.utils.findActivity import dev.arkbuilders.rate.presentation.utils.keyboardAsState import kotlinx.coroutines.flow.drop @@ -47,7 +53,19 @@ fun MainScreen() { ) val navController = engine.rememberNavController() val snackState = remember { SnackbarHostState() } + val ctx = LocalContext.current + LaunchedEffect(key1 = Unit) { + val activity = ctx.findActivity() + val intent = activity?.intent + val createNewPair = intent?.getStringExtra(ADD_NEW_PAIR) ?: "" + if (createNewPair.isNotEmpty()) { + val group = intent?.getStringExtra(ADD_NEW_PAIR_GROUP_KEY) + navController.navigate(AddQuickScreenDestination(group = group)) + intent?.removeExtra(ADD_NEW_PAIR_GROUP_KEY) + intent?.removeExtra(ADD_NEW_PAIR) + } + } LaunchedEffect(key1 = Unit) { DIManager.component.networkStatus().onlineStatus .drop(1) diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt index d9d0114b3..cf6b458c9 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt @@ -77,6 +77,7 @@ fun AddQuickScreen( quickPairId: Long? = null, newCode: CurrencyCode? = null, reuseNotEdit: Boolean = true, + group: String? = null, navigator: DestinationsNavigator, ) { val ctx = LocalContext.current @@ -84,7 +85,7 @@ fun AddQuickScreen( viewModel( factory = DIManager.component.addQuickVMFactory() - .create(quickPairId, newCode, reuseNotEdit), + .create(quickPairId, newCode, reuseNotEdit, group), ) val state by viewModel.collectAsState() diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt index 35713d37f..617b4e401 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt @@ -47,6 +47,7 @@ class AddQuickViewModel( private val quickPairId: Long?, private val newCode: CurrencyCode?, private val reuseNotEdit: Boolean, + private val group: String?, private val quickRepo: QuickRepo, private val convertUseCase: ConvertWithRateUseCase, private val codeUseStatRepo: CodeUseStatRepo, @@ -105,7 +106,7 @@ class AddQuickViewModel( newCode?.let { listOf(AmountStr(newCode, "")) } ?: state.currencies - state.copy(currencies = currencies, availableGroups = groups) + state.copy(currencies = currencies, availableGroups = groups, group = group) } } } @@ -201,8 +202,9 @@ class AddQuickViewModel( class AddQuickViewModelFactory @AssistedInject constructor( @Assisted private val quickPairId: Long?, - @Assisted private val newCode: CurrencyCode?, + @Assisted("newCode") private val newCode: CurrencyCode?, @Assisted private val reuseNotEdit: Boolean, + @Assisted("group") private val group: String?, private val quickRepo: QuickRepo, private val codeUseStatRepo: CodeUseStatRepo, private val convertUseCase: ConvertWithRateUseCase, @@ -213,6 +215,7 @@ class AddQuickViewModelFactory @AssistedInject constructor( quickPairId, newCode, reuseNotEdit, + group, quickRepo, convertUseCase, codeUseStatRepo, @@ -224,8 +227,9 @@ class AddQuickViewModelFactory @AssistedInject constructor( interface Factory { fun create( quickPairId: Long?, - newCode: CurrencyCode?, + @Assisted("newCode") newCode: CurrencyCode?, reuseNotEdit: Boolean, + @Assisted("group") group: String?, ): AddQuickViewModelFactory } } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt index 772a9c52a..78813e1b1 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt @@ -1,8 +1,5 @@ package dev.arkbuilders.rate.presentation.quick -import android.app.Activity -import android.content.Context -import android.content.ContextWrapper import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable @@ -26,7 +23,6 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -58,7 +54,6 @@ import dev.arkbuilders.rate.domain.model.CurrencyName import dev.arkbuilders.rate.domain.model.PinnedQuickPair import dev.arkbuilders.rate.domain.model.QuickPair import dev.arkbuilders.rate.presentation.destinations.AddQuickScreenDestination -import dev.arkbuilders.rate.presentation.quick.glancewidget.action.AddNewPairAction.Companion.ADD_NEW_PAIR import dev.arkbuilders.rate.presentation.theme.ArkColor import dev.arkbuilders.rate.presentation.ui.AppButton import dev.arkbuilders.rate.presentation.ui.AppHorDiv16 @@ -91,15 +86,6 @@ fun QuickScreen(navigator: DestinationsNavigator) { val state by viewModel.collectAsState() val snackState = remember { SnackbarHostState() } val ctx = LocalContext.current - LaunchedEffect(key1 = Unit) { - val activity = ctx.findActivity() - val intent = activity?.intent - val createNewPair = intent?.getStringExtra(ADD_NEW_PAIR) ?: "" - if (createNewPair.isNotEmpty()) { - navigator.navigate(AddQuickScreenDestination()) - intent?.removeExtra(ADD_NEW_PAIR) - } - } viewModel.collectSideEffect { effect -> when (effect) { is QuickScreenEffect.ShowSnackbarAdded -> @@ -591,10 +577,3 @@ private fun QuickEmpty(navigator: DestinationsNavigator) { } } } - -fun Context.findActivity(): Activity? = - when (this) { - is Activity -> this - is ContextWrapper -> baseContext.findActivity() - else -> null - } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidget.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidget.kt index 9db64cf5e..98bac6547 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidget.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidget.kt @@ -30,22 +30,27 @@ import androidx.glance.text.FontWeight import androidx.glance.text.Text import androidx.glance.text.TextStyle import androidx.glance.unit.ColorProvider -import com.google.gson.GsonBuilder import dev.arkbuilders.rate.R -import dev.arkbuilders.rate.domain.model.PinnedQuickPair +import dev.arkbuilders.rate.di.DIManager import dev.arkbuilders.rate.presentation.quick.glancewidget.action.AddNewPairAction +import dev.arkbuilders.rate.presentation.quick.glancewidget.action.NextPageAction import dev.arkbuilders.rate.presentation.quick.glancewidget.action.OpenAppAction +import dev.arkbuilders.rate.presentation.quick.glancewidget.action.PreviousPageAction import dev.arkbuilders.rate.presentation.theme.ArkColor class QuickPairsWidget : GlanceAppWidget() { + private val getPinnedUseCase = DIManager.component.getSortedPinnedQuickPairsUseCase() + override suspend fun provideGlance( context: Context, id: GlanceId, ) { + val pinned = getPinnedUseCase.invoke() provideContent { val prefs = currentState() - val quickPairsString = prefs[QuickPairsWidgetReceiver.quickDisplayPairs] - val quickPairsList = quickPairsString?.let { parseQuickPairs(it) } + val group = prefs[QuickPairsWidgetReceiver.currentGroupKey] + val quickPairsList = pinned.filter { it.pair.group == group } + val displayGroup = group ?: context.getString(R.string.group_default_name) Column( modifier = GlanceModifier.fillMaxSize().background(Color.White) @@ -59,10 +64,7 @@ class QuickPairsWidget : GlanceAppWidget() { modifier = GlanceModifier.size(24.dp).padding(4.dp) .clickable(actionRunCallback()), - provider = - ImageProvider( - R.drawable.ic_about_logo, - ), + provider = ImageProvider(R.drawable.ic_about_logo), contentDescription = null, ) Text( @@ -74,6 +76,30 @@ class QuickPairsWidget : GlanceAppWidget() { fontWeight = FontWeight.Medium, ), ) + Text( + modifier = GlanceModifier.defaultWeight(), + text = displayGroup, + style = + TextStyle( + color = ColorProvider(ArkColor.TextTertiary), + fontWeight = FontWeight.Medium, + ), + ) + Image( + modifier = + GlanceModifier.size(24.dp).padding(4.dp) + .clickable(actionRunCallback()), + provider = ImageProvider(R.drawable.ic_chevron_left), + contentDescription = null, + ) + + Image( + modifier = + GlanceModifier.size(24.dp).padding(4.dp) + .clickable(actionRunCallback()), + provider = ImageProvider(R.drawable.ic_chevron_right), + contentDescription = null, + ) Image( modifier = GlanceModifier.size(24.dp).padding(4.dp) @@ -97,29 +123,24 @@ class QuickPairsWidget : GlanceAppWidget() { ) } LazyColumn(modifier = GlanceModifier.fillMaxHeight()) { - quickPairsList?.let { pairs -> - items(pairs) { quick -> - Column { - QuickPairItem( - quick = quick, - context = context, - ) - Spacer( - modifier = - GlanceModifier.fillMaxWidth().height(1.dp) - .background(Color.Gray.copy(alpha = 0.2f)) - .padding(vertical = 2.dp), - ) - } + items(quickPairsList) { quick -> + Column { + QuickPairItem( + quick = quick, + context = context, + ) + Spacer( + modifier = + GlanceModifier + .fillMaxWidth() + .height(1.dp) + .background(Color.Gray.copy(alpha = 0.2f)) + .padding(vertical = 2.dp), + ) } } } } } } - - private fun parseQuickPairs(quickPairsString: String): List { - val gson = GsonBuilder().create() - return gson.fromJson(quickPairsString, Array::class.java).toList() - } } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidgetReceiver.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidgetReceiver.kt index 20dd7c402..2edeed8ab 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidgetReceiver.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/QuickPairsWidgetReceiver.kt @@ -4,31 +4,17 @@ import android.appwidget.AppWidgetManager import android.content.Context import android.content.Intent import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.glance.GlanceId import androidx.glance.appwidget.GlanceAppWidget import androidx.glance.appwidget.GlanceAppWidgetManager import androidx.glance.appwidget.GlanceAppWidgetReceiver import androidx.glance.appwidget.state.updateAppWidgetState -import androidx.glance.state.PreferencesGlanceStateDefinition -import com.google.gson.GsonBuilder import dev.arkbuilders.rate.di.DIManager -import dev.arkbuilders.rate.domain.model.PinnedQuickPair -import dev.arkbuilders.rate.domain.model.QuickPair -import dev.arkbuilders.rate.domain.repo.QuickRepo -import dev.arkbuilders.rate.domain.usecase.ConvertWithRateUseCase -import dev.arkbuilders.rate.presentation.MainActivity -import dev.arkbuilders.rate.presentation.quick.QuickScreenPage -import dev.arkbuilders.rate.presentation.quick.glancewidget.action.AddNewPairAction -import dev.arkbuilders.rate.presentation.quick.glancewidget.action.OpenAppAction import kotlinx.coroutines.MainScope -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch import timber.log.Timber -import java.time.OffsetDateTime -class QuickPairsWidgetReceiver( - private val quickRepo: QuickRepo = DIManager.component.quickRepo(), - private val convertUseCase: ConvertWithRateUseCase = DIManager.component.convertUseCase(), -) : GlanceAppWidgetReceiver() { +class QuickPairsWidgetReceiver : GlanceAppWidgetReceiver() { private val coroutineScope = MainScope() override val glanceAppWidget: GlanceAppWidget = QuickPairsWidget() @@ -42,22 +28,7 @@ class QuickPairsWidgetReceiver( Timber.d(action) when (action) { AppWidgetManager.ACTION_APPWIDGET_ENABLED, PINNED_PAIRS_REFRESH -> - getQuickPairs(context) - OpenAppAction.OPEN_APP -> { - context.startActivity( - Intent(context, MainActivity::class.java).apply { - setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - }, - ) - } - AddNewPairAction.ADD_NEW_PAIR -> { - context.startActivity( - Intent(context, MainActivity::class.java).apply { - putExtra(AddNewPairAction.ADD_NEW_PAIR, "ADD_NEW_PAIR") - setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - }, - ) - } + updateAll(context) } } @@ -67,56 +38,43 @@ class QuickPairsWidgetReceiver( appWidgetIds: IntArray, ) { super.onUpdate(context, appWidgetManager, appWidgetIds) - getQuickPairs(context) + updateAll(context) } - private fun getQuickPairs(context: Context) { - Timber.d("Get quick pairs for widget") - quickRepo.allFlow().onEach { quick -> - val pages = mapPairsToPages(quick) - val quickDisplayPair = pages.first().pinned - val quickPairs = GsonBuilder().create().toJson(quickDisplayPair) - val glanceIds = - GlanceAppWidgetManager(context).getGlanceIds(QuickPairsWidget::class.java) - for (glanceId in glanceIds) { - glanceId.let { id -> - updateAppWidgetState(context, PreferencesGlanceStateDefinition, id) { pref -> - pref.toMutablePreferences().apply { - this[quickDisplayPairs] = quickPairs - } - } - glanceAppWidget.update(context, id) + private fun updateAll(context: Context) { + coroutineScope.launch { + GlanceAppWidgetManager(context) + .getGlanceIds(QuickPairsWidget::class.java) + .forEach { glanceId -> + glanceAppWidget.update(context, glanceId) } - } - }.launchIn(coroutineScope) - } - - private suspend fun mapPairsToPages(pairs: List): List { - val pages = - pairs - .reversed() - .groupBy { it.group } - .map { (group, pairs) -> - val pinnedQuickPairs = pairs.filter { it.isPinned() } - val pinnedMappedQuickPairs = pinnedQuickPairs.map { mapPairToPinned(it) } - val sortedPinned = - pinnedMappedQuickPairs.sortedByDescending { it.pair.pinnedDate } - QuickScreenPage(group, sortedPinned, listOf()) - } - return pages - } - - private suspend fun mapPairToPinned(pair: QuickPair): PinnedQuickPair { - val actualTo = - pair.to.map { to -> - val (amount, _) = convertUseCase.invoke(pair.from, pair.amount, to.code) - amount - } - return PinnedQuickPair(pair, actualTo, OffsetDateTime.now()) + } } companion object { - val quickDisplayPairs = stringPreferencesKey("quick_pair_display") + val currentGroupKey = stringPreferencesKey("currentGroupKey") const val PINNED_PAIRS_REFRESH = "PINNED_PAIRS_REFRESH" + + suspend fun updateWidgetNewGroup( + context: Context, + glanceId: GlanceId, + findNewIndex: (currentIndex: Int?, lastIndex: Int) -> Int, + ) { + val quickRepo = DIManager.component.quickRepo() + val allGroups = quickRepo.getAllGroups() + updateAppWidgetState(context, glanceId) { prefs -> + var currentIndex: Int? = allGroups.indexOf(prefs[currentGroupKey]) + if (currentIndex == -1) + currentIndex = null + val newIndex = findNewIndex(currentIndex, allGroups.lastIndex) + val newGroup = allGroups[newIndex] + if (newGroup != null) { + prefs[currentGroupKey] = newGroup + } else { + prefs.remove(currentGroupKey) + } + } + QuickPairsWidget().update(context, glanceId) + } } } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/AddNewPairAction.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/AddNewPairAction.kt index 9cf90980b..3b3bf3b7c 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/AddNewPairAction.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/AddNewPairAction.kt @@ -5,6 +5,8 @@ import android.content.Intent import androidx.glance.GlanceId import androidx.glance.action.ActionParameters import androidx.glance.appwidget.action.ActionCallback +import androidx.glance.appwidget.state.updateAppWidgetState +import dev.arkbuilders.rate.presentation.MainActivity import dev.arkbuilders.rate.presentation.quick.glancewidget.QuickPairsWidgetReceiver class AddNewPairAction : ActionCallback { @@ -13,14 +15,22 @@ class AddNewPairAction : ActionCallback { glanceId: GlanceId, parameters: ActionParameters, ) { - val intent = - Intent(context, QuickPairsWidgetReceiver::class.java).apply { - action = ADD_NEW_PAIR - } - context.sendBroadcast(intent) + var group: String? = null + + updateAppWidgetState(context, glanceId) { pref -> + group = pref[QuickPairsWidgetReceiver.currentGroupKey] + } + context.startActivity( + Intent(context, MainActivity::class.java).apply { + putExtra(ADD_NEW_PAIR, "ADD_NEW_PAIR") + putExtra(ADD_NEW_PAIR_GROUP_KEY, group) + setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + }, + ) } companion object { const val ADD_NEW_PAIR = "QUICK_PAIRS_WIDGET_NEW_PAIR" + const val ADD_NEW_PAIR_GROUP_KEY = "ADD_NEW_PAIR_GROUP_KEY" } } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/NextPageAction.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/NextPageAction.kt new file mode 100644 index 000000000..2e8a2d800 --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/NextPageAction.kt @@ -0,0 +1,27 @@ +package dev.arkbuilders.rate.presentation.quick.glancewidget.action + +import android.content.Context +import androidx.glance.GlanceId +import androidx.glance.action.ActionParameters +import androidx.glance.appwidget.action.ActionCallback +import dev.arkbuilders.rate.presentation.quick.glancewidget.QuickPairsWidgetReceiver + +class NextPageAction : ActionCallback { + override suspend fun onAction( + context: Context, + glanceId: GlanceId, + parameters: ActionParameters, + ) { + QuickPairsWidgetReceiver.updateWidgetNewGroup( + context = context, + glanceId = glanceId, + findNewIndex = { currentIndex, lastIndex -> + var newIndex = currentIndex?.let { it + 1 } ?: 0 + if (newIndex > lastIndex) { + newIndex = 0 + } + newIndex + }, + ) + } +} diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/OpenAppAction.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/OpenAppAction.kt index 02c1e4958..629c5d8b2 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/OpenAppAction.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/OpenAppAction.kt @@ -5,7 +5,7 @@ import android.content.Intent import androidx.glance.GlanceId import androidx.glance.action.ActionParameters import androidx.glance.appwidget.action.ActionCallback -import dev.arkbuilders.rate.presentation.quick.glancewidget.QuickPairsWidgetReceiver +import dev.arkbuilders.rate.presentation.MainActivity class OpenAppAction : ActionCallback { override suspend fun onAction( @@ -13,14 +13,10 @@ class OpenAppAction : ActionCallback { glanceId: GlanceId, parameters: ActionParameters, ) { - val intent = - Intent(context, QuickPairsWidgetReceiver::class.java).apply { - action = OPEN_APP - } - context.sendBroadcast(intent) - } - - companion object { - const val OPEN_APP = "QUICK_PAIRS_WIDGET_OPEN_APP" + context.startActivity( + Intent(context, MainActivity::class.java).apply { + setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + }, + ) } } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/PreviousPageAction.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/PreviousPageAction.kt new file mode 100644 index 000000000..3f4281b7c --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/glancewidget/action/PreviousPageAction.kt @@ -0,0 +1,27 @@ +package dev.arkbuilders.rate.presentation.quick.glancewidget.action + +import android.content.Context +import androidx.glance.GlanceId +import androidx.glance.action.ActionParameters +import androidx.glance.appwidget.action.ActionCallback +import dev.arkbuilders.rate.presentation.quick.glancewidget.QuickPairsWidgetReceiver + +class PreviousPageAction : ActionCallback { + override suspend fun onAction( + context: Context, + glanceId: GlanceId, + parameters: ActionParameters, + ) { + QuickPairsWidgetReceiver.updateWidgetNewGroup( + context = context, + glanceId = glanceId, + findNewIndex = { currentIndex, lastIndex -> + var newIndex = currentIndex?.let { it - 1 } ?: 0 + if (newIndex < 0) { + newIndex = lastIndex + } + newIndex + }, + ) + } +} diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/utils/ContextUtils.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/utils/ContextUtils.kt index cffbfc085..c1b93f810 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/utils/ContextUtils.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/utils/ContextUtils.kt @@ -1,6 +1,8 @@ package dev.arkbuilders.rate.presentation.utils +import android.app.Activity import android.content.Context +import android.content.ContextWrapper import android.content.Intent import android.net.Uri import android.widget.Toast @@ -40,3 +42,10 @@ fun Context.openEmail( } } } + +fun Context.findActivity(): Activity? = + when (this) { + is Activity -> this + is ContextWrapper -> baseContext.findActivity() + else -> null + } diff --git a/app/src/main/res/drawable/ic_chevron_left.xml b/app/src/main/res/drawable/ic_chevron_left.xml new file mode 100644 index 000000000..81b9ffac9 --- /dev/null +++ b/app/src/main/res/drawable/ic_chevron_left.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_chevron_right.xml b/app/src/main/res/drawable/ic_chevron_right.xml new file mode 100644 index 000000000..249e60c19 --- /dev/null +++ b/app/src/main/res/drawable/ic_chevron_right.xml @@ -0,0 +1,5 @@ + + + + +