Last active
November 29, 2024 12:06
-
-
Save hector6872/66ea3fed040ab0c75e11c1a45b8c2360 to your computer and use it in GitHub Desktop.
DRMVPFragmentV4 example for DaRealMVP library - https://github.com/hector6872/DaRealMVP
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
@SuppressWarnings("unchecked") public abstract class DRMVPFragmentV4<P extends DRMVPPresenter<V>, V extends DRMVPView> extends Fragment { | |
private P presenter; | |
@CallSuper @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
return inflater.inflate(getLayoutResId(), container, false); | |
} | |
@CallSuper @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
presenter = MVPUtils.getDeclaredPresenter(getClass()); | |
presenter.bind((V) this); | |
} | |
@CallSuper @Override public void onDestroyView() { | |
super.onDestroyView(); | |
presenter.unbind(); | |
} | |
protected @NonNull P getPresenter() { | |
return presenter; | |
} | |
protected abstract @LayoutRes int getLayoutResId(); | |
} |
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
public class DRMVPUtils { | |
@SuppressWarnings("unchecked") public static <P> P getDeclaredPresenter(@NonNull Class clazz) { | |
Type genericSuperclass; | |
for (; ; ) { | |
genericSuperclass = clazz.getGenericSuperclass(); | |
if (genericSuperclass instanceof ParameterizedType) { | |
break; | |
} | |
clazz = clazz.getSuperclass(); | |
} | |
Type presenterClass = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0]; | |
try { | |
return (P) Class.forName(presenterClass.toString().split(" ")[1]).newInstance(); | |
} catch (java.lang.InstantiationException | IllegalAccessException | ClassNotFoundException e) { | |
throw new IllegalArgumentException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment