ThirtyInch —a new MVP library for Android

living next to the Activity

flaky tests are worse than no tests

ThirtyInch — the MVP library

  • The core implementation is very lightweight giving you a TiPresenter with a TiView interface which can be attached or detached.
  • The TiPresenter can be described as an Object which lives during the whole lifetime of an Activity, even when the Activity gets recreated, moved into background and gets killed. When the Activity finishes the TiPresenter dies, too (onDestroy()).
  • The TiPresenter has four lifecycle events.
    onCreate(): called once for the initialization, the view is currently not attached
    onAttachView(view): the View was attached and is visible to the user
    onDetachView(): the View will be detached after this call and is not visible to the user anymore
    onDestroy(): called once when the Activity/Fragment is completely destroyed and will never return again. Stop all your work!
    onAttachView(view) and onDetachView() are mapped to onStart() and onStop(), callbacks for onResume/onPause are not supported because those lifecycle events should be handled in the View layer.
  • The TiPresenter is able to survive orientation changes allowing you to keep network requests running and hold your Model instead of serializing it in onSaveInstanceState.
  • The TiPresenter can be attached to Activities and Fragments
  • Configurable for your needs. You can pass a TiConfiguration into a TiPresenter removing features such as surviving a configuration change or the View method annotations which are enabled by default. When you remove everything you end up with nothing more than the Mosby library.
  • All Presenter lifecycle events will be called in the correct order and onCreate() and onDestroy() only once. This may sound relatively easy but naive MVP implementations can detach the View twice leading to crashes or unnecessary null checks.
  • No RxJava dependency. Some MVP libraries have a dependency to RxJava even if you’re not using it in your project. ThirtyInch has a separate Rx module you can add optionally allowing you to manage subscriptions and delay events until the View is attached. ThirtyInch also was RxJava driven and this separation was one of the main reasons why it hasn’t been published earlier.
  • View interface annotations for methods (configurable with TiConfiguraiton):
    @CallOnMainThread makes sure the View method will be automatically called on the main thread
    @DistinctUntilChanged doesn’t call the View method twice when already called with the same parameter(s)
    The annotations work by proxying the View.
  • Public API. The Rx module relies on the TiLifecycleObserver and the annotations use the BindViewInterceptor API. These and all other APIs are public and can be used by everyone making your TiPresenter even more powerful without forking the library.
  • You don’t have to extend TiActivity and increase the inheritance stack of your BaseActivity. You can use CompositeAndroid and add the TiActivityPlugin to your Activity by adding the plugin module to your dependencies.

Example

Not strict MVP, it also works with MVVM

Testing — Keep Android At Arm’s Length

via martworkshop.com

How does the Presenter survive the configuration change?

  1. A Fragment with setRetainInstance(true) will be restored in the new Activity instance. The same Fragment instance we had before with all properties and all references to long running tasks.
  2. Use Activity#onRetainNonConfigurationInstance() to save a random Object and get it in the new Activity instance with Activity#getLastNonConfigurationInstance(). That’s exactly how the Android Framework saves retained Fragments.
    This method was recently undeprecated. The deprecation before was in favor of retained Fragments which always used this deprecated method.

tl;dr:

Edit #1

--

--

--

Google Developer Expert for Flutter and passionate Android developer #kotlin working at @grandcentrix in Cologne

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
passsy

passsy

Google Developer Expert for Flutter and passionate Android developer #kotlin working at @grandcentrix in Cologne

More from Medium

Fastlane: Automate beta deployments and releases for your android apps

Android | Toggle Theme Mode With Lottie Animation

FontMetrics for Spanning Text

Shimmer effect in android