Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Implementation for Enum in BoltLiveData.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikeelde committed Jan 3, 2018
1 parent 7bfac41 commit 2b9340f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.example.wrench;
package com.example.wrench.BoltLiveData;

import android.arch.lifecycle.LiveData;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ProviderInfo;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
Expand All @@ -12,31 +13,33 @@
import com.izettle.wrench.core.Bolt;
import com.izettle.wrench.core.WrenchProviderContract;

public class BoltLiveData extends LiveData<Bolt> {
public abstract class BoltLiveData extends LiveData<Bolt> {
private final String key;
private final String type;
private final String defValue;
private final Context context;
private BoltContentObserver boltContentObserver;

BoltLiveData(Context context, String key, String defValue, @Bolt.BoltType String type) {
BoltLiveData(Context context, String key, @Bolt.BoltType String type) {
this.context = context;
this.key = key;
this.type = type;
this.defValue = defValue;
boltContentObserver = new BoltContentObserver(new Handler());
}

public static BoltLiveData string(Context context, String key, String def) {
return new BoltLiveData(context, key, def, Bolt.TYPE.STRING);
public static BoltLiveData create(Context context, String key, String def) {
return new StringBoltLiveData(context, key, def, Bolt.TYPE.STRING);
}

public static BoltLiveData bool(Context context, String key, boolean def) {
return new BoltLiveData(context, key, String.valueOf(def), Bolt.TYPE.BOOLEAN);
public static BoltLiveData create(Context context, String key, boolean def) {
return new StringBoltLiveData(context, key, String.valueOf(def), Bolt.TYPE.BOOLEAN);
}

public static BoltLiveData integer(Context context, String key, int def) {
return new BoltLiveData(context, key, String.valueOf(def), Bolt.TYPE.INTEGER);
public static BoltLiveData create(Context context, String key, int def) {
return new StringBoltLiveData(context, key, String.valueOf(def), Bolt.TYPE.INTEGER);
}

public static <T extends Enum> BoltLiveData create(Context context, String key, Class<T> enumClass, T def) {
return new EnumBoltLiveData<>(context, key, enumClass, def);
}

@Nullable
Expand Down Expand Up @@ -65,39 +68,40 @@ private static Bolt getBolt(ContentResolver contentResolver, String key) {
return new Bolt();
}

private static Uri insertBolt(ContentResolver contentResolver, Bolt bolt) {
return contentResolver.insert(WrenchProviderContract.boltUri(), bolt.toContentValues());
String getType() {
return type;
}

Context getContext() {
return context;
}

String getKey() {
return key;
}

@Override
protected void onActive() {
context.getContentResolver()
.registerContentObserver(WrenchProviderContract.boltUri(), true, boltContentObserver);
ProviderInfo providerInfo = context.getPackageManager().resolveContentProvider(WrenchProviderContract.WRENCH_AUTHORITY, 0);
if (providerInfo != null) {
context.getContentResolver()
.registerContentObserver(WrenchProviderContract.boltUri(), true, boltContentObserver);

boltChanged();
}
onChange();
}

@Override
protected void onInactive() {
context.getContentResolver().unregisterContentObserver(boltContentObserver);
}

private void boltChanged() {
Bolt bolt = getBolt(context.getContentResolver(), key);

if (bolt == null) {
setValue(null);
return;
}

if (bolt.getId() == 0) {
bolt = bolt.copy(bolt.getId(), type, key, defValue);
Uri uri = insertBolt(context.getContentResolver(), bolt);
bolt.setId(Long.parseLong(uri.getLastPathSegment()));
}
setValue(bolt);
private void onChange() {
boltChanged(getBolt(context.getContentResolver(), key));
}

abstract void boltChanged(@Nullable Bolt bolt);

class BoltContentObserver extends ContentObserver {
BoltContentObserver(Handler handler) {
super(handler);
Expand All @@ -110,7 +114,7 @@ public void onChange(boolean selfChange) {

@Override
public void onChange(boolean selfChange, Uri uri) {
BoltLiveData.this.boltChanged();
BoltLiveData.this.onChange();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.wrench.BoltLiveData;

import android.content.Context;
import android.net.Uri;

import com.izettle.wrench.core.Bolt;
import com.izettle.wrench.core.Nut;
import com.izettle.wrench.core.WrenchProviderContract;

class EnumBoltLiveData<T extends Enum> extends BoltLiveData {
private final Class<T> enumClass;
private final T defValue;

EnumBoltLiveData(Context context, String key, Class<T> enumClass, T defValue) {
super(context, key, Bolt.TYPE.ENUM);
this.enumClass = enumClass;
this.defValue = defValue;
}

private Uri insertBolt(Bolt bolt) {
return getContext().getContentResolver().insert(WrenchProviderContract.boltUri(), bolt.toContentValues());
}

private void insertNut(Nut nut) {
getContext().getContentResolver().insert(WrenchProviderContract.nutUri(), nut.toContentValues());
}

@Override
void boltChanged(Bolt bolt) {
if (bolt == null) {
setValue(new Bolt(0, getType(), getKey(), String.valueOf(defValue)));
return;
}

if (bolt.getId() == 0) {
bolt = bolt.copy(bolt.getId(), getType(), getKey(), String.valueOf(defValue));
Uri uri = insertBolt(bolt);
bolt.setId(Long.parseLong(uri.getLastPathSegment()));

for (T t : enumClass.getEnumConstants()) {
insertNut(new Nut(bolt.getId(), t.toString()));
}
}
setValue(bolt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.wrench.BoltLiveData;

import android.content.Context;
import android.net.Uri;

import com.izettle.wrench.core.Bolt;
import com.izettle.wrench.core.WrenchProviderContract;

class StringBoltLiveData extends BoltLiveData {
private final String defValue;

StringBoltLiveData(Context context, String key, String defValue, String type) {
super(context, key, type);

this.defValue = defValue;
}

@Override
void boltChanged(Bolt bolt) {
if (bolt == null) {
setValue(new Bolt(0, getType(), getKey(), defValue));
return;
}

if (bolt.getId() == 0) {
bolt = bolt.copy(bolt.getId(), getType(), getKey(), defValue);
Uri uri = getContext().getContentResolver().insert(WrenchProviderContract.boltUri(), bolt.toContentValues());
bolt.setId(Long.parseLong(uri.getLastPathSegment()));
}
setValue(bolt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ protected void onCreate(Bundle savedInstanceState) {
});

wrenchPreferences.getEnum(getString(R.string.enum_configuration), MyEnum.class, MyEnum.FIRST);
wrenchSampleViewModel.getBolt(getString(R.string.enum_configuration)).observe(this, bolt -> {
wrenchSampleViewModel.getEnumBolt().observe(this, bolt -> {
if (bolt != null) {
activityMainBinding.enumConfiguration.setText(bolt.getValue());
}
});

wrenchPreferences.getString(getString(R.string.service_configuration), null);
wrenchSampleViewModel.getBolt(getString(R.string.service_configuration)).observe(this, bolt -> {
wrenchSampleViewModel.getServiceStringBolt().observe(this, bolt -> {
if (bolt != null) {
activityMainBinding.serviceConfiguration.setText(bolt.getValue());
}
Expand All @@ -80,7 +80,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
}

@SuppressWarnings("unused")
enum MyEnum {
public enum MyEnum {
FIRST, SECOND, THIRD
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.arch.lifecycle.LiveData;
import android.support.annotation.NonNull;

import com.example.wrench.BoltLiveData.BoltLiveData;
import com.izettle.wrench.core.Bolt;

public class WrenchSampleViewModel extends AndroidViewModel {
Expand All @@ -13,23 +14,26 @@ public WrenchSampleViewModel(@NonNull Application application) {
}

LiveData<Bolt> getStringBolt() {
return BoltLiveData.string(getApplication(), getApplication().getResources().getString(R.string.string_configuration), "string1");
return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.string_configuration), "string1");
}

LiveData<Bolt> getIntBolt() {
return BoltLiveData.integer(getApplication(), getApplication().getResources().getString(R.string.int_configuration), 1);
return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.int_configuration), 1);
}

LiveData<Bolt> getBooleanBolt() {
return BoltLiveData.bool(getApplication(), getApplication().getResources().getString(R.string.boolean_configuration), true);
return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.boolean_configuration), true);
}

LiveData<Bolt> getUrlBolt() {
return BoltLiveData.string(getApplication(), getApplication().getResources().getString(R.string.url_configuration), "http://www.example.com/path?param=value");
return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.url_configuration), "http://www.example.com/path?param=value");
}

LiveData<Bolt> getBolt(String key) {
return new BoltLiveData(getApplication(), key, null, null);
LiveData<Bolt> getEnumBolt() {
return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.enum_configuration), MainActivity.MyEnum.class, MainActivity.MyEnum.FIRST);
}

LiveData<Bolt> getServiceStringBolt() {
return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.service_configuration), null);
}
}

0 comments on commit 2b9340f

Please sign in to comment.