com.spotify.mobius.ControllerStateRunning Maven / Gradle / Ivy
/*
* -\-\-
* Mobius
* --
* Copyright (c) 2017-2018 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.mobius;
import com.spotify.mobius.functions.Consumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
class ControllerStateRunning extends ControllerStateBase {
@Nonnull private final ControllerActions actions;
@Nonnull private final Connection renderer;
@Nonnull private final MobiusLoop loop;
@Nonnull private final M startModel;
ControllerStateRunning(
ControllerActions actions,
Connection renderer,
MobiusLoop.Factory loopFactory,
M modelToStartFrom,
@Nullable Init init) {
this.actions = actions;
this.renderer = renderer;
if (init != null) {
First first = init.init(modelToStartFrom);
this.loop = loopFactory.startFrom(first.model(), first.effects());
this.startModel = first.model();
} else {
this.loop = loopFactory.startFrom(modelToStartFrom);
this.startModel = modelToStartFrom;
}
}
void start() {
loop.observe(
new Consumer() {
@Override
public void accept(M model) {
actions.postUpdateView(model);
}
});
}
@Override
protected String getStateName() {
return "running";
}
@Override
public boolean isRunning() {
return true;
}
@Override
public void onDispatchEvent(E event) {
loop.dispatchEvent(event);
}
@Override
public void onUpdateView(M model) {
renderer.accept(model);
}
@Override
public void onStop() {
loop.dispose();
M mostRecentModel = loop.getMostRecentModel();
actions.goToStateCreated(renderer, mostRecentModel);
}
@Nonnull
@Override
public M onGetModel() {
M model = loop.getMostRecentModel();
return model != null ? model : startModel;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy