Created
April 6, 2018 20:38
-
-
Save ahmed3elshaer/d85b5526882a5e3a700e028b2970af05 to your computer and use it in GitHub Desktop.
This snippet of code is extracted for Google's sample of MPV for Android.
https://github.com/googlesamples/android-architecture/tree/todo-mvp/ - At the beginning of the project we create the "BasePresenter" interface that every Presenter in our proj
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
public interface BasePresenter { | |
void start(); | |
} |
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
public interface BaseView<T> { | |
void setPresenter(T presenter); | |
} |
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
public class TasksActivity extends AppCompatActivity { | |
private TasksPresenter mTasksPresenter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.tasks_act); | |
TasksFragment tasksFragment = | |
(TasksFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame); | |
if (tasksFragment == null) { | |
// Create the fragment | |
tasksFragment = TasksFragment.newInstance(); | |
ActivityUtils.addFragmentToActivity( | |
getSupportFragmentManager(), tasksFragment, R.id.contentFrame); | |
} | |
// Create the presenter and attach it to our View (Fragment) | |
mTasksPresenter = new TasksPresenter( | |
Injection.provideTasksRepository(getApplicationContext()), tasksFragment); | |
} | |
} |
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
public interface TasksContract { | |
interface Presenter extends BasePresenter { | |
//the presenter functions that will be triggered from the view | |
//eg. Bussines logic or fetching data from the backend | |
//based on the user actions or a View Lifecycle. | |
void result(int requestCode, int resultCode); | |
void loadTasks(boolean forceUpdate); | |
void addNewTask(); | |
void openTaskDetails(@NonNull Task requestedTask); | |
void completeTask(@NonNull Task completedTask); | |
void activateTask(@NonNull Task activeTask); | |
void clearCompletedTasks(); | |
void setFiltering(TasksFilterType requestType); | |
TasksFilterType getFiltering(); | |
} | |
//this presenter between <Presenter> is what we just declared in the above interface | |
interface View extends BaseView<Presenter> { | |
//triggered functions from the presenter to update the UI | |
void setLoadingIndicator(boolean active); | |
void showTasks(List<Task> tasks); | |
void showAddTask(); | |
void showTaskDetailsUi(String taskId); | |
void showTaskMarkedComplete(); | |
void showTaskMarkedActive(); | |
void showCompletedTasksCleared(); | |
void showLoadingTasksError(); | |
void showNoTasks(); | |
void showActiveFilterLabel(); | |
void showCompletedFilterLabel(); | |
void showAllFilterLabel(); | |
void showNoActiveTasks(); | |
void showNoCompletedTasks(); | |
void showSuccessfullySavedMessage(); | |
boolean isActive(); | |
void showFilteringPopUpMenu(); | |
} | |
} |
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
public class TasksFragment extends Fragment implements TasksContract.View { | |
// our refrence of the presenter | |
private TasksContract.Presenter mPresenter; | |
//triggering a presenter function based on the fragment lifecycle | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mPresenter.start(); | |
} | |
//initializing the presenter from the method we impelmented from the Contract.View after the Presenter | |
//class triggered this function | |
@Override | |
public void setPresenter(@NonNull TasksContract.Presenter presenter) { | |
mPresenter = checkNotNull(presenter); | |
} | |
//at anytime we could trigger any function of the presenter class using it's instance eg. | |
mPresenter.completeTask(completedTask); | |
} |
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
public class TasksPresenter implements TasksContract.Presenter { | |
//refrence for the repository that is resposible for fetching the model data from any datasource | |
private final TasksRepository mTasksRepository; | |
//refrence of the View to trigger the functions after proccessing the task | |
private final TasksContract.View mTasksView; | |
// Presenter's Constructor the intialize the view attached to it | |
//and the repository and setting the view to this Presenter | |
public TasksPresenter(@NonNull TasksRepository tasksRepository, @NonNull TasksContract.View tasksView) { | |
mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null"); | |
mTasksView = checkNotNull(tasksView, "tasksView cannot be null!"); | |
mTasksView.setPresenter(this); | |
} | |
//triggring a method based on the view lifecycle | |
@Override | |
public void start() { | |
loadTasks(false); | |
} | |
//anytime we want to trigger a function inside our view we use our view refrence eg. | |
mTasksView.showSuccessfullySavedMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment