Skip to content

Commit 7efef43

Browse files
committed
mvp added and mvc updated
1 parent 9bee91c commit 7efef43

17 files changed

+370
-55
lines changed

mvc/example/LoginModel.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

mvc/example/TrackModel.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class TrackModel {
2+
3+
//some network provider
4+
private SharedPreferences sharedPref;
5+
private SharedPreferences.Editor editor;
6+
private Context context;
7+
8+
public TrackModel(Context context) {
9+
this.context = context;
10+
sharedPref = context.getPreferences(Context.MODE_PRIVATE);
11+
}
12+
13+
//could be some response message instead of String result
14+
public String search(String start, String destination) {
15+
//use some network library to login
16+
17+
//if response is ok
18+
saveHome(start);
19+
return "14:00 " + start " - " + destination + " 15:30";
20+
return true; //mock
21+
22+
//if response fail just return "EMPTY"
23+
}
24+
25+
public String getHomePlace() {
26+
return sharedPref.getString("home", "");
27+
}
28+
29+
public void saveHome(String start) {
30+
editor = sharedPref.edit();
31+
editor.putString("home", start);
32+
editor.commit();
33+
}
34+
}

mvc/example/ViewControllerActivity.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
public class ViewControllerActivity extends AppCompatActivity {
22

3-
private EditText loginEdit;
4-
private EditText passwordEdit;
5-
private Button loginButton;
3+
private EditText startEdit;
4+
private EditText destinationEdit;
5+
private Button searchButton;
66

7-
private LoginModel model;
7+
private TrackModel model;
88

99
@Override
1010
public void onCreate(Bundle savedInstanceState) {
1111
super.onCreate(savedInstanceState);
12-
model = new LoginModel(this);
12+
model = new TrackModel(this);
1313

1414
//in clear MVC inflating layout, listeners and views update
1515
//should be done in separeted View layer
@@ -21,20 +21,20 @@ public void onCreate(Bundle savedInstanceState) {
2121
//view layer
2222
private void initView() {
2323
setContentView(R.layout.viewcontroller_activity);
24-
loginEdit = findViewById(R.id.loginEdit);
25-
passwordEdit = findViewById(R.id.passwordEdit);
26-
loginButton = findViewById(R.id.loginButton);
24+
startEdit = findViewById(R.id.startEdit);
25+
destinationEdit = findViewById(R.id.destinationEdit);
26+
searchButton = findViewById(R.id.searchButton);
2727
}
2828

2929
//view layer set listener passed from controller
3030
private void setListeners() {
31-
loginButton.setOnClickListener(v -> { loginAction(); });
31+
searchButton.setOnClickListener(v -> { searchAction(); });
3232
}
3333

3434
//view layer
3535
private void setViews() {
36-
String login = model.getLastLogin();
37-
loginEdit.setText(login);
36+
String home = model.getHomePlace();
37+
startEdit.setText(home);
3838
}
3939

4040
//controller layer runs this method on the view layer
@@ -43,16 +43,17 @@ private void showError(String text) {
4343
}
4444

4545
//controller layer
46-
private void loginAction() {
46+
private void searchAction() {
4747
//controller get inputs from view
48-
String login = loginEdit.getText().toString();
49-
String password = passwordEdit.getText().toString();
50-
boolean success = model.login(login, password);
51-
if(success) {
52-
//go to another activity
48+
String login = startEdit.getText().toString();
49+
String destination = destinationEdit.getText().toString();
50+
String result = model.search(start, destination);
51+
if(result.equals("EMPTY")) {
52+
showError("Incorrect login or password");
53+
5354
}
5455
else {
55-
showError("Incorrect login or password");
56+
//show search result in another activity/popup or in list
5657
}
5758
}
5859

mvc/pattern/Controller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private void actionRequest() {
1313
//do proper action
1414
String input = view.getInput();
1515
try {
16-
boolean success = model.addTask(input);
16+
boolean success = model.addData(input);
1717
if(success)
1818
view.updateText();
1919
else

mvc/pattern/Model.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public Model() {
99
database = new Database();
1010
}
1111

12-
public boolean addTask(String input) {
12+
public boolean addData(String input) {
1313
boolean isSuccess = database.add("TASK", input);
1414
return success;
1515
}
1616

17-
public String getTasks() {
17+
public String getData() {
1818
String tasks = "";
1919
for(int i=0; i<database.size("TASK"); i++)
2020
tasks += database.get("TASK", i) + "\n";

mvc/pattern/View.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public String getInput() {
2828

2929
public void updateText() {
3030
//update tasks list in TextView
31-
textView.setText(model.getTasks());
31+
textView.setText(model.getData());
3232
//show response text in the textView
3333
}
3434
}

mvp/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# mvp
2+
3+
This is repository of http://androidcode.pl blog. It shows uses of MVP in Android. It is a part of Design Patterns - MVP post in the blog.

mvp/example/Contract.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
interface Contract {
2+
3+
interface View {
4+
5+
void navigateToHome();
6+
void showError();
7+
void showInvalidLogin(boolean valid);
8+
void showInvalidPassword(boolean valid);
9+
}
10+
11+
interface Presenter {
12+
13+
void initView();
14+
void loginButtonClicked(String login, String password);
15+
}
16+
}

mvp/example/Interactor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
interface Interactor {
2+
3+
void getSavedLogin();
4+
void login(String login, String password);
5+
6+
interface OnResult {
7+
8+
void onLoginSuccess();
9+
void onLoginFail();
10+
void onSavedLoginExists(String savedLogin);
11+
}
12+
}

mvp/example/InteractorImpl.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class InteractorImpl implements Interactor {
2+
3+
//some network provider
4+
private SharedPreferences sharedPref;
5+
private SharedPreferences.Editor editor;
6+
private Context context;
7+
8+
private OnResult listener;
9+
10+
public InteractorImpl(OnResult listener) {
11+
this.listener = listener;
12+
this.context = App.getContext();
13+
sharedPref = context.getPreferences(Context.MODE_PRIVATE);
14+
}
15+
16+
public void login(String login, String password) {
17+
//use some network library to login
18+
int code = Response.OK; //mock code response from network
19+
20+
if(code == Response.OK) {
21+
saveLogin(login);
22+
listener.onLoginSuccess();
23+
}
24+
else {
25+
listener.onLoginFail();
26+
}
27+
}
28+
29+
public void getSavedLogin() {
30+
String savedLogin = sharedPref.getString("login", "");
31+
if(!savedLogin.equals(""))
32+
listener.onSavedLoginExists(savedLogin);
33+
}
34+
35+
private void saveLogin(String login) {
36+
editor = sharedPref.edit();
37+
editor.putString("login", login);
38+
editor.commit();
39+
}
40+
}

0 commit comments

Comments
 (0)