π¨βπ»
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
// First, we define a structure to store contact data we want to retrieve | |
data class ContactData( | |
val contactId: Long, | |
val name: String, | |
val phoneNumber: List<String>, | |
val avatar: Uri? | |
) | |
// Here are the functions to retrieve all contacts matching the search pattern | |
fun Context.retrieveAllContacts( |
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
/** | |
* Logs lifecycle events and provides subclasses a method to bind the views, bindViews(). | |
* | |
* Note that the bindViews() uses Butterknife to bind the views. However, the views | |
* can also be bound without using Butterknife. Using Butterknife or not | |
* plays no part in this demonstration. | |
*/ | |
// BaseFragment.java | |
public abstract class BaseFragment extends Fragment { |
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 Example1View extends MVPView { | |
void showSomething(String something); | |
} |
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
@Module | |
public abstract class Example1PresenterModule { | |
@Binds | |
@PerFragment | |
abstract Example1Presenter example1Presenter(Example1PresenterImpl example1PresenterImpl); | |
} |
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
@PerFragment | |
final class Example1PresenterImpl extends BasePresenter<Example1View> implements Example1Presenter { | |
private final SingletonUtil singletonUtil; | |
private final PerActivityUtil perActivityUtil; | |
private final PerFragmentUtil perFragmentUtil; | |
@Inject | |
Example1PresenterImpl(Example1View view, SingletonUtil singletonUtil, | |
PerActivityUtil perActivityUtil, PerFragmentUtil perFragmentUtil) { |
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 Example1Presenter extends Presenter { | |
void onDoSomething(); | |
} |
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 abstract class BaseViewFragment<T extends Presenter> extends BaseFragment | |
implements MVPView { | |
@Inject | |
protected T presenter; | |
@Override | |
public void onViewStateRestored(Bundle savedInstanceState) { | |
super.onViewStateRestored(savedInstanceState); | |
// Only start the presenter when the views have been bound. |
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 abstract class BasePresenter<T extends MVPView> implements Presenter { | |
protected final T view; | |
protected BasePresenter(T view) { | |
this.view = view; | |
} | |
@Override | |
public void onStart(@Nullable Bundle savedInstanceState) { |
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 Presenter { | |
/** | |
* Starts the presentation. This should be called in the view's (Activity or Fragment) | |
* onCreate() or onViewStatedRestored() method respectively. | |
* | |
* @param savedInstanceState the saved instance state that contains state saved in | |
* {@link #onSaveInstanceState(Bundle)} | |
*/ | |
void onStart(@Nullable Bundle savedInstanceState); |