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

Pinned quick pairs widget #115

Closed
wants to merge 48 commits into from
Closed

Conversation

mdrlzy
Copy link
Member

@mdrlzy mdrlzy commented Sep 19, 2024

Continuation of #100

Comment on lines +19 to +33
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)
Copy link
Collaborator

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
    }
}

@mdrlzy
Copy link
Member Author

mdrlzy commented Sep 21, 2024

Replaced by #117

@mdrlzy mdrlzy closed this Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants