Commit 89abcb9e authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add structure for MVP


Signed-off-by: Bruno Freitas Tissei's avatarBruno Freitas Tissei <bft15@inf.ufpr.br>
parent 33eb639a
......@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -25,5 +25,9 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
......@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.ufpr.c3sl.agendador.agendador">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......
......@@ -3,11 +3,21 @@ package br.ufpr.c3sl.agendador.agendador;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import br.ufpr.c3sl.agendador.agendador.presenters.LoginPresenter;
public class MainActivity extends AppCompatActivity {
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
presenter = new LoginPresenter();
} else {
presenter = PresenterManager.getInstance().restorePresenter(savedInstanceState);
}
setContentView(R.layout.activity_main);
}
}
package br.ufpr.c3sl.agendador.agendador;
import android.os.Bundle;
import android.support.test.espresso.core.deps.guava.cache.Cache;
import android.support.test.espresso.core.deps.guava.cache.CacheBuilder;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import br.ufpr.c3sl.agendador.agendador.presenters.BasePresenter;
/**
* Created by bft15 on 2/2/17.
*/
public class PresenterManager {
private static final String SIS_KEY_PRESENTER_ID = "presenter_id";
private static PresenterManager instance;
private final AtomicLong currentId;
private final Cache<Long, BasePresenter<?, ?>> presenters;
PresenterManager(long maxSize, long expirationValue, TimeUnit expirationUnit) {
currentId = new AtomicLong();
presenters = CacheBuilder.newBuilder()
.maximumSize(maxSize)
.expireAfterWrite(expirationValue, expirationUnit)
.build();
}
public static PresenterManager getInstance() {
if (instance == null) {
instance = new PresenterManager(10, 30, TimeUnit.SECONDS);
}
return instance;
}
public <P extends BasePresenter<?, ?>> P restorePresenter(Bundle savedInstanceState) {
Long presenterId = savedInstanceState.getLong(SIS_KEY_PRESENTER_ID);
P presenter = (P) presenters.getIfPresent(presenterId);
presenters.invalidate(presenterId);
return presenter;
}
public void savePresenter(BasePresenter<?, ?> presenter, Bundle outState) {
long presenterId = currentId.incrementAndGet();
presenters.put(presenterId, presenter);
outState.putLong(SIS_KEY_PRESENTER_ID, presenterId);
}
}
package br.ufpr.c3sl.agendador.agendador.models;
import com.google.gson.annotations.SerializedName;
/**
* Created by bft15 on 2/2/17.
*/
public class Account {
@SerializedName("cpf")
String mCpf;
@SerializedName("password")
String mPassword;
public Account(String cpf, String password) {
this.mCpf = cpf;
this.mPassword = password;
}
}
package br.ufpr.c3sl.agendador.agendador.presenters;
import android.support.annotation.NonNull;
import java.lang.ref.WeakReference;
/**
* Created by bft15 on 2/2/17.
*/
public abstract class BasePresenter<M, V> {
protected M model;
private WeakReference<V> view;
public void setModel(M model) {
resetState();
this.model = model;
if (setupDone()) {
updateView();
}
}
protected void resetState() {
}
public void bindView(@NonNull V view) {
this.view = new WeakReference<>(view);
if (setupDone()) {
updateView();
}
}
public void unbindView() {
this.view = null;
}
protected V view() {
if (view == null) {
return null;
} else {
return view.get();
}
}
protected abstract void updateView();
protected boolean setupDone() {
return view() != null && model != null;
}
}
\ No newline at end of file
package br.ufpr.c3sl.agendador.agendador.presenters;
import br.ufpr.c3sl.agendador.agendador.models.Account;
import br.ufpr.c3sl.agendador.agendador.views.LoginView;
/**
* Created by bft15 on 2/2/17.
*/
public class LoginPresenter extends BasePresenter<Account, LoginView> {
@Override
protected void updateView() {
}
}
package br.ufpr.c3sl.agendador.agendador.views;
/**
* Created by bft15 on 2/2/17.
*/
public interface LoginView {
void setLoginEnabled(boolean enabled);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment