Last active
October 9, 2016 18:24
-
-
Save jmsalcido/12dafcb7f1fc4e70ca122395b12ba4f0 to your computer and use it in GitHub Desktop.
Android Architectura with MVP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------------------------- | |
// VIEW | |
// ---------------------------------------------------------- | |
// This class represents View layer and also Controller layer | |
// ---------------------------------------------------------- | |
public interface MainView { | |
void showMessage(String message); | |
void isLoadingEnabled(boolean value); | |
} | |
public class MainActivity extends AppCompatActivity implements MainView { | |
private Button loadButton; | |
private TextView console; | |
// Use your favorite DI framework to achieve this | |
@Inject | |
private MainPresenter mainPresenter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mainPresenter.attachView(this); | |
// show views | |
setContentView(R.layout.some_layout_activity); | |
loadButton = (Button) findViewById(R.id.load_button); | |
console = (TextView) findViewById(R.id.console_view); | |
// delegate everything to the controller layer! | |
loadButton.setOnClickListener(view -> { | |
mainPresenter.clickOnLoad(); | |
}); | |
} | |
@Override | |
public void isLoadingEnabled(boolean value) { | |
runOnUiThread(() -> loadButton.setEnabled(value)); | |
} | |
@Override | |
public void showMessage(String message) { | |
runOnUiThread(() -> console.setText(message)); | |
} | |
} | |
// ---------------------------------------------------------- | |
// Presenter | |
// ---------------------------------------------------------- | |
public interface MainPresenter { | |
void attachView(MainView view); | |
void clickOnLoad(); | |
} | |
public class MainPresenterImpl implements MainPresenter { | |
private final CloudApi cloudApi; | |
private MainView mainView; | |
public MainController(CloudApi cloudApi) { | |
this.cloudApi = cloudApi; | |
} | |
@Override | |
public void attachView(MainView view){ | |
mainView = view; | |
} | |
@Override | |
public void clickOnLoad() { | |
mainView.isLoadingEnabled(false); | |
doOnBackgroundThread(() -> { | |
// prepare cloudApi for something, prepare everything that model might need to work | |
try { | |
int downloadedData = cloudApi.downloadSomething(); | |
mainView.isLoadingEnabled(true); | |
} catch(InterruptedException e) { | |
mainView.showMessage(e.getMessage()); | |
return; | |
} | |
mainView.showMessage("DOWNLOADED: " + downloadedData + ", Click again!"); | |
}); | |
} | |
} | |
// ---------------------------------------------------------- | |
// MODEL | |
// ---------------------------------------------------------- | |
// Classes that represents the Model layer | |
// ---------------------------------------------------------- | |
public interface CloudApi { | |
int downloadSomething() throws InterruptedException; | |
} | |
public class CloudApiImpl { | |
@Override | |
public int downloadSomething() throws Exception { | |
Thread.sleep(5 * 1000); // simulate downloading something | |
return 5; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment