Permalink
Browse files

Use MutableLiveData instead of ObservableField

  • Loading branch information...
petrnohejl committed Jun 1, 2018
1 parent 3c5d1ce commit 1164630f37f2a3fea14922aa9ba7cda51123cc6f
View
@@ -172,37 +172,37 @@ Finally let's create a ViewModel. The best thing about the ViewModel is that it
```java
public class HelloWorldViewModel extends BaseViewModel
{
public final ObservableField<Integer> state = new ObservableField<>();
public final ObservableField<MessageEntity> message = new ObservableField<>();
public final MutableLiveData<Integer> state = new MutableLiveData<>();
public final MutableLiveData<MessageEntity> message = new MutableLiveData<>();
public void loadData()
{
// show progress
state.set(StatefulLayout.PROGRESS);
state.setValue(StatefulLayout.PROGRESS);
// load data from data provider...
}
public void updateMessage(String text)
{
MessageEntity m = message.get();
MessageEntity m = message.getValue();
m.setText(text);
message.notifyChange();
message.setValue(m);
}
private void onLoadData(MessageEntity m)
{
// save data
message.set(m);
message.setValue(m);
// show content
if(message.get() != null)
if(message.getValue() != null)
{
state.set(StatefulLayout.CONTENT);
state.setValue(StatefulLayout.CONTENT);
}
else
{
state.set(StatefulLayout.EMPTY);
state.setValue(StatefulLayout.EMPTY);
}
}
}
@@ -241,7 +241,7 @@ public class HelloWorldViewModel extends BaseViewModel implements LifecycleObser
public void onStart()
{
// load data
if(message.get() == null) loadData();
if(message.getValue() == null) loadData();
}
}
```
@@ -43,6 +43,7 @@ private B setupBinding(@NonNull LayoutInflater inflater)
B binding = inflateBindingLayout(inflater);
binding.setVariable(BR.view, this);
binding.setVariable(BR.viewModel, getViewModel());
binding.setLifecycleOwner(this);
return binding;
}
}
@@ -36,6 +36,7 @@ private B setupBinding(@NonNull LayoutInflater inflater)
B binding = inflateBindingLayout(inflater);
binding.setVariable(BR.view, this);
binding.setVariable(BR.viewModel, getViewModel());
binding.setLifecycleOwner(this);
return binding;
}
}
View
@@ -15,3 +15,5 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.databinding.enableV2=true
@@ -1,9 +1,11 @@
package org.alfonz.samples.alfonzarch;
import android.annotation.SuppressLint;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.OnLifecycleEvent;
import android.databinding.ObservableField;
import android.os.AsyncTask;
import android.os.Bundle;
import org.alfonz.utility.NetworkUtility;
@@ -12,8 +14,8 @@
public class ArchSampleViewModel extends BaseViewModel implements LifecycleObserver
{
public final ObservableField<Integer> state = new ObservableField<>();
public final ObservableField<String> message = new ObservableField<>();
public final MutableLiveData<Integer> state = new MutableLiveData<>();
public final MutableLiveData<String> message = new MutableLiveData<>();
public ArchSampleViewModel(Bundle extras)
@@ -25,15 +27,15 @@ public ArchSampleViewModel(Bundle extras)
public void onStart()
{
// load data
if(message.get() == null) loadData();
if(message.getValue() == null) loadData();
}
public void updateMessage()
{
String s = message.get();
String s = message.getValue();
s += "o";
message.set(s);
message.setValue(s);
}
@@ -42,47 +44,55 @@ private void loadData()
if(NetworkUtility.isOnline(getApplicationContext()))
{
// show progress
state.set(StatefulLayout.PROGRESS);
state.setValue(StatefulLayout.PROGRESS);
// load data
new Thread(new Runnable()
@SuppressLint("StaticFieldLeak")
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>()
{
@Override
public void run()
protected String doInBackground(Void... voids)
{
try
{
Thread.sleep(2000L);
String s = "Hello";
onLoadData(s);
return "Hello";
}
catch(InterruptedException e)
{
e.printStackTrace();
return null;
}
}
}).start();
@Override
protected void onPostExecute(String s)
{
onLoadData(s);
}
}.execute();
}
else
{
state.set(StatefulLayout.OFFLINE);
state.setValue(StatefulLayout.OFFLINE);
}
}
private void onLoadData(String s)
{
// save data
message.set(s);
message.setValue(s);
// show content
if(message.get() != null)
if(message.getValue() != null)
{
state.set(StatefulLayout.CONTENT);
state.setValue(StatefulLayout.CONTENT);
}
else
{
state.set(StatefulLayout.EMPTY);
state.setValue(StatefulLayout.EMPTY);
}
}
}
@@ -1,6 +1,6 @@
package org.alfonz.samples.alfonzmedia;
import android.databinding.ObservableField;
import android.arch.lifecycle.MutableLiveData;
import android.graphics.Bitmap;
import org.alfonz.samples.alfonzarch.BaseViewModel;
@@ -10,13 +10,13 @@
public class MediaSampleViewModel extends BaseViewModel
{
public final ObservableField<Bitmap> bitmap = new ObservableField<>();
public final MutableLiveData<Bitmap> bitmap = new MutableLiveData<>();
public final PermissionManager permissionManager = new PermissionManager(new PermissionRationaleHandler());
public void updateBitmap(Bitmap bitmap)
{
this.bitmap.set(bitmap);
this.bitmap.setValue(bitmap);
}
}
@@ -2,8 +2,8 @@
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.OnLifecycleEvent;
import android.databinding.ObservableField;
import org.alfonz.rest.rx.RestRxManager;
import org.alfonz.rx.AlfonzDisposableSingleObserver;
@@ -22,8 +22,8 @@
public class RestSampleRxViewModel extends BaseViewModel implements LifecycleObserver
{
public final ObservableField<Integer> state = new ObservableField<>();
public final ObservableField<RepoEntity> repo = new ObservableField<>();
public final MutableLiveData<Integer> state = new MutableLiveData<>();
public final MutableLiveData<RepoEntity> repo = new MutableLiveData<>();
private RestRxManager mRestRxManager = new RestRxManager(new RestResponseHandler(), new RestHttpLogger());
@@ -32,7 +32,7 @@
public void onStart()
{
// load data
if(repo.get() == null) loadData();
if(repo.getValue() == null) loadData();
}
@@ -60,7 +60,7 @@ private void loadData()
if(!mRestRxManager.isRunning(callType))
{
// show progress
state.set(StatefulLayout.PROGRESS);
state.setValue(StatefulLayout.PROGRESS);
// subscribe
Single<Response<RepoEntity>> rawSingle = RepoRxServiceProvider.getService().repo("petrnohejl", "Alfonz");
@@ -71,7 +71,7 @@ private void loadData()
else
{
// show offline
state.set(StatefulLayout.OFFLINE);
state.setValue(StatefulLayout.OFFLINE);
}
}
@@ -81,7 +81,7 @@ private void loadData()
return AlfonzDisposableSingleObserver.newInstance(
response ->
{
repo.set(response.body());
repo.setValue(response.body());
setState(repo);
},
throwable ->
@@ -93,15 +93,15 @@ private void loadData()
}
private void setState(ObservableField<RepoEntity> data)
private void setState(MutableLiveData<RepoEntity> data)
{
if(data.get() != null)
if(data.getValue() != null)
{
state.set(StatefulLayout.CONTENT);
state.setValue(StatefulLayout.CONTENT);
}
else
{
state.set(StatefulLayout.EMPTY);
state.setValue(StatefulLayout.EMPTY);
}
}
}
@@ -2,8 +2,8 @@
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.OnLifecycleEvent;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import org.alfonz.rest.HttpException;
@@ -23,8 +23,8 @@
public class RestSampleViewModel extends BaseViewModel implements LifecycleObserver
{
public final ObservableField<Integer> state = new ObservableField<>();
public final ObservableField<RepoEntity> repo = new ObservableField<>();
public final MutableLiveData<Integer> state = new MutableLiveData<>();
public final MutableLiveData<RepoEntity> repo = new MutableLiveData<>();
private CallManager mCallManager = new CallManager(new RestResponseHandler(), new RestHttpLogger());
@@ -33,7 +33,7 @@
public void onStart()
{
// load data
if(repo.get() == null) loadData();
if(repo.getValue() == null) loadData();
}
@@ -61,7 +61,7 @@ private void loadData()
if(!mCallManager.hasRunningCall(callType))
{
// show progress
state.set(StatefulLayout.PROGRESS);
state.setValue(StatefulLayout.PROGRESS);
// enqueue call
Call<RepoEntity> call = RepoServiceProvider.getService().repo("petrnohejl", "Alfonz");
@@ -72,20 +72,20 @@ private void loadData()
else
{
// show offline
state.set(StatefulLayout.OFFLINE);
state.setValue(StatefulLayout.OFFLINE);
}
}
private void setState(ObservableField<RepoEntity> data)
private void setState(MutableLiveData<RepoEntity> data)
{
if(data.get() != null)
if(data.getValue() != null)
{
state.set(StatefulLayout.CONTENT);
state.setValue(StatefulLayout.CONTENT);
}
else
{
state.set(StatefulLayout.EMPTY);
state.setValue(StatefulLayout.EMPTY);
}
}
@@ -101,7 +101,7 @@ public RepoCallback(CallManager callManager)
@Override
public void onSuccess(@NonNull Call<RepoEntity> call, @NonNull Response<RepoEntity> response)
{
repo.set(response.body());
repo.setValue(response.body());
setState(repo);
}
@@ -1,6 +1,6 @@
package org.alfonz.samples.alfonzrx;
import android.databinding.ObservableField;
import android.arch.lifecycle.MutableLiveData;
import org.alfonz.rx.AlfonzDisposableObserver;
import org.alfonz.rx.RxBus;
@@ -29,15 +29,15 @@
private static final String LOG_MESSAGE_ON_COMPLETE = "on complete";
private static final String[] NAMES = {"Peter", "James", "John", "Robert", "Michael", "William", "David", "Richard", "Charles", "Joseph", "Jane", "Mary", "Patricia", "Linda", "Barbara", "Elizabeth", "Jennifer", "Maria", "Susan", "Margaret"};
public final ObservableField<String> log = new ObservableField<>();
public final MutableLiveData<String> log = new MutableLiveData<>();
private RxManager mRxManager = new RxManager();
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
public RxSampleViewModel()
{
log.set("");
log.setValue("");
listen();
}
@@ -108,7 +108,7 @@ private String getRandomName()
private void log(String message)
{
String line = String.format("[%s] %s", DateConvertor.dateToString(new Date(), "HH:mm:ss.SSS"), message);
log.set(line + "\n" + log.get());
log.setValue(line + "\n" + log.getValue());
}
Oops, something went wrong.

0 comments on commit 1164630

Please sign in to comment.