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

MVC #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .idea/gradle.xml

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

6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="upday.mvpvsmvvm"
<manifest package="upday.patterns"
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".DroidconApplication"
android:name="upday.patterns.DroidconApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".mvp.MVPActivity">
<activity android:name="upday.patterns.mvp.MVPActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/upday/mvpvsmvvm/datamodel/IDataModel.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package upday.mvpvsmvvm;
package upday.patterns;

import android.app.Application;
import android.support.annotation.NonNull;

import upday.mvpvsmvvm.datamodel.DataModel;
import upday.mvpvsmvvm.datamodel.IDataModel;
import upday.patterns.datamodel.DataModel;
import upday.patterns.datamodel.IDataModel;

public class DroidconApplication extends Application {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package upday.mvpvsmvvm.datamodel;
package upday.patterns.datamodel;

import android.support.annotation.NonNull;

Expand All @@ -8,7 +8,13 @@ public class DataModel implements IDataModel {

@NonNull
@Override
public Observable<String> getGreeting() {
public Observable<String> getGreetingAsync() {
return Observable.just("Hello, World!");
}

@NonNull
@Override
public String getGreetingSync() {
return "Hello World";
}
}
24 changes: 24 additions & 0 deletions app/src/main/java/upday/patterns/datamodel/IDataModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package upday.patterns.datamodel;

import android.support.annotation.NonNull;

import rx.Observable;

/**
* The Data Model used by all patterns
*/
public interface IDataModel {

/**
* @return a stream of the greeting. The stream will emit once and then complete.
* Allows asynchronous retrieval of the greeting.
*/
@NonNull
Observable<String> getGreetingAsync();

/**
* @return a greeting synchronously.
*/
@NonNull
String getGreetingSync();
}
17 changes: 17 additions & 0 deletions app/src/main/java/upday/patterns/mvc/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package upday.patterns.mvc;

import android.support.annotation.NonNull;

/**
* Implementation class for the Controller in the MVC pattern, with a passive model.
*/
public class Controller {

@NonNull
private final IView mView;

public Controller(@NonNull final IView view) {
mView = view;
mView.update();
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/upday/patterns/mvc/IView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package upday.patterns.mvc;

/**
* A view that can update.
*/
public interface IView {

void update();
}
53 changes: 53 additions & 0 deletions app/src/main/java/upday/patterns/mvc/MVCActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package upday.patterns.mvc;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import upday.patterns.DroidconApplication;
import upday.patterns.R;
import upday.patterns.datamodel.IDataModel;

/**
* Implements the view class of the MVC pattern with passive model.
*/
public class MVCActivity extends AppCompatActivity implements IView {

@NonNull
private Controller mController;

@NonNull
private IDataModel mDataModel;

@Nullable
private TextView mGreetingView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mDataModel = getDataModel();
mController = new Controller(this);

setupViews();
}

private void setupViews() {
mGreetingView = (TextView) findViewById(R.id.greeting);
}

@Override
public void update() {
assert mGreetingView != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@florina-muntenescu how do you assert on Android ?

mGreetingView.setText(mDataModel.getGreetingSync());
}

@NonNull
private IDataModel getDataModel() {
return ((DroidconApplication) getApplication()).getDataModel();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package upday.mvpvsmvvm.mvp;
package upday.patterns.mvp;

/**
* Interface for the Presenter class in the MVP pattern.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package upday.mvpvsmvvm.mvp;
package upday.patterns.mvp;

import android.support.annotation.NonNull;

/**
* Interface for the view classes in the MVP pattern.
*/
public interface IView {
interface IView {

void setGreeting(@NonNull final String greeting);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package upday.mvpvsmvvm.mvp;
package upday.patterns.mvp;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import upday.mvpvsmvvm.DroidconApplication;
import upday.mvpvsmvvm.R;
import upday.mvpvsmvvm.datamodel.IDataModel;
import upday.patterns.DroidconApplication;
import upday.patterns.R;
import upday.patterns.datamodel.IDataModel;

/**
* Implements the view class of the MVP pattern.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package upday.mvpvsmvvm.mvp;
package upday.patterns.mvp;

import android.support.annotation.NonNull;

import rx.subscriptions.CompositeSubscription;
import upday.mvpvsmvvm.datamodel.IDataModel;
import upday.patterns.datamodel.IDataModel;

/**
* Implementation class for the Presenter in the MVP model.
Expand All @@ -28,7 +28,7 @@ public Presenter(@NonNull final IDataModel dataModel,
public void bind() {
mSubscription = new CompositeSubscription();

mSubscription.add(mDataModel.getGreeting()
mSubscription.add(mDataModel.getGreetingAsync()
.subscribe(this::setGreeting));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package upday.mvpvsmvvm.mvvm;
package upday.patterns.mvvm;

import android.os.Bundle;
import android.support.annotation.NonNull;
Expand All @@ -7,9 +7,9 @@
import android.widget.TextView;

import rx.subscriptions.CompositeSubscription;
import upday.mvpvsmvvm.DroidconApplication;
import upday.mvpvsmvvm.R;
import upday.mvpvsmvvm.datamodel.IDataModel;
import upday.patterns.DroidconApplication;
import upday.patterns.R;
import upday.patterns.datamodel.IDataModel;

public class MVVMActivity extends AppCompatActivity {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package upday.mvpvsmvvm.mvvm;
package upday.patterns.mvvm;

import android.support.annotation.NonNull;

import rx.Observable;
import upday.mvpvsmvvm.datamodel.IDataModel;
import upday.patterns.datamodel.IDataModel;

/**
* View model for the main activity.
Expand All @@ -19,6 +19,6 @@ public ViewModel(@NonNull final IDataModel dataModel) {

@NonNull
public Observable<String> getGreeting() {
return mDataModel.getGreeting();
return mDataModel.getGreetingAsync();
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">MVP vs MVVM</string>
<string name="app_name">UI Architecture Patterns</string>
</resources>
30 changes: 30 additions & 0 deletions app/src/test/java/upday/patterns/mvc/ControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package upday.patterns.mvc;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
* Class that tests the implementation of {@link Controller}
*/
public class ControllerTest {

@Mock
private IView mView;

private Controller mController;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}

@Test
public void updateCalled_whenControllerCreated() {
mController = new Controller(mView);

Mockito.verify(mView).update();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package upday.mvpvsmvvm.mvp;
package upday.patterns.mvp;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -7,7 +7,7 @@
import org.mockito.MockitoAnnotations;

import rx.Observable;
import upday.mvpvsmvvm.datamodel.IDataModel;
import upday.patterns.datamodel.IDataModel;

public class PresenterTest {

Expand All @@ -29,7 +29,7 @@ public void setUp() throws Exception {
@Test
public void testGetGreeting_set_whenViewBinded() {
String greeting = "Hello!";
Mockito.when(mDataModel.getGreeting()).thenReturn(Observable.just(greeting));
Mockito.when(mDataModel.getGreetingAsync()).thenReturn(Observable.just(greeting));

mPresenter.bind();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package upday.mvpvsmvvm.mvvm;
package upday.patterns.mvvm;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -8,7 +8,7 @@

import rx.Observable;
import rx.observers.TestSubscriber;
import upday.mvpvsmvvm.datamodel.IDataModel;
import upday.patterns.datamodel.IDataModel;

public class ViewModelTest {

Expand All @@ -27,7 +27,7 @@ public void setUp() throws Exception {
@Test
public void testGetGreeting_emitsCorrectGreeting() {
String greeting = "Hello!";
Mockito.when(mDataModel.getGreeting()).thenReturn(Observable.just(greeting));
Mockito.when(mDataModel.getGreetingAsync()).thenReturn(Observable.just(greeting));
TestSubscriber<String> testSubscriber = new TestSubscriber<>();

mViewModel.getGreeting().subscribe(testSubscriber);
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0-rc1'
classpath 'com.android.tools.build:gradle:2.2.0-beta1'
classpath 'me.tatarka:gradle-retrolambda:3.2.3'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip