Skip to content

Commit

Permalink
feat: extend AString from Parcelable
Browse files Browse the repository at this point in the history
  • Loading branch information
tynn committed Nov 8, 2021
1 parent 3abe911 commit 649f1ff
Show file tree
Hide file tree
Showing 23 changed files with 261 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
[![Build][build-shield]][build]
[![Download][download-shield]][download]
[![API][api-shield]][api]
###### A context aware string abstraction for _Android_
###### A context aware parcelable string abstraction for _Android_

```
public interface AString {
public interface AString extends Parcelable {
@Nullable
CharSequence invoke(@NonNull Context context);
}
Expand Down
5 changes: 3 additions & 2 deletions android.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
minSdkVersion 19
targetSdkVersion 30
targetSdkVersion 31
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
4 changes: 3 additions & 1 deletion astring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ plugins {
}

dependencies {
api 'androidx.fragment:fragment:1.3.4'
api 'androidx.fragment:fragment:1.3.6'

testImplementation project(':testing')

androidTestImplementation 'androidx.test:rules:1.4.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 Christian Schmitz
// SPDX-License-Identifier: Apache-2.0

package xyz.tynn.astring;

import static android.os.Parcel.obtain;
import static org.junit.Assert.assertEquals;

import android.os.Parcel;
import android.os.Parcelable;

class AssertParcelable {

private static ClassLoader getClassloader() {
return AssertParcelable.class.getClassLoader();
}

static <T extends Parcelable> void assertParcelable(T expected) {
Parcel parcel = obtain();
parcel.writeValue(expected);
parcel.setDataPosition(0);
assertEquals(expected, parcel.readValue(getClassloader()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2021 Christian Schmitz
// SPDX-License-Identifier: Apache-2.0

package xyz.tynn.astring;

import static xyz.tynn.astring.AssertParcelable.assertParcelable;

import org.junit.Test;

import java.util.Date;

public class ParcelableAStringTest {

public static final int RES_ID = 123;
public static final int QUANTITY = 456;
public static final Object[] FORMAT_ARGS = {"arg1", 2, 3L, 4.5, 6F, new Date()};

@Test
public void CharSequence_should_implement_parcelable() {
assertParcelable(new CharSequenceWrapper("test-string"));
}

@Test
public void NullValueWrapper_should_implement_parcelable() {
assertParcelable(NullValueWrapper.I);
}

@Test
public void QuantityStringResourceDelegate_should_implement_parcelable() {
assertParcelable(new QuantityStringResourceDelegate(RES_ID, QUANTITY, FORMAT_ARGS));
}

@Test
public void QuantityTextResourceDelegate_should_implement_parcelable() {
assertParcelable(new QuantityTextResourceDelegate(RES_ID, QUANTITY));
}

@Test
public void StringResourceDelegate_should_implement_parcelable() {
assertParcelable(new StringResourceDelegate(RES_ID, FORMAT_ARGS));
}

@Test
public void TextResourceDelegate_should_implement_parcelable() {
assertParcelable(new TextResourceDelegate(RES_ID));
}
}
8 changes: 7 additions & 1 deletion astring/src/main/java/xyz/tynn/astring/AString.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package xyz.tynn.astring;

import android.content.Context;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -18,7 +19,7 @@
* {@code AString} is almost always used from the main thread,
* therefore all implementations must be non-blocking since
*/
public interface AString {
public interface AString extends Parcelable {

/**
* Provides a context sensitive string
Expand All @@ -28,5 +29,10 @@ public interface AString {
*/
@Nullable
CharSequence invoke(@NonNull Context context);

@Override
default int describeContents() {
return 0;
}
}

22 changes: 22 additions & 0 deletions astring/src/main/java/xyz/tynn/astring/CharSequenceWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package xyz.tynn.astring;

import static android.text.TextUtils.CHAR_SEQUENCE_CREATOR;

import android.content.Context;
import android.os.Parcel;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -45,4 +49,22 @@ public int hashCode() {
public String toString() {
return "AString(" + "CharSequence(" + string + "))";
}

@Override
public void writeToParcel(Parcel dest, int flags) {
TextUtils.writeToParcel(string, dest, flags);
}

public static final Creator<CharSequenceWrapper> CREATOR = new Creator<CharSequenceWrapper>() {

@Override
public CharSequenceWrapper createFromParcel(Parcel source) {
return new CharSequenceWrapper(CHAR_SEQUENCE_CREATOR.createFromParcel(source));
}

@Override
public CharSequenceWrapper[] newArray(int size) {
return new CharSequenceWrapper[size];
}
};
}
21 changes: 21 additions & 0 deletions astring/src/main/java/xyz/tynn/astring/NullValueWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package xyz.tynn.astring;

import android.content.Context;
import android.os.Parcel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -16,6 +17,9 @@ final class NullValueWrapper implements AString {
@NonNull
static NullValueWrapper I = new NullValueWrapper();

private NullValueWrapper() {
}

@Nullable
@Override
public CharSequence invoke(@Nullable Context context) {
Expand All @@ -37,4 +41,21 @@ public int hashCode() {
public String toString() {
return "AString(" + "NullValue(" + null + "))";
}

@Override
public void writeToParcel(Parcel dest, int flags) {
}

public static final Creator<NullValueWrapper> CREATOR = new Creator<NullValueWrapper>() {

@Override
public NullValueWrapper createFromParcel(Parcel source) {
return I;
}

@Override
public NullValueWrapper[] newArray(int size) {
return new NullValueWrapper[size];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import android.content.Context;
import android.content.res.Resources;
import android.os.Parcel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -62,4 +63,25 @@ public String toString() {
sb.append(',').append(o);
return sb.append("))").toString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(resId);
dest.writeInt(quantity);
dest.writeValue(formatArgs);
}

public static final Creator<QuantityStringResourceDelegate> CREATOR = new Creator<QuantityStringResourceDelegate>() {

@Override
public QuantityStringResourceDelegate createFromParcel(Parcel source) {
return new QuantityStringResourceDelegate(source.readInt(), source.readInt(),
(Object[]) source.readValue(getClass().getClassLoader()));
}

@Override
public QuantityStringResourceDelegate[] newArray(int size) {
return new QuantityStringResourceDelegate[size];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package xyz.tynn.astring;

import android.content.Context;
import android.os.Parcel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -46,4 +47,23 @@ public int hashCode() {
public String toString() {
return "AString(" + "QuantityTextResource(" + resId + ',' + quantity + "))";
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(resId);
dest.writeInt(quantity);
}

public static final Creator<QuantityTextResourceDelegate> CREATOR = new Creator<QuantityTextResourceDelegate>() {

@Override
public QuantityTextResourceDelegate createFromParcel(Parcel source) {
return new QuantityTextResourceDelegate(source.readInt(), source.readInt());
}

@Override
public QuantityTextResourceDelegate[] newArray(int size) {
return new QuantityTextResourceDelegate[size];
}
};
}
21 changes: 21 additions & 0 deletions astring/src/main/java/xyz/tynn/astring/StringResourceDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package xyz.tynn.astring;

import android.content.Context;
import android.os.Parcel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -57,4 +58,24 @@ public String toString() {
sb.append(',').append(o);
return sb.append("))").toString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(resId);
dest.writeValue(formatArgs);
}

public static final Creator<StringResourceDelegate> CREATOR = new Creator<StringResourceDelegate>() {

@Override
public StringResourceDelegate createFromParcel(Parcel source) {
return new StringResourceDelegate(source.readInt(),
(Object[]) source.readValue(getClass().getClassLoader()));
}

@Override
public StringResourceDelegate[] newArray(int size) {
return new StringResourceDelegate[size];
}
};
}
19 changes: 19 additions & 0 deletions astring/src/main/java/xyz/tynn/astring/TextResourceDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package xyz.tynn.astring;

import android.content.Context;
import android.os.Parcel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -44,4 +45,22 @@ public int hashCode() {
public String toString() {
return "AString(" + "TextResource(" + resId + "))";
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(resId);
}

public static final Creator<TextResourceDelegate> CREATOR = new Creator<TextResourceDelegate>() {

@Override
public TextResourceDelegate createFromParcel(Parcel source) {
return new TextResourceDelegate(source.readInt());
}

@Override
public TextResourceDelegate[] newArray(int size) {
return new TextResourceDelegate[size];
}
};
}
11 changes: 11 additions & 0 deletions astring/src/main/kotlin/xyz/tynn/astring/AStringFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package xyz.tynn.astring

import android.os.Parcel
import android.text.TextUtils
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes

Expand All @@ -16,6 +18,9 @@ public val nullAsAString: AString = NullValueWrapper.I
* Creates an `AString` from a `CharSequence?`
*
* Returns [nullAsAString] for null
*
* **Note** that [TextUtils.writeToParcel] is used to parcel
* the [CharSequence] which might lose some custom styles
*/
@JvmName("createFromCharSequence")
public fun CharSequence?.asAString(): AString = if (this == null)
Expand Down Expand Up @@ -43,6 +48,9 @@ else QuantityStringResourceDelegate(
* Creates an `AString` from a plurals string resource with format arguments
*
* Returns [nullAsAString] for 0
*
* **Note** that [Parcel.writeValue] is used to parcel [formatArgs]
* which might throw a [RuntimeException] for un-parcelable values
*/
@JvmName("createFromQuantityStringResource")
public fun QuantityStringResource(
Expand Down Expand Up @@ -92,6 +100,9 @@ else StringResourceDelegate(
* Creates an `AString` from a string resource with format arguments
*
* Returns [nullAsAString] for 0
*
* **Note** that [Parcel.writeValue] is used to parcel [formatArgs]
* which might throw a [RuntimeException] for un-parcelable values
*/
@JvmName("createFromStringResource")
public fun StringResource(
Expand Down
Loading

0 comments on commit 649f1ff

Please sign in to comment.