Skip to content

Commit

Permalink
Set initial UI state in onResumeFragments.
Browse files Browse the repository at this point in the history
onResumeFragments is when the fragments state has been restored.
The app doesn't save any UI state (onSaveInstanceState implementations
are empty, and all UI state is derived from the state of the services
for playback and sample downloads) it's better this way in case some
operations are performed at this stage.
  • Loading branch information
msimonides committed Jan 2, 2019
1 parent a7bb7a7 commit 5bbb4b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ protected void onStart() {
handleIntent(getIntent());
}

@Override
protected void onResumeFragments() {
super.onResumeFragments();
controller.onActivityResumeFragments();
}

@SuppressLint("MissingSuperCall")
@Override
protected void onSaveInstanceState(Bundle outState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class UiControllerMain implements ServiceConnection {
private @Nullable PlaybackService playbackService;

private @NonNull State currentState = new InitState();
private boolean isRunning = false;

@Inject
UiControllerMain(@NonNull AppCompatActivity activity,
Expand Down Expand Up @@ -76,11 +77,16 @@ void onActivityCreated() {
void onActivityStart() {
Crashlytics.log(Log.DEBUG, TAG,"activity start");
scanAudioBookFiles();
}

void onActivityResumeFragments() {
isRunning = true;
maybeSetInitialState();
}

void onActivityPause() {
currentState.onActivityPause();
isRunning = false;
}

void onActivityStop() {
Expand Down Expand Up @@ -193,7 +199,7 @@ private void scanAudioBookFiles() {

private void maybeSetInitialState() {
if (currentState instanceof InitState && playbackService != null &&
audioBookManager.isInitialized()) {
audioBookManager.isInitialized() && isRunning) {

if (playbackService.getState() != PlaybackService.State.IDLE) {
Preconditions.checkState(hasAnyBooks());
Expand All @@ -215,6 +221,10 @@ private AudioBook currentAudioBook() {
}

private void changeState(StateFactory newStateFactory) {
Preconditions.checkState(isRunning,
"attempt to switch state from " + currentState.debugName() + " to "
+ newStateFactory.name() + " while activity is paused");

Crashlytics.log(Log.DEBUG, TAG, "UI: leave state: " + currentState.debugName());
currentState.onLeaveState();
Crashlytics.log(Log.DEBUG, TAG,"UI: create state: " + newStateFactory.name());
Expand Down

0 comments on commit 5bbb4b6

Please sign in to comment.