More Modern Presenter.
A more lightweight and simpler to use MVVM architecture for Android, Data and views are two-way bound.
Only 300 lines of code.
Currently implemented using reflection, it can be optimized to automatically generate code using Gradle Plugin.
I have developed the world's fastest general purpose sorting algorithm, which is on average 3 times faster than Quicksort and up to 20 times faster, ChenSort
Presenter
packagecn.flutterfirst.mmp_architecture.mainimportandroid.view.Viewimportcn.flutterfirst.mmp_architecture.Stateimportcn.flutterfirst.mmp_architecture.ViewClickimportcn.flutterfirst.mmp_architecture.mmp.BaseMMPActivityabstractclassMainPresenter : BaseMMPActivity() {
@State
protectedvar editValue:String?=null
get() {
if (field ==null|| field!!.isEmpty()) {
return""
}
return field
}
@State
protectedvar count =0privateval countMax =5privateval countMin =0
@State
protectedvar hintVisibility:Int?=null
get() {
if (count == countMax || count == countMin) {
returnView.VISIBLE
}
returnView.GONE
}
@State
protectedvar hintText:String?=null
get() {
returnif (count == countMax) {
"Can't add any more"
} else {
"Can't sub any more"
}
}
@State
protectedvar addEnable:Boolean?=null
get() {
return count != countMax
}
@State
protectedvar subEnable:Boolean?=null
get() {
return count != countMin
}
@ViewClick
protectedfunadd() {
if (count == countMax) {
return
}
setState {
count++
}
}
@ViewClick
protectedfunsub() {
if (count == countMin) {
return
}
setState {
count--
}
}
}View
packagecn.flutterfirst.mmp_architecture.mainimportcn.flutterfirst.mmp_architecture.ContentViewimportcn.flutterfirst.mmp_architecture.Rimportcn.flutterfirst.mmp_architecture.StateChanged
@ContentView(R.layout.activity_main)
classMainActivity : MainPresenter() {
overridefuninitViewsBeforeFirstSetState() {
}
@StateChanged("count")
funcountChanged() {
println("count changed, new value = $count")
}
@StateChanged("editValue")
funeditValueChanged() {
println("editValue changed, new value = $editValue")
}
}Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".main.MainActivity">
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:textSize="20sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:vText="Your input: {{editValue}}" />
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:text="One-way bound EditText"android:textSize="20sp"app:layout_constraintBottom_toTopOf="@+id/one_way_bind_edit"app:layout_constraintLeft_toLeftOf="@+id/one_way_bind_edit" />
<EditTextandroid:id="@+id/one_way_bind_edit"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginHorizontal="50dp"android:layout_marginTop="150dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:vEdit="{{editValue}}" />
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:text="Two-way bound EditText"android:textSize="20sp"app:layout_constraintBottom_toTopOf="@+id/two_way_bind_edit"app:layout_constraintLeft_toLeftOf="@+id/two_way_bind_edit" />
<EditTextandroid:id="@+id/two_way_bind_edit"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginHorizontal="50dp"android:layout_marginTop="250dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:vEditText="{{editValue}}" />
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="50sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:vText="{{count}}" />
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="180dp"android:textSize="20sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:vText="{{hintText}}"app:vVisible="{{hintVisibility}}" />
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="100dp"android:text="Add"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:vClick="{{add}}"app:vEnable="{{addEnable}}" />
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="50dp"android:text="Sub"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:vClick="{{sub}}"app:vEnable="{{subEnable}}" />
</androidx.constraintlayout.widget.ConstraintLayout>MIT License
Copyright (c) 2022 hackware1993
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.
