-
Notifications
You must be signed in to change notification settings - Fork 2
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
Pinned quick pairs widget #115
Conversation
# Conflicts: # app/src/main/java/dev/arkbuilders/rate/di/AppComponent.kt # app/src/main/java/dev/arkbuilders/rate/presentation/App.kt # app/src/main/java/dev/arkbuilders/rate/presentation/MainActivity.kt # app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt
val allGroups = quickRepo.getAllGroups() | ||
updateAppWidgetState(context, glanceId) { prefs -> | ||
val currentIndex = allGroups.indexOf(prefs[currentGroupKey]) | ||
var newIndex = if (currentIndex == -1) 0 else currentIndex + 1 | ||
if (newIndex > allGroups.lastIndex) { | ||
newIndex = 0 | ||
} | ||
val newGroup = allGroups[newIndex] | ||
newGroup?.let { | ||
prefs[currentGroupKey] = newGroup | ||
} ?: let { | ||
prefs.remove(currentGroupKey) | ||
} | ||
} | ||
QuickPairsWidget().update(context, glanceId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this particular code block, for NextPageAction
and PreviousPageAction
, there's only difference in the way we find newIndex
. Hence we could share the rest of the code and only . HOC is a good fit in this case.
So we can have a helper file with this function:
suspend fun updatePinnedPairsWidgetNewPageState(
context: Context,
glanceId: GlanceId,
quickRepo: QuickRepo,
findNextIndexAction: (Int, Int) -> Int,
) {
val allGroups = quickRepo.getAllGroups()
updateAppWidgetState(context, glanceId) { prefs ->
val currentIndex = allGroups.indexOf(prefs[QuickPairsWidgetReceiver.currentGroupKey])
val newIndex = findNextIndexAction(currentIndex, allGroups.lastIndex)
val newGroup = allGroups[newIndex]
if (newGroup != null) {
prefs[QuickPairsWidgetReceiver.currentGroupKey] = newGroup
} else {
prefs.remove(QuickPairsWidgetReceiver.currentGroupKey)
}
}
QuickPairsWidget().update(context, glanceId)
}
And then update code in our two classes for example:
class NextPageAction : ActionCallback {
override suspend fun onAction(
context: Context,
glanceId: GlanceId,
parameters: ActionParameters,
) {
val quickRepo = DIManager.component.quickRepo()
updatePinnedPairsWidgetNewPageState(
context = context,
glanceId = glanceId,
quickRepo = quickRepo,
findNextIndexAction = { current, last ->
findIndex(current, last)
},
)
}
private fun findIndex(
currentIndex: Int,
lastIndex: Int,
): Int {
var newIndex = if (currentIndex == -1) 0 else currentIndex + 1
if (newIndex > lastIndex) {
newIndex = 0
}
return newIndex
}
}
Replaced by #117 |
Continuation of #100