Base MVVM implementation for android inspired by kickstarter.
This library uses the architecture component internally. Learn more: https://developer.android.com/topic/libraries/architecture/viewmodel.html
// in your root gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.IVIanuu:SimpleMVVM:LATEST-VERSION'
}
This example uses a activity but it works the same for fragments(Just replace Activity with Fragment). First create a view model it needs to extend SimpleMVVMActivityViewModel.
public class LoginActivityViewModel extends MVVMActivityViewModel {
public LoginActivityViewModel() {
}
public void login() {
// todo implement login
}
}
Then create your activity class. The activity needs to extend SimpleMVVMActivity and must be annotated with a @RequiresActivityViewModel(MyViewModel.class) annotation.
@RequiresActivityViewModel(LoginActivityViewModel.class)
public class LoginActivity extends MVVMActivity<LoginActivityViewModel> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// use your view model here
viewModel.login();
}
...
}
By default the library will use the default ViewModelProvider.Factory. If you want to use your own (For example to inject dependencies in your view model via dagger) you have to override the provideViewModelFactory method like this.
@RequiresActivityViewModel(LoginActivityViewModel.class)
public class LoginActivity extends MVVMActivity<LoginActivityViewModel> {
...
@Override
protected ViewModelProvider.Factory provideViewModelFactory() {
return mySuperCoolViewModelFactory;
}
}
The library is meant to be used with rxjava and for convenience it includes RxLifecycle from trello so you can easily bind your observables to the lifecycle of your activities, fragments or view models. This should look like the following.
@RequiresActivityViewModel(DetailActivityViewModel.class)
public class DetailActivity extends MVVMActivity<DetailActivityViewModel> {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
viewModel.loadData()
.compose(bindToLifecycle()) // will complete the observable in onDestroy
.compose(bindUntilEvent(Lifecycle.Event.ON_PAUSE)) // will complete the observable in onPause
.subscribe()
}
}
In view models the observable completes in onCleared which will be called when your activity or fragment will be destroyed.
public class DetailActivityViewModel extends MVVMActivityViewModel {
private final MyCoolRepository repo;
@Inject
public DetailActivityViewModel(@NonNull MyCoolRepository repo) {
this.repo = repo;
repo.loadDetails()
.compose(bindToLifecycle()) // will complete the observable onCleared
.subscribe(data -> {
// do something cool with your data
});
}
}
You can observe new intents and activity results in your activity view models.
public class CommentActivityViewModel extends MVVMActivityViewModel {
private static final int REQUEST_CODE = 1234;
public CommentActivityViewModel() {
intent()
.compose(bindToLifecycle())
.subscribe(intent -> {
// do something with the intent
});
activityResult()
.compose(bindToLifecycle())
.filter(activityresult -> activityresult.isRequestCode(REQUEST_CODE))
.subscribe(activityResult -> {
// do something with the activity result
});
}
}
In fragment view models you can observe arguments.
public class ProfileFragmentViewModel extends MVVMFragmentViewModel {
private static final int REQUEST_CODE = 1234;
public ProfileFragmentViewModel() {
arguments()
.compose(bindToLifecycle())
.subscribe(bundle -> {
// do something with the bundle
});
}
}
Copyright 2017 Manuel Wrage
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.