Moxy is a library that helps to use MVP pattern when you do the Android Application. Without problems of lifecycle and boilerplate code!
The main idea of using Moxy: See what's happening here in the wiki.
Moxy has a few killer features in other ways:
- Presenter stay alive when Activity recreated(it simplify work with multithreading)
- Automatically restore all that user see when Activity recreated(including dynamic content is added)
- Capability to changes of many Views from one Presenter
View interface
interface HelloWorldView: MvpView {
var waiting: Boolean
@MoxyViewCommand(ONE_EXECUTION)
fun showMessage(int message)
}
Presenter
class HelloWorldPresenter: MvpPresenter<HelloWorldView> {
init {
viewState.showMessage(R.string.hello_world)
}
}
View implementation
class HelloWorldActivity: MvpAppCompatActivity, HelloWorldView {
private val helloWorldPresenter: HelloWorldPresenter by providePresenter {
HelloWorldPresenter()
}
private lateinit var helloWorldTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hello_world)
helloWorldTextView = findViewById<TextView>(R.id.activity_hello_world_text_view_message)
}
@Override
override fun showMessage(message: Int) {
helloWorldTextView.setText(message)
}
}
Here you can see "Github" sample application.
For all information check Moxy Wiki
Add the JitPack repository to your build file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add dependency:
plugins {
id 'com.google.devtools.ksp' version '1.9.0-1.0.11' apply false
}
dependencies {
...
implementation 'com.github.Omega-R.OmegaMoxy:moxy:3.1.0'
implementation 'com.github.Omega-R.OmegaMoxy:moxy-androidx:3.1.0'
ksp 'com.github.Omega-R.OmegaMoxy:moxy-compiler:3.1.0'
}
Moxy is completely without reflection! No special ProGuard rules required.
The MIT License (MIT)
Copyright (c) 2023 Omega
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.