jeudi 2 janvier 2020

I have a "Ticket" which needs to have state remembered across a life cycle

I have a "Ticket", it has features such as Name and UUID etc... It has a selection state. The selection is remembered across activities. So returning to the recycler view means it "remains" selected (no, not actually... it's re-selected via coding magic) It has a "New" and "Existing" state It can transition from "New" to "Existing" mid lifecycle. (Lifecycle of the ticket, not lifecycle of the activity)

I have created a state engine, but this seems to be the wrong pattern. The purpose of the class in the example below is to decouple the activities/components.

I know that the following example does not look like a state engine, which is why I'm asking this question, an actual state engine doesn't seem to fit my needs.

What is the recommended way to handle the following example:

public class TicketStateEngine {

    private static int currentSelectedRecyclerViewPosition = -1;
    private static LinearLayoutManager mLinearLayoutManager;
    private static boolean isNewTicket;

    public static boolean isIsNewTicket() {
        return isNewTicket;
    }

    public static void setIsNewTicket(boolean isNewTicket) {
        TicketStateEngine.isNewTicket = isNewTicket;
    }

    public static void setLinearLayoutManager(LinearLayoutManager linearLayoutManager) {
        mLinearLayoutManager = linearLayoutManager;
    }

    public static int getCurrentSelectedRecyclerViewPosition() {
        return currentSelectedRecyclerViewPosition;
    }

    public static void setCurrentSelectedRecyclerViewPosition(int currentSelectedRecyclerViewPosition) {
        TicketStateEngine.currentSelectedRecyclerViewPosition = currentSelectedRecyclerViewPosition;
    }

    public static void scrollToSelected() {
        try {
            mLinearLayoutManager.scrollToPositionWithOffset(currentSelectedRecyclerViewPosition, 0);
        } catch (NullPointerException e) {
            Log.e("NullPointerException", "Make sure a Linear Layout manger is set for TicketStateEngine");
            throw e;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire