Skip to content

Instantly share code, notes, and snippets.

@evant
Created May 13, 2015 15:20
Show Gist options
  • Save evant/bb8281c58bfb090f4a6c to your computer and use it in GitHub Desktop.
Save evant/bb8281c58bfb090f4a6c to your computer and use it in GitHub Desktop.
Helper for having a uniform resource text object.
import android.content.res.Resources;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.annotation.PluralsRes;
import android.support.annotation.StringRes;
import android.widget.TextView;
import java.util.Arrays;
/**
* Created by evantatarka on 5/13/15.
*/
public abstract class Text implements Parcelable {
public abstract CharSequence getText(Resources resources);
public static void setText(TextView textView, @Nullable Text text) {
if (text == null) {
textView.setText(null);
} else {
textView.setText(text.getText(textView.getResources()));
}
}
public static Text string(CharSequence string) {
return new StringText(string);
}
public static Text res(@StringRes int res) {
return new ResText(res, null);
}
public static Text res(@StringRes int res, Object... formatArgs) {
return new ResText(res, formatArgs);
}
public static Text plural(@PluralsRes int res, int quantity) {
return new PluralText(res, quantity, null);
}
public static Text plural(@PluralsRes int res, int quantity, Object... formatArgs) {
return new PluralText(res, quantity, formatArgs);
}
private static class StringText extends Text implements Parcelable {
CharSequence res;
StringText(CharSequence res) {
this.res = res;
}
@Override
public CharSequence getText(Resources resources) {
return res;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StringText that = (StringText) o;
return !(res != null ? !res.equals(that.res) : that.res != null);
}
@Override
public int hashCode() {
return res != null ? res.hashCode() : 0;
}
@Override
public String toString() {
return "Text(" + res + ")";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.res.toString());
}
private StringText(Parcel in) {
this.res = in.readString();
}
public static final Creator<StringText> CREATOR = new Creator<StringText>() {
public StringText createFromParcel(Parcel source) {
return new StringText(source);
}
public StringText[] newArray(int size) {
return new StringText[size];
}
};
}
private static class ResText extends Text implements Parcelable {
@StringRes
int res;
Object[] formatArgs;
ResText(@StringRes int res, Object[] formatArgs) {
this.res = res;
this.formatArgs = formatArgs;
}
@Override
public CharSequence getText(Resources resources) {
if (formatArgs == null) {
return resources.getString(res);
} else {
return resources.getString(res, formatArgs);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ResText resText = (ResText) o;
if (res != resText.res) return false;
// Probably incorrect - comparing Object[] arrays with Arrays.equals
return Arrays.equals(formatArgs, resText.formatArgs);
}
@Override
public int hashCode() {
int result = res;
result = 31 * result + (formatArgs != null ? Arrays.hashCode(formatArgs) : 0);
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("Text(").append(res);
if (formatArgs != null) {
builder.append(", ");
for (int i = 0; i < formatArgs.length; i++) {
Object arg = formatArgs[i];
builder.append(arg);
if (i < formatArgs.length - 1) {
builder.append(", ");
}
}
}
builder.append(")");
return builder.toString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.res);
dest.writeArray(this.formatArgs);
}
private ResText(Parcel in) {
this.res = in.readInt();
this.formatArgs = in.readArray(getClass().getClassLoader());
}
public static final Creator<ResText> CREATOR = new Creator<ResText>() {
public ResText createFromParcel(Parcel source) {
return new ResText(source);
}
public ResText[] newArray(int size) {
return new ResText[size];
}
};
}
private static class PluralText extends Text implements Parcelable {
@PluralsRes
int res;
int quantity;
Object[] formatArgs;
PluralText(int res, int quantity, Object[] formatArgs) {
this.res = res;
this.quantity = quantity;
this.formatArgs = formatArgs;
}
@Override
public CharSequence getText(Resources resources) {
if (formatArgs == null) {
return resources.getQuantityString(res, quantity);
} else {
return resources.getQuantityString(res, quantity, formatArgs);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PluralText that = (PluralText) o;
if (res != that.res) return false;
if (quantity != that.quantity) return false;
// Probably incorrect - comparing Object[] arrays with Arrays.equals
return Arrays.equals(formatArgs, that.formatArgs);
}
@Override
public int hashCode() {
int result = res;
result = 31 * result + quantity;
result = 31 * result + (formatArgs != null ? Arrays.hashCode(formatArgs) : 0);
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("Text(").append(res).append(", ").append(quantity);
if (formatArgs != null) {
builder.append(", ");
for (int i = 0; i < formatArgs.length; i++) {
Object arg = formatArgs[i];
builder.append(arg);
if (i < formatArgs.length - 1) {
builder.append(", ");
}
}
}
builder.append(")");
return builder.toString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.res);
dest.writeInt(this.quantity);
dest.writeArray(this.formatArgs);
}
private PluralText(Parcel in) {
this.res = in.readInt();
this.quantity = in.readInt();
this.formatArgs = in.readArray(getClass().getClassLoader());
}
public static final Creator<PluralText> CREATOR = new Creator<PluralText>() {
public PluralText createFromParcel(Parcel source) {
return new PluralText(source);
}
public PluralText[] newArray(int size) {
return new PluralText[size];
}
};
}
}
@evant
Copy link
Author

evant commented May 13, 2015

Why? Because you may have a property that can either be a string or a stringId depending on context. Or you have a stringId you don't want to depend on a context to immediately change it to a string and then have to figure out when to update it when there is a configuration change.

@evant
Copy link
Author

evant commented May 13, 2015

Copyright 2015 Evan Tatarka

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment