Android UI State.


License

License

GroupId

GroupId

com.github.sophoun
ArtifactId

ArtifactId

android-ui-state
Last Version

Last Version

0.0.4
Release Date

Release Date

Type

Type

aar
Description

Description

Android UI State.
Android UI State.
Project URL

Project URL

https://github.com/Sophoun/android-ui-state.git
Source Code Management

Source Code Management

https://github.com/Sophoun/android-ui-state

Download android-ui-state

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.sophoun/android-ui-state/ -->
<dependency>
    <groupId>com.github.sophoun</groupId>
    <artifactId>android-ui-state</artifactId>
    <version>0.0.4</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.sophoun/android-ui-state/
implementation 'com.github.sophoun:android-ui-state:0.0.4'
// https://jarcasting.com/artifacts/com.github.sophoun/android-ui-state/
implementation ("com.github.sophoun:android-ui-state:0.0.4")
'com.github.sophoun:android-ui-state:aar:0.0.4'
<dependency org="com.github.sophoun" name="android-ui-state" rev="0.0.4">
  <artifact name="android-ui-state" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.sophoun', module='android-ui-state', version='0.0.4')
)
libraryDependencies += "com.github.sophoun" % "android-ui-state" % "0.0.4"
[com.github.sophoun/android-ui-state "0.0.4"]

Dependencies

compile (5)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-android-extensions-runtime jar 1.3.72
androidx.core » core-ktx jar 1.3.2
androidx.fragment » fragment-ktx jar 1.2.5
org.jetbrains.kotlinx : kotlinx-coroutines-core jar 1.3.7
com.github.sophoun : android-utils jar 0.0.2

Project Modules

There are no modules declared in this project.

Android UI State

We create this library to easy update Activity or Fragment ui state. And also easy to send message between service and Activity or Fragment.

How to use

Import dependency

We host this library on jcenter. Make sure you have added jcenter() repository to your build.gradle root project.

allprojects {
    repositories {
        jcenter()
    }
}

Then import library dependency inside your app module:

implementation 'com.github.sophoun:android-ui-state:0.0.1'

Sample

In our library we force user to extend Activity, Fragment, ViewModel or Service class from our class that provided.

If you add Activity, Fragment or ViewModel you need to extend from class BaseActivity, BaseFragment or BaseViewModel and then set it up with ViewModel that also extended from BaseViewModel.

Example for Activity, Fragment and ViewModel

Activity:

class MyActivity : BaseActivity() {

    private val sampleViewModel by lazy { (application as MyApplication).viewModelFactory.create(SampleViewModel::class.java) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setupWithViewModel(sampleViewModel)
        
        myBtn.setOnClickListener {
            sampleViewModel.getString()
        }
    }
    
    override fun onStateChanged(state: UiState) {
        super.onStateChanged(state)
        // Here is the place where you
        // handle the state changed
        // of [MyActivityState] class
    }
}

Fragment:

class MyFragment : BaseFragment() {

    private val sampleViewModel by lazy { (activity?.application as MyApplication).viewModelFactory?.create(SampleViewModel::class.java)!! }
    override fun layout(): Int = R.layout.fragment_first

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        setupWithViewModel(sampleViewModel)

        btn_open_second.setOnClickListener {
            sampleViewModel.getStringForFragment()
        }
    }

    override fun onStateChanged(state: UiState) {
        super.onStateChanged(state)
        // Here is the place where you
        // handle the state changed
        // of [MyFragmentState] class
    }
}

Inside your ViewModel class, you just extend from BaseViewModel and then call setState(uiState) from wherever you want.

class SampleViewModel : BaseViewModel() {

    fun getString() {
        setState(MyActivityState("Hello world!"))
    }
    
    fun getStringForFragment() {
        setState(MyFragmentState("Hello Fragment!"))
    }
}

Inside onStateChanged it will be call based on ViewModel lifecycle and the state class type that you cast to.

Example for Service

TODO()

Documentation

Document references

Versions

Version
0.0.4
0.0.1