-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainPresenter.java
More file actions
161 lines (139 loc) · 4.16 KB
/
MainPresenter.java
File metadata and controls
161 lines (139 loc) · 4.16 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.tinmegali.mvp_tutorial;
import android.util.Log;
import com.tinmegali.mvp_tutorial.modelos.Note;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* ---------------------------------------------------
* Created by Tin Megali on 26/02/16.
* Project: MVP_Tutorial
* ---------------------------------------------------
* <a href="http://www.tinmegali.com">tinmegali.com</a>
* <a href="http://www.github.com/tinmegali>github</a>
*/
public class MainPresenter
implements MainMVP.RequiredPresenterOps, MainMVP.PresenterOps {
private String TAG = getClass().getSimpleName();
// Layer View reference
// Referência para layer View
private WeakReference<MainMVP.RequiredViewOps> mView;
// Layer Model reference
// Referência para o layer Model
private MainMVP.ModelOps mModel;
// Configuration change state
// Estado da mudança de configuração
private boolean mIsChangingConfig;
public MainPresenter(MainMVP.RequiredViewOps mView) {
Log.d(TAG, "MainPresenter()");
this.mView = new WeakReference<>(mView);
this.mModel = new MainModel(this);
}
/**
* Sent from Activity after a configuration changes
*
* Disparado por Activity após mudança de configuração
*
* @param view View reference
*/
@Override
public void onConfigurationChanged(MainMVP.RequiredViewOps view) {
Log.d(TAG, "onConfigurationChanged()");
mView = new WeakReference<>(view);
}
/**
* Receives {@link MainActivity#onDestroy()} event
* @param isChangingConfig Config change state
*
* Recebe evento {@link MainActivity#onDestroy()}
* @param isChangingConfig Se está mudando de config
*/
@Override
public void onDestroy(boolean isChangingConfig) {
Log.d(TAG, "onDestroy(isChangingConfig:"+isChangingConfig+")");
mView = null;
mIsChangingConfig = isChangingConfig;
if ( !isChangingConfig ) {
mModel.onDestroy();
}
}
/**
* Called by user interaction from {@link MainActivity}
* creates a new Note
*
* Chamado por {@link MainActivity} com a
* interação do usuário de pedido para inserção de
* nova nota
*/
@Override
public void newNote(String noteText) {
Log.d(TAG, "newNote()");
Note note = new Note();
note.setText(noteText);
note.setDate(getDate());
mModel.insertNote(note);
}
/**
* Called from {@link MainActivity},
* Removes a Note
*
* Chamado por {@link MainActivity}, pedido
* para remoção de nota
*/
@Override
public void deleteNote(Note note) {
Log.d(TAG, "deleteNote()");
mModel.removeNote(note);
}
/**
* Called from {@link MainModel}
* when a Note is inserted successfully
*
* Recebe chamado de {@link MainModel} quando
* Nota for inserida com sucesso no DB
*/
@Override
public void onNoteInserted(Note novaNote) {
Log.d(TAG, "onNoteInserted()");
mView.get().showToast("New " + novaNote.getDate());
}
/**
* Receives call from {@link MainModel}
* when Note is removed
*
* Recebe chamado de {@link MainModel} quando
* Nota for removida do DB
*/
@Override
public void onNoteRemoved(Note removedNote) {
Log.d(TAG, "onNoteRemoved()");
mView.get().showToast("Note " + removedNote.getDate() + " removed");
}
/**
* receive errors
*
* Recebe erros
*/
@Override
public void onError(String errorMsg) {
Log.d(TAG, "onError()");
mView.get().showAlert(errorMsg);
}
/**
* Returns current date
*
* Retorna data atual
*/
private String getDate(){
Log.d(TAG, "getDate()");
return new SimpleDateFormat("EEEE, dd/MM, kk:mm", Locale.getDefault()).format(new Date());
}
private Note getNote(String noteText){
Log.d(TAG, "getNote()");
Note note = new Note();
note.setText(noteText);
note.setDate( getDate() );
return note;
}
}