This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation for Enum in BoltLiveData.
- Loading branch information
Showing
5 changed files
with
126 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
wrench-sample/src/main/java/com/example/wrench/BoltLiveData/EnumBoltLiveData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
wrench-sample/src/main/java/com/example/wrench/BoltLiveData/StringBoltLiveData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters