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

ALTAPPS-506: Shared load gems and streak on the Track screen #315

Merged
merged 21 commits into from
Jan 16, 2023

Conversation

ivan-magda
Copy link
Member

@ivan-magda ivan-magda commented Jan 11, 2023

YouTrack Issues:
#ALTAPPS-506, #ALTAPPS-518

Checklist

Before Code Review:

  • Fields "Assignees, Labels, Milestone" are filled in the pull request;
  • Sentry Performance Monitoring of screen loading is added for new screens;
  • View analytics events are added for new screens;
  • New analytics events are documented;
  • All checks have been passed;
  • Changes have been checked locally.

Description

Miscellaneous changes:

  • Adds StreaksDataComponent that provides StreaksInteractor. It helps to avoid code duplication on the injection level.
  • Refactors user streaks loading in the StreaksRemoteDataSourceImpl, no need to load all pages, because we are using only the first Streak element.
  • Extracts getRepetitionsState into helper function in HomeActionDispatcher, which makes code more readable.
  • Fixes incorrect forceUpdate value parameter for HomeFeature.Message.Initialize message on tryAgain in HomeFragment
  • Optimizes imports

Primary changes:

  • Adds GamificationToolbarFeature that encapsulates loading of the streak and hypercoins balance. It also moves analytic events to this feature and ViewActions.
  • Migrates HomeFeature to using the GamificationToolbarFeature on iOS and Android.
  • Connects TrackFeature with the GamificationToolbarFeature on iOS only. Android implementation has remote data loading, and messages that trigger remote data loading are marked with TODO, we should implement UI or delete those messages before merging this PR.

Also, we could send messages to the GamificationToolbarFeature from the parent feature directly. For example Message.Initialize. The current implementation sends 2 messages: one for the parent feature (home or track) and the second for the toolbar:

homeViewModel.onNewMessage(HomeFeature.Message.Initialize(forceUpdate = false))

homeViewModel.onNewMessage(
    HomeFeature.Message.GamificationToolbarMessage(
        GamificationToolbarFeature.Message.Initialize(
            screen = GamificationToolbarScreen.HOME,
            forceUpdate = false
        )
    )
)

we could update our HomeReducer to initialize home and toolbar in one place:

is Message.Initialize -> {
    val (homeState, homeActions) = if (state.homeState is HomeState.Idle ||
        (message.forceUpdate && (state.homeState is HomeState.Content || state.homeState is HomeState.NetworkError))
    ) {
        HomeState.Loading to setOf(Action.FetchHomeScreenData)
    } else {
        state.homeState to emptySet()
    }

    val (toolbarState, toolbarActions) = reduceGamificationToolbarMessage(
        state.toolbarState,
        GamificationToolbarFeature.Message.Initialize(GamificationToolbarScreen.HOME, message.forceUpdate)
    )

    state.copy(
        homeState = homeState,
        toolbarState = toolbarState
    ) to homeActions + toolbarActions
}

/**
 * Reduces [Message.GamificationToolbarMessage] to [GamificationToolbarFeature.State] and set of [HomeFeature.Action]
 * Avoids code duplication when reducing [Message.GamificationToolbarMessage]
 */
private fun reduceGamificationToolbarMessage(
    state: GamificationToolbarFeature.State,
    message: GamificationToolbarFeature.Message
): Pair<GamificationToolbarFeature.State, Set<HomeFeature.Action>> {
    val (gamificationToolbarState, gamificationToolbarActions) = gamificationToolbarReducer.reduce(state, message)

    val actions = gamificationToolbarActions
        .map {
            if (it is GamificationToolbarFeature.Action.ViewAction) {
                HomeFeature.Action.ViewAction.GamificationToolbarViewAction(it)
            } else {
                HomeFeature.Action.GamificationToolbarAction(it)
            }
        }
        .toSet()

    return gamificationToolbarState to actions
}

I tried this approach in the HomeReducer and it's become harder to read/understand code and more complicated than just sending messages from the Fragment on Android or ViewModel on iOS.

I think there is no silver bullet - the above approach adds the ability to send only one message to initialize parent and child features, but the source code gets more complicated in the reducer and the other approach with sending distinct messages (first for home or track and second for toolbar) leaves reducer simpler, but we could forget to send some message from Fragment/ViewModel.

So, to be discussed...

@ivan-magda ivan-magda added this to the 1.7 milestone Jan 11, 2023
@ivan-magda ivan-magda self-assigned this Jan 11, 2023
@github-actions github-actions bot added the shared Shared module task label Jan 11, 2023
@github-actions github-actions bot added android Android module task ios iOS module task labels Jan 11, 2023
@ivan-magda ivan-magda marked this pull request as ready for review January 11, 2023 18:01
@ivan-magda ivan-magda requested review from XanderZhu and removed request for XanderZhu January 11, 2023 18:04
@ivan-magda ivan-magda marked this pull request as draft January 12, 2023 02:43
@ivan-magda ivan-magda marked this pull request as ready for review January 14, 2023 16:37
@ivan-magda ivan-magda added the awaiting review Pull Request is awaiting code reviews label Jan 14, 2023
@XanderZhu
Copy link
Contributor

GamificationToolbarFeature - Nice naming

@XanderZhu
Copy link
Contributor

XanderZhu commented Jan 14, 2023

Also, we could send messages to the GamificationToolbarFeature from the parent feature directly.

I think it would be useful to follow this approach.

@XanderZhu XanderZhu added awaiting changes Changes requested, waiting on author to update and removed awaiting review Pull Request is awaiting code reviews labels Jan 14, 2023
@XanderZhu XanderZhu added ready to pull Pull Request is ready to merge and removed awaiting changes Changes requested, waiting on author to update labels Jan 16, 2023
To not contact with API on Android on Track screen.
@ivan-magda ivan-magda merged commit 73bdc0b into develop Jan 16, 2023
@ivan-magda ivan-magda deleted the feature/ALTAPPS_506/shared_track_gems_streak branch January 16, 2023 10:32
@ivan-magda ivan-magda mentioned this pull request Jan 16, 2023
@ivan-magda ivan-magda removed the ready to pull Pull Request is ready to merge label Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Android module task ios iOS module task shared Shared module task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants