README/中文文档
You can create a new Kotgo template project fast by using the following command. Just paste and execute it at a terminal prompt. Have fun!
python -c "$(curl -fsSL https://raw.githubusercontent.com/nekocode/kotgo/master/project_creator.py)"
Of course, you can also download the python script to your local disk to run it. It depends on the requests
lib.
Kotgo is an android development framework using MVP architecture. It is built with pure Kotlin.
com.nekocode.baseframework
├─ data
│ ├─ dto
│ ├─ exception
│ ├─ model
│ └─ service
│
├─ ui
│ └─ screen_one
│ ├─ Presenter.kt
│ └─ Activity.kt
│
└─ App.kt
- Data Layer: It likes the traditional Model Layer, includes
dto
(Data Transfer Object),service
,model
,exception
or other element directories. It uses the dtos or other meta object to interact with the Presenter Layer. - View Layer: Including
activity
,adapter
,fragment
,view
. Only concerned with the user interaction, as well as view manipulation (animation, interface input, output and update, etc.). - Presenter Layer: Control logic layer. Contains the logic to respond to the events, updates the model (both the business logic and the application data), and alters the state of the view.
- kotlin version:
1.0.2
- anko:
0.8.3
- rxkotlin:
0.55.0
- retrofit:
2.0.2
- picasso:
2.5.2
- hawk:
1.23
- stetho:
1.3.1
- paperparcel:
1.0.0-rc4
Thanks to gank.io. The sample App fetchs beautiful girl photos' information from it.
Another better sample: Check this
You can only use the kotgo's component library with gradle. It provides many useful tools to help you to build a MVP project fast and simply. Just add the JitPack repository to your root build.gradle:
repositories {
maven { url "https://jitpack.io" }
}
And then add the dependency to your sub build.gradle:
dependencies {
compile 'com.github.nekocode:kotgo:<lastest-version>'
}
It helps you to bind the RxJava subscriptions into the lifecycle of Activity and Fragment. It will terminate the RxJava subscription when the activity or fragment is destorying or detaching. And the most importent thing is that it can be used anywhere (such in Prensenter), It's more flexible then the RxLifecycle.
MeiziModel.getMeizis(50, 1).onUI().bindLifecycle(view).subscribe {
view.refreshMeizis(it)
}
It simulates the Event Bus by RxJava. It uses many syntax sugar of Kotlin to make subscribing the Bus's events easier. And it is also binded into the lifecyle of Activity and Fragment automatically, you can just subscribe events in the bus
block without worrying about any accidents!
bus {
subscribe(String::class.java) {
showToast(it)
}
subscribe(Int::class.java) {
showToast(it.toString())
}
}
Using the Fragment to implement Presenter maybe is one of the best ways! Now we can move the logic which depended on Activity's lifecycle to Presenter. And we can also use setRetainInstance(true)
to solve the screen rotation problem.
It look like this:
class MeiziPresenter(): BasePresenter(), Contract.Presenter {
var view: Contract.View? = null
override fun onAttach(activity: Activity?) {
super.onAttach(activity)
view = getParent() as Contract.View
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MeiziModel.getMeizis(50, 1).onUI().bindLifecycle(this).subscribe {
view?.refreshMeizis(it)
}
}
}
Look at these code.
We can inherit the Dependency class for storing some denpendencies providing.
object TestDep : Dependency() {
override fun createDependencies() {
bindSingleton<Int> {
args ->
args[0] as Int
}
}
}
And then use the following code to inject dependency.
val int = TestDep.inject<Int>
You can build applications that have only one activity with the help of the FragmentActivity
provided by the Component Library.
It provides following functions for helping you manage the fragment stack.
push(fragmentTag, classType, args)
pushForResult(originalTag, requestCode, fragmentTag, classType, args)
pop()
get(fragmentTag)
getFragmentTopInStack()
startActivityForResult(fragmentTag, intent, requestCode, options)
We also deal with more details, look at the FragmentActivity.kt
.
It also contains some other useful tools and extensions (such as KotterKnife). Check the util package for more details.