项目作者: YvesCheung

项目描述 :
A low invasive state management on Android
高级语言: Kotlin
项目地址: git://github.com/YvesCheung/PageStatusTransformer.git
创建时间: 2020-04-17T09:14:41Z
项目社区:https://github.com/YvesCheung/PageStatusTransformer

开源协议:Apache License 2.0

下载


PageStatusTransformer

jitpack

A low invasive state management on Android

Preview

中文版

Feature

  • Declare your own state
  1. val transformer =
  2. PageStatusTransformer.newInstance {
  3. "Normal" {
  4. SimpleState(contentView)
  5. }
  6. "Loading" {
  7. MyLoadingPageState()
  8. }
  9. "Empty" {
  10. MyEmptyPageState()
  11. }
  12. //Can be any string or enum
  13. "Error" {
  14. MyErrorPageState()
  15. }
  16. }
  17. //transform to your state by its name
  18. transformer.transform("Empty")
  19. //or by enum
  20. transformer.transform(YourCustomEnum.State1)
  • Replace the origin View
    1. PageStatusTransformer.newInstance(
    2. replaceTo = contentView /*contentView would be replaced by the new ui which has the same size and parent */
    3. ) {
    4. State1 {...}
    5. State2 {...}
    6. }
    The view of the original page can be specified by the optional parameter replaceTo.
    When a transform occurs, the view in the new state will dynamically replace the view in the old state.

Therefore, there is no need to put a so-called <StatusLayout ></StatusLayout> in XML, and it is not necessary to
plug the business into BaseActivity or BaseFragment.

  • Different ways to toggle the visibility of View
    1. PageStatusTransformer.newInstance(...) {
    2. State1 {
    3. SimpleStatus(view)
    4. }
    5. State2 {
    6. ViewStubStatus(viewStub)
    7. }
    8. State3 {
    9. ReplacementViewStatus(view)
    10. }
    11. }

SimpleStatus supports only changing the visibility of the state UI, which is applicable to the state
that exists in the layout for a long time, such as the normal state.

ViewStubStatus which supports rendering only when used, is suitable for states in lazy loading layouts.

ReplacementViewStatus supports dynamic adding and removing state UI, which is suitable for temporary
state existing in layout.

  • Custom State

    1. PageStatusTransformer.newInstance(replaceTo = ...) {
    2. "State 1" {
    3. object: PageDisplayStatus {
    4. override fun showView(){
    5. //how to show state 1
    6. }
    7. override fun hideView(){
    8. //how to hide state 1
    9. }
    10. }
    11. }
    12. "State 2" {
    13. object: ReplacementViewStatus() {
    14. override fun inflateView(inflater: LayoutInflater, parent: ViewGroup): View {
    15. return inflater.inflate(...., parent, false)
    16. }
    17. override fun onViewShow(){...}
    18. override fun onViewHide(){...}
    19. }
    20. }
    21. ...
    22. }

Java support

Adapting Java language to Kotlin DSL:

  1. PageStatusTransformer transformer = PageStatusTransformer.newInstance(mRecyclerView,
  2. new JavaViewStatusBuilder()
  3. .putStatus("Normal", new SimpleStatus(mRecyclerView))
  4. .putStatus("Empty", new YouCustomStatus("No Data"))
  5. .putStatus("Error", new YouCustomStatus("Sorry! An error happen"))
  6. .putStatus("Loading"new YouCustomStatus("Loading"))
  7. .build()
  8. );
  9. transformer.transform("Empty");

Install

  1. repositories {
  2. ...
  3. maven { url 'https://jitpack.io' }
  4. }
  5. dependencies {
  6. implementation 'com.github.YvesCheung:PageStatusTransformer:x.y.z'
  7. }

version replace with jitpack