-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainActivity.java
129 lines (108 loc) · 4.19 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.tinmegali.mvp_tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements MainMVP.RequiredViewOps {
protected final String TAG = getClass().getSimpleName();
// Responsible to maintain the Objects state
// during changing configuration
// Responsável por manter estado dos objetos inscritos
// durante mudanças de configuração
private final StateMaintainer mStateMaintainer =
new StateMaintainer( this.getFragmentManager(), TAG );
// Presenter operations
// Operações no Presenter
private MainMVP.PresenterOps mPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startMVPOps();
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final EditText notaEditText = (EditText) findViewById(R.id.notaEditText);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPresenter.newNote(notaEditText.getText().toString());
}
});
}
/**
Initialize and restart the Presenter.
* This method should be called after {@link Activity#onCreate(Bundle)}
*
* Inicia e reinicia o Presenter. Este método precisa ser chamado
* após {@link Activity#onCreate(Bundle)}
*/
public void startMVPOps() {
Log.d(TAG, "startMVPOps()");
try {
if ( mStateMaintainer.firstTimeIn() ) {
Log.d(TAG, "onCreate() called for the first time");
initialize(this);
} else {
Log.d(TAG, "onCreate() called more than once");
reinitialize(this);
}
} catch ( InstantiationException | IllegalAccessException e ) {
Log.d(TAG, "onCreate() " + e );
throw new RuntimeException( e );
}
}
/**
* Initialize relevant MVP Objects.
* Creates a Presenter instance, saves the presenter in {@link StateMaintainer}
*
* Inicializa os objetos relevantes para o MVP.
* Cria uma instância do Presenter, salva o presenter
* no {@link StateMaintainer} e informa à instância do
* presenter que objeto foi criado.
* @param view Operações no View exigidas pelo Presenter
*/
private void initialize( MainMVP.RequiredViewOps view )
throws InstantiationException, IllegalAccessException{
Log.d(TAG, "initialize");
mPresenter = new MainPresenter(view);
mStateMaintainer.put(MainMVP.PresenterOps.class.getSimpleName(), mPresenter);
}
/**
* Recovers Presenter and informs Presenter that occurred a config change.
* If Presenter has been lost, recreates a instance
*
* Recupera o presenter e informa à instância que houve uma mudança
* de configuração no View.
* Caso o presenter tenha sido perdido, uma nova instância é criada
*/
private void reinitialize( MainMVP.RequiredViewOps view)
throws InstantiationException, IllegalAccessException {
mPresenter = mStateMaintainer.get( MainMVP.PresenterOps.class.getSimpleName() );
if ( mPresenter == null ) {
Log.w(TAG, "recreating Presenter");
initialize( view );
} else {
mPresenter.onConfigurationChanged(view);
}
}
// Show AlertDialog
// Exibe AlertDialog
@Override
public void showAlert(String msg) {
// show alert Box
Log.d(TAG, "showAlert()");
}
// Show Toast
@Override
public void showToast(String msg) {
Log.d(TAG, "showToast()");
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}