Skip to content

Commit

Permalink
Navigator as ViewModel pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
liujoshua committed Apr 24, 2018
1 parent 433d0d5 commit 27cffd8
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 98 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
1 change: 1 addition & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ dependencies {
implementation 'com.ryanharter.auto.value:auto-value-parcel-adapter:0.2.6'

implementation "android.arch.lifecycle:extensions:1.1.1"

annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
implementation 'com.android.support:appcompat-v7:27.1.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
.build();
}
if (performTaskFragment == null) {
performTaskFragment = PerformTaskFragment.newInstance(taskView);
performTaskFragment = PerformTaskFragment.newInstance("taskId");

getSupportFragmentManager()
.beginTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.sagebionetworks.research.data.model.step.ConcreteUIStep;
import org.sagebionetworks.research.domain.Schema;
import org.sagebionetworks.research.domain.repository.TaskRepository;
import org.sagebionetworks.research.domain.result.TaskResult;
import org.sagebionetworks.research.domain.step.Step;
import org.sagebionetworks.research.domain.step.ui.UIStep;
import org.sagebionetworks.research.domain.task.Task;
Expand All @@ -47,14 +48,12 @@
import java.util.List;
import java.util.UUID;

import io.reactivex.Completable;
import io.reactivex.Single;
import javax.inject.Inject;

public class FakeTaskRepository implements TaskRepository {
@Inject
public FakeTaskRepository() {
}

@Inject public FakeTaskRepository() {}
@Override
public Single<Task> getTask(final String taskIdentifier) {
return Single.<Task>just(new Task() {
Expand Down Expand Up @@ -106,6 +105,12 @@ public Single<List<Step>> getTaskSteps(final Task task) {
));
}

@NonNull
@Override
public Completable setTaskResult(final TaskResult taskResult) {
return Completable.error(new UnsupportedOperationException("Not implemented yet"));
}

private UIStep createUIStep(String id) {
return ConcreteUIStep.builder()
.setIdentifier(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@

package org.sagebionetworks.research.domain.repository;

import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;

import org.sagebionetworks.research.domain.result.TaskResult;
import org.sagebionetworks.research.domain.step.Step;
import org.sagebionetworks.research.domain.task.Task;

import java.util.List;

import io.reactivex.Completable;
import io.reactivex.Single;

public interface TaskRepository {
Expand All @@ -47,4 +50,8 @@ public interface TaskRepository {

@NonNull
Single<List<Step>> getTaskSteps(Task task);

@NonNull
@CheckResult
Completable setTaskResult(TaskResult taskResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.sagebionetworks.research.domain.task.navigation.strategy.StepNavigationStrategy.NextStepStrategy;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;

public class ConstantNextStepStrategyTest {

Expand Down
1 change: 1 addition & 0 deletions mobile-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependencies {
implementation 'android.arch.lifecycle:extensions:1.1.1'
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
testImplementation 'junit:junit:4.12'
testImplementation "android.arch.core:core-testing:1.1.1"
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@

package org.sagebionetworks.research.mobile_ui.mapper;

import javax.inject.Inject;

public class TaskMapper {
@Inject
public TaskMapper() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
import android.view.View;
import android.view.ViewGroup;

import com.google.common.base.Strings;

import org.sagebionetworks.research.domain.mobile_ui.R;
import org.sagebionetworks.research.mobile_ui.mapper.StepMapper;
import org.sagebionetworks.research.mobile_ui.show_step.StepPresenter;
import org.sagebionetworks.research.mobile_ui.show_step.StepPresenterFactory;
import org.sagebionetworks.research.mobile_ui.show_step.view.ShowStepFragment;
import org.sagebionetworks.research.mobile_ui.show_step.view.ShowStepFragmentBase;
import org.sagebionetworks.research.presentation.model.StepView;
import org.sagebionetworks.research.presentation.model.StepView.NavDirection;
import org.sagebionetworks.research.presentation.model.TaskView;
import org.sagebionetworks.research.presentation.perform_task.PerformTaskViewModel;
import org.sagebionetworks.research.presentation.perform_task.PerformTaskViewModelFactory;

Expand All @@ -70,7 +70,7 @@
* A placeholder fragment containing a simple view.
*/
public class PerformTaskFragment extends Fragment implements HasSupportFragmentInjector {
private static final String ARGUMENT_TASK_VIEW = "TASK_VIEW";
private static final String ARGUMENT_TASK_IDENTIFIER = "TASK_VIEW";

@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;
Expand All @@ -88,17 +88,15 @@ public class PerformTaskFragment extends Fragment implements HasSupportFragmentI

private PerformTaskViewModel performTaskViewModel;

private StepPresenter stepPresenter;

private TaskView taskView;
private String taskIdentifier;

private Unbinder unbinder;

public static PerformTaskFragment newInstance(@NonNull TaskView taskView) {
checkNotNull(taskView);
public static PerformTaskFragment newInstance(@NonNull String taskIdentifier) {
checkNotNull(taskIdentifier);

Bundle arguments = new Bundle();
arguments.putParcelable(ARGUMENT_TASK_VIEW, taskView);
arguments.putString(ARGUMENT_TASK_IDENTIFIER, taskIdentifier);

PerformTaskFragment fragment = new PerformTaskFragment();
fragment.setArguments(arguments);
Expand All @@ -119,14 +117,14 @@ public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
Bundle arguments = getArguments();
if (arguments != null) {
taskView = getArguments().getParcelable(ARGUMENT_TASK_VIEW);
taskIdentifier = getArguments().getString(ARGUMENT_TASK_IDENTIFIER);
}
} else {
taskView = savedInstanceState.getParcelable(ARGUMENT_TASK_VIEW);
taskIdentifier = savedInstanceState.getString(ARGUMENT_TASK_IDENTIFIER);
}
checkState(taskView != null, "no taskView found");
checkState(!Strings.isNullOrEmpty(taskIdentifier), "taskIdentifier cannot be null or empty");

performTaskViewModel = ViewModelProviders.of(this, taskViewModelFactory.create(taskView))
performTaskViewModel = ViewModelProviders.of(this, taskViewModelFactory.create(taskIdentifier))
.get(PerformTaskViewModel.class);

performTaskViewModel.getStep().observe(this, this::showStep);
Expand All @@ -143,7 +141,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
@Override
public void onSaveInstanceState(Bundle outState) {
if (outState != null) {
outState.putParcelable(ARGUMENT_TASK_VIEW, taskView);
outState.putString(ARGUMENT_TASK_IDENTIFIER, taskIdentifier);
}
}

Expand Down
6 changes: 6 additions & 0 deletions presentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ dependencies {

implementation 'com.google.guava:guava:24.1-android'
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

implementation 'org.slf4j:slf4j-api:1.7.21'

api 'com.google.dagger:dagger-android:2.15'
Expand All @@ -83,7 +85,11 @@ dependencies {
annotationProcessor 'com.ryanharter.auto.value:auto-value-parcel:0.2.6'

implementation 'com.android.support:appcompat-v7:27.1.1'

testImplementation 'junit:junit:4.12'
testImplementation "android.arch.core:core-testing:1.1.1"
testImplementation 'org.mockito:mockito-core:2.8.9'

androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@
import org.sagebionetworks.research.domain.task.Task;
import org.sagebionetworks.research.presentation.model.TaskView;

import javax.inject.Inject;

/**
* Map a {@link Task} to a {@link TaskView} when data is moving between the Domain layer and this layer.
*/
public class TaskMapper implements Function<Task, TaskView> {
@Inject
public TaskMapper() {

}

@Override
@NonNull
public TaskView apply(@NonNull Task input) {
Expand Down
Loading

0 comments on commit 27cffd8

Please sign in to comment.