FromTo


Source link: https://github.com/importre/fromto

FromTo

FromTo is a helper library that makes it simple to handle asynchronous actions with views for Android and Java/Kotlin.

This library is written in Kotlin. But it's interoperable with Java.

Installation

Set to your build.gradle.

repositories {

  jcenter() 
}
  dependencies {

  compile 'io.github.importre:fromto:<version>' 
}

Getting Started

// make your job val job = Observable.just(1)

.subscribeOn(Schedulers.newThread())  // define an action and handlers you want val action1 = FtAction.Builder<Int>()

.from(job)

.to {
 println(it) 
}

.build()  val fromTo = FromTo.create(action1/*, more if you want */)  // attach `view`(FtView) related with fromTo's lifecycle fromTo.attach(view).execute()  // you can know `loading` state via `view`  // detach when `view` is destroyed fromTo.detach()

Basically FromTo has an FtAction or more.
(You can think that FtAction is a wrapper class of rx.Observable.)
Given some actions, FromTo can execute all actions asynchronously. And FromTo notifies corresponding view of the loading state via FtView interface.

  • Synchronous or asynchronous action
    • Actually it's up to observable's schedule as you know.
  • The state is true if one of actions is working.

See details in Kotlin example

Example

See examples.

Test

./gradlew test

Why should I use it?

FromTo is useful when developing an app, which has lifecycle by itself.
In Android world, for example, An Activity(or a Fragment) can take some heavy tasks such as Network, DB, File I/O and/or something.

Assume that you are in two conditions.

  • two tasks are working asynchronously,
  • you have to show ProgressBar if started, hide it if finished.

In these cases, how do you resolve this problem if your screen is rotated?
I know and you know some solutions.

  • Do nothing.
    • Easy but bad UX. Everything will be always reloaded.
  • Prevent recreating activity.
    • Using android:configChanges of < activity> element. It's very easy but the layout is not flexible(on tablet especially).
  • Use AsyncTask, Handler or whatever. And Store/Restore data
    • Inconvenient, verbose 😭
  • Set Fragment.setRetainInstance(true)
    • The fragment is not recreated/destroyed. So the fragment can store what you want although parent activity is recreated.
    • It's good. But some guys wouldn't like to use Fragment.
  • Use RxJava
    • Observable's cache() is a blessing. 🎉
    • See Lifecycle section of this post.

I thought that RxJava is the best solution because it's very simple and convenient.
But I realized that there are still boilerplate codes. So I implemented FromTo simply.

License

Apache 2.0 © Jaewe Heo

Resources

A small library to send an email in background without user interaction.

Android Library to help you with your runtime Permissions.

This is a lightweight Image Cropper for Android which also supports no-crop feature.

Recycling mechanism for pager adapter.

Take Android screenshots with Falcons bright eye!

Includes dialogs, toasts and all other extra windows in your screenshot.

Android Recycler View with Parallax Effect on each item, inspired by soundcloud android app.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes