diff --git a/wrench-sample/src/main/java/com/example/wrench/BoltLiveData.java b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/BoltLiveData.java similarity index 55% rename from wrench-sample/src/main/java/com/example/wrench/BoltLiveData.java rename to wrench-sample/src/main/java/com/example/wrench/BoltLiveData/BoltLiveData.java index beb362d..542866a 100644 --- a/wrench-sample/src/main/java/com/example/wrench/BoltLiveData.java +++ b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/BoltLiveData.java @@ -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; @@ -12,31 +13,33 @@ import com.izettle.wrench.core.Bolt; import com.izettle.wrench.core.WrenchProviderContract; -public class BoltLiveData extends LiveData { +public abstract class BoltLiveData extends LiveData { 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 BoltLiveData create(Context context, String key, Class enumClass, T def) { + return new EnumBoltLiveData<>(context, key, enumClass, def); } @Nullable @@ -65,16 +68,27 @@ 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 @@ -82,22 +96,12 @@ 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); @@ -110,7 +114,7 @@ public void onChange(boolean selfChange) { @Override public void onChange(boolean selfChange, Uri uri) { - BoltLiveData.this.boltChanged(); + BoltLiveData.this.onChange(); } } } \ No newline at end of file diff --git a/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/EnumBoltLiveData.java b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/EnumBoltLiveData.java new file mode 100644 index 0000000..ca33896 --- /dev/null +++ b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/EnumBoltLiveData.java @@ -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 extends BoltLiveData { + private final Class enumClass; + private final T defValue; + + EnumBoltLiveData(Context context, String key, Class 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); + } +} diff --git a/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/StringBoltLiveData.java b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/StringBoltLiveData.java new file mode 100644 index 0000000..b097fee --- /dev/null +++ b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/StringBoltLiveData.java @@ -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); + } +} diff --git a/wrench-sample/src/main/java/com/example/wrench/MainActivity.java b/wrench-sample/src/main/java/com/example/wrench/MainActivity.java index d0c0b41..5f88864 100644 --- a/wrench-sample/src/main/java/com/example/wrench/MainActivity.java +++ b/wrench-sample/src/main/java/com/example/wrench/MainActivity.java @@ -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()); } @@ -80,7 +80,7 @@ public boolean onCreateOptionsMenu(Menu menu) { } @SuppressWarnings("unused") - enum MyEnum { + public enum MyEnum { FIRST, SECOND, THIRD } } diff --git a/wrench-sample/src/main/java/com/example/wrench/WrenchSampleViewModel.java b/wrench-sample/src/main/java/com/example/wrench/WrenchSampleViewModel.java index 8f42609..91deac2 100644 --- a/wrench-sample/src/main/java/com/example/wrench/WrenchSampleViewModel.java +++ b/wrench-sample/src/main/java/com/example/wrench/WrenchSampleViewModel.java @@ -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 { @@ -13,23 +14,26 @@ public WrenchSampleViewModel(@NonNull Application application) { } LiveData 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 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 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 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 getBolt(String key) { - return new BoltLiveData(getApplication(), key, null, null); + LiveData getEnumBolt() { + return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.enum_configuration), MainActivity.MyEnum.class, MainActivity.MyEnum.FIRST); } + LiveData getServiceStringBolt() { + return BoltLiveData.create(getApplication(), getApplication().getResources().getString(R.string.service_configuration), null); + } }