Comments (1)
Steps:
- Clone from branches Java & Java-template, or Kotlin & Kotlin-template (depending on your choice of language)
- Get to know the concept and folder architecture by looking up the Kotlin/Java branch
- Start working on the "x-template" branch to create your own app, don't be afraid to take a peek back at the sample app if you are stuck or don't know where certains things belong.
Typical workflow
I'll base further things on Kotlin, because I haven't used the Java branch - it's probably similar anyway.
For screens/fragments/list items etc, you create 3 files.
So let's take a User screen (Activity and Fragment is mostly interchangeable here) as an example that's the most common.
You need UserMvvm
, UserActivity
and UserViewModel
(the given order is significant, you can switch and create ViewModel before activity)
Start off by creating the interfaces in the UserMvvm file
This is just a "container" for two inner interfaces - just for quality of life sake to keep them "together"
interface UserMvvm
Now you need a View
and ViewModel
interface UserMvvm {
interface View: MvvmView {
//TODO methods/fields that end up in your Activity/Fragment/ViewHolder
}
interface ViewModel: MvvmViewModel<View> {
//TODO methods/fields that end up in your ViewModel
//and are available for use in the XML under `vm.`
}
}
Now you can create your XML layout for the activity
<layout>
<data>
<variable
name="vm"
type="{full namespace of UserMvvm.ViewModel interface}" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout>
.....
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
You can continue with your Activity (UserActivity
in this case)
class UserActivity :
// 1st generated databinding class for your XML, 2nd ViewModel interface you created before
BaseActivity<ActivityUserBinding, UserMvvm.ViewModel>(),
UserMvvm.View { //and this is your View interface
//TODO implement UserMvvm.View methods/fields if needed
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setAndBindContentView(savedInstanceState, R.layout.activity_user)
//TODO setup your action bar etc
//TODO update your viewmodel if necessary, for example if you received some args in your intent
}
}
Proceed with implementing the UserViewModel
@PerActivity
class UserViewModel
@Inject
constructor() : //inject stuff in the constructor that you need in the viewmodel
BaseViewModel<UserMvvm.View>(),
UserMvvm.ViewModel {
//TODO implement the interface
//(stuff like updating the viewmodel with initial data from navigation, etc)
//button logic, api calls, ...
//properties used in XML ... (they need to be defined in the interface!)
}
Last thing but VERY IMPORTANT
Go to injection > components > ActivityComponent
and inject your UserActivity
fun inject(activity: UserActivity)
Next is your viewmodel in injection > modules > ViewModelModule
@Binds
internal abstract fun bindUserViewModel(userViewModel: UserViewModel): UserMvvm.ViewModel
The specific Components
and Modules
depend on which View "type" (Activity, Fragment, ActivityViewHolder, FragmentViewHolder) you are implementing
Stuff for injecting into your viewmodels and views
Choose injection > modules > NetModule
for REST APIs, or other network related stuff...
injection > modules > DataModule
for data repositories...
I think by now you should get the basic idea of how it works, especially if you've worked with Dagger2 before.
Final words
I used this template in one of my project that I worked on for around half a year. It's great, especially if you need to write your app quickly, however keep in mind it was released before Android Jetpack has arrived, so it's not using some amazing things like LiveData, their ViewModel or Navigation (or even Room instead of Realm).
Especially injection can be done less painfully with libs like Koin.
Since you found this repo, you were probably looking for the specific libs that are used here, and there's nothing wrong with that. Just keep in mind that Android and Kotlin is still evolving ;)
Kudos to @patloew.
from countries.
Related Issues (12)
- Realm instances properly closed? HOT 6
- Why startActivity and replaceFragment remove bundle args HOT 1
- Shared Element Transition
- Build error
- Kotlin: Crash on resume / orientation change HOT 1
- Is it possible to inherit ViewHolderComponent from FragmentComponent? HOT 3
- ClassNotFoundException when unmarshalling: io.realm.NeedRealmProxy$$Parcelable #4459 HOT 1
- where did you close the realm ?? HOT 1
- Adding a third tab and going back and forth crashes the app HOT 3
- Cannot resolve symbol 'DaggerAppComponent' HOT 2
- Size of installed application is very large HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from countries.