-
-
Save cloudshooterhuman/ee8db3c3be4b3cc7e215a7e29c06b1b5 to your computer and use it in GitHub Desktop.
Clean Architecture
This file contains hidden or 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
//BasePresenter.java | |
public interface BasePresenter<V extends BaseView> { | |
/** | |
* Set or attach the view to this presenter | |
*/ | |
public void attachView(V view); | |
/** | |
* Will be called if the view has been destroyed. Typically this method will | |
* be invoked from <code>Activity.detachView()</code> or | |
* <code>Fragment.onDestroyView()</code> | |
*/ | |
public void detachView(boolean retainInstance); | |
} | |
//Presenter.java | |
public class Presenter<V extends BaseView> implements BasePresenter<V> { | |
private WeakReference<V> viewRef; | |
@Override | |
public void attachView(V view) { | |
viewRef = new WeakReference<>(view); | |
} | |
/** | |
* Get the attached view. You should always call {@link #isViewAttached()} | |
* to check if the view is attached to avoid NullPointerExceptions | |
*/ | |
protected V getView() { | |
if (isViewAttached()) | |
return viewRef.get(); | |
else | |
return null; | |
} | |
public void viewReady() { | |
} | |
public void resume() { | |
} | |
public void addObserver(String id) { | |
} | |
public void removeObserver() { | |
} | |
public void pause() { | |
} | |
/** | |
* Checks if a view is attached to this presenter. You should always call | |
* this method before calling {@link #getView()} to get the view instance. | |
*/ | |
protected boolean isViewAttached() { | |
return viewRef != null && viewRef.get() != null; | |
} | |
@Override | |
public void detachView(boolean retainInstance) { | |
if (viewRef != null) { | |
viewRef.clear(); | |
viewRef = null; | |
} | |
} | |
} | |
//BaseView.java | |
public interface BaseView { | |
/* | |
* Interaction listener to be implemented by the Activity | |
*/ | |
interface InteractionListener<T> { | |
void onItemClick(T item); | |
} | |
void findViews(View view); | |
void bindViews(); | |
} | |
//BaseFragment.java | |
/** | |
* An abstract base class called BaseFragment which caches the Booking Object to be used across the Checkin Flow | |
*/ | |
public abstract class BaseFragment extends Fragment{ | |
.. | |
} | |
//PresenterFragment.java | |
public abstract class PresenterFragment<P extends BasePresenter> extends BaseFragment implements BaseView { | |
/** | |
* The presenter for this view. Will be instantiated with | |
* {@link #createPresenter(Bundle)} | |
*/ | |
protected P presenter; | |
/** | |
* Creates a new presenter instance, if needed. Will reuse the previous | |
* presenter instance if {@link #setRetainInstance(boolean)} is set to true. | |
* This method will be called after from | |
* {@link #onViewCreated(View, Bundle)} | |
*/ | |
protected abstract P createPresenter(Bundle bundle); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Create the presenter if needed | |
if (presenter == null) { | |
presenter = createPresenter(getArguments()); | |
if (presenter == null) { | |
throw new NullPointerException("Presenter is null! Do you return null in createPresenter()?"); | |
} | |
} | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
presenter.attachView(this); | |
bindViews(); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
super.onCreateView(inflater, container, savedInstanceState); | |
int layoutRes = getLayoutRes(); | |
if (layoutRes == 0) { | |
throw new IllegalArgumentException("getLayoutRes() returned 0, which is not allowed. " | |
+ "If you don't want to use getLayoutRes() but implement your own view for this " | |
+ "fragment manually, then you have to override onCreateView();"); | |
} else { | |
View v = inflater.inflate(layoutRes, container, false); | |
findViews(v); | |
return v; | |
} | |
} | |
@Override | |
public void onDestroyView() { | |
super.onDestroyView(); | |
presenter.detachView(getRetainInstance()); | |
} | |
/** | |
* Return the layout resource like R.layout.my_layout | |
* | |
* @return the layout resource or null, if you don't want to have an UI | |
*/ | |
protected int getLayoutRes() { | |
return 0; | |
} | |
} | |
/** | |
* BaseAcitvity would be used for Login mechanism(social integration) and with Push Notification integration | |
*/ | |
public abstract class BaseActivity { | |
} | |
/** | |
*/ | |
public abstract class BaseUseCase<T>{ | |
ExecutionThread ex = Interactor.getExecutionThread(); | |
GenericCallback<T> genCallBack = new GenericCallback(); | |
protected void notifySuccess(final Object response){ | |
if(ex != null){ | |
ex.post(new Runnable(){ | |
@Override | |
public void run(){ | |
if(mCallBack != null){ | |
mCallBack.onSuccess(response); | |
} | |
} | |
}); | |
} | |
//UI thread call | |
else{ | |
if(mCallBack != null){ | |
mCallBack.onSuccess(response); | |
} | |
} | |
} | |
public interface GenericCallback<E>{ | |
void onSuccess(E obj); | |
void onFailure(); | |
} | |
/** | |
* A factory for all our usecases | |
*/ | |
public class UseCasefactory{ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment