Adds monster meta (size, type, subtype/tag, and alignment) to monster cards.
This commit is contained in:
@@ -1,11 +1,6 @@
|
|||||||
package com.majinnaibu.monstercards.helpers;
|
package com.majinnaibu.monstercards.helpers;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
@SuppressWarnings({"BooleanMethodIsAlwaysInverted", "RedundantIfStatement"})
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
@SuppressWarnings({"RedundantIfStatement"})
|
|
||||||
public final class StringHelper {
|
public final class StringHelper {
|
||||||
public static boolean isNullOrEmpty(CharSequence value) {
|
public static boolean isNullOrEmpty(CharSequence value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
@@ -18,62 +13,4 @@ public final class StringHelper {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public static String join(String delimiter, @NonNull Collection<String> strings) {
|
|
||||||
int length = strings.size();
|
|
||||||
if (length < 1) {
|
|
||||||
return "";
|
|
||||||
} else {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
boolean isFirst = true;
|
|
||||||
for (String element : strings) {
|
|
||||||
if (!isFirst) {
|
|
||||||
sb.append(delimiter);
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.append(element);
|
|
||||||
|
|
||||||
isFirst = false;
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String oxfordJoin(String delimiter, String lastDelimiter, String onlyDelimiter, @NonNull Collection<String> strings) {
|
|
||||||
int length = strings.size();
|
|
||||||
if (length < 1) {
|
|
||||||
return "";
|
|
||||||
} else if (length == 2) {
|
|
||||||
return join(onlyDelimiter, strings);
|
|
||||||
} else {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
int index = 0;
|
|
||||||
int lastIndex = length - 1;
|
|
||||||
for (String element : strings) {
|
|
||||||
if (index > 0 && index < lastIndex) {
|
|
||||||
sb.append(delimiter);
|
|
||||||
} else if (index > 0 && index >= lastIndex) {
|
|
||||||
sb.append(lastDelimiter);
|
|
||||||
}
|
|
||||||
sb.append(element);
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static Integer parseInt(String s) {
|
|
||||||
try {
|
|
||||||
return Integer.parseInt(s);
|
|
||||||
} catch (NumberFormatException _ex) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean containsCaseInsensitive(@NonNull String text, @NonNull String search) {
|
|
||||||
// TODO: find a locale independent way to do this
|
|
||||||
return text.toLowerCase().contains(search.toLowerCase());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
package com.majinnaibu.monstercards.models;
|
package com.majinnaibu.monstercards.models;
|
||||||
|
|
||||||
|
import com.majinnaibu.monstercards.helpers.StringHelper;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Monster {
|
public class Monster {
|
||||||
|
|
||||||
private String mName;
|
private String mName;
|
||||||
@@ -10,4 +15,76 @@ public class Monster {
|
|||||||
mName = value;
|
mName = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String mSize;
|
||||||
|
public String getSize() {
|
||||||
|
return mSize;
|
||||||
|
}
|
||||||
|
public void setSize(String value) {
|
||||||
|
mSize = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String mType;
|
||||||
|
public String getType() {
|
||||||
|
return mType;
|
||||||
|
}
|
||||||
|
public void setType(String value) {
|
||||||
|
mType = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String mTag;
|
||||||
|
public String getTag() {
|
||||||
|
return mTag;
|
||||||
|
}
|
||||||
|
public void setTag(String value) {
|
||||||
|
mTag = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String mAlignment;
|
||||||
|
public String getAlignment() {
|
||||||
|
return mAlignment;
|
||||||
|
}
|
||||||
|
public void setAlignment(String value) {
|
||||||
|
mAlignment = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMeta() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
boolean isFirstOutput = true;
|
||||||
|
String size = getSize();
|
||||||
|
if (!StringHelper.isNullOrEmpty(size)) {
|
||||||
|
sb.append(size);
|
||||||
|
isFirstOutput = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String type = getType();
|
||||||
|
if (!StringHelper.isNullOrEmpty(type)) {
|
||||||
|
if (!isFirstOutput) {
|
||||||
|
sb.append(" ");
|
||||||
|
}
|
||||||
|
sb.append(type);
|
||||||
|
isFirstOutput = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String tag = getTag();
|
||||||
|
if (!StringHelper.isNullOrEmpty(tag)) {
|
||||||
|
if (!isFirstOutput) {
|
||||||
|
sb.append(" ");
|
||||||
|
}
|
||||||
|
sb.append("(");
|
||||||
|
sb.append(tag);
|
||||||
|
sb.append(")");
|
||||||
|
isFirstOutput = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String alignment = getAlignment();
|
||||||
|
if (!StringHelper.isNullOrEmpty(alignment)) {
|
||||||
|
if (!isFirstOutput) {
|
||||||
|
sb.append(", ");
|
||||||
|
}
|
||||||
|
sb.append(alignment);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import androidx.lifecycle.ViewModelProvider;
|
|||||||
import com.majinnaibu.monstercards.R;
|
import com.majinnaibu.monstercards.R;
|
||||||
import com.majinnaibu.monstercards.models.Monster;
|
import com.majinnaibu.monstercards.models.Monster;
|
||||||
|
|
||||||
|
@SuppressWarnings("FieldCanBeLocal")
|
||||||
public class MonsterFragment extends Fragment {
|
public class MonsterFragment extends Fragment {
|
||||||
|
|
||||||
private MonsterViewModel monsterViewModel;
|
private MonsterViewModel monsterViewModel;
|
||||||
@@ -24,7 +25,13 @@ public class MonsterFragment extends Fragment {
|
|||||||
|
|
||||||
// TODO: remove this block make the monster ID a parameter to the view and get the monster from saved data (sqlite)
|
// TODO: remove this block make the monster ID a parameter to the view and get the monster from saved data (sqlite)
|
||||||
Monster monster = new Monster();
|
Monster monster = new Monster();
|
||||||
|
// Name
|
||||||
monster.setName("Pixie");
|
monster.setName("Pixie");
|
||||||
|
// Meta
|
||||||
|
monster.setSize("tiny");
|
||||||
|
monster.setType("fey");
|
||||||
|
monster.setTag("");
|
||||||
|
monster.setAlignment("neutral good");
|
||||||
// END remove block
|
// END remove block
|
||||||
monsterViewModel = new ViewModelProvider(this).get(MonsterViewModel.class);
|
monsterViewModel = new ViewModelProvider(this).get(MonsterViewModel.class);
|
||||||
View root = inflater.inflate(R.layout.fragment_monster, container, false);
|
View root = inflater.inflate(R.layout.fragment_monster, container, false);
|
||||||
@@ -38,6 +45,14 @@ public class MonsterFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
final TextView monsterMeta = root.findViewById(R.id.meta);
|
||||||
|
monsterViewModel.getMeta().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||||
|
@Override
|
||||||
|
public void onChanged(@Nullable String s) {
|
||||||
|
monsterMeta.setText(s);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,17 +12,23 @@ public class MonsterViewModel extends ViewModel {
|
|||||||
mMonster = null;
|
mMonster = null;
|
||||||
mName = new MutableLiveData<>();
|
mName = new MutableLiveData<>();
|
||||||
mName.setValue("");
|
mName.setValue("");
|
||||||
|
mMeta = new MutableLiveData<>();
|
||||||
|
mMeta.setValue("");
|
||||||
}
|
}
|
||||||
|
|
||||||
private MutableLiveData<String> mName;
|
private MutableLiveData<String> mName;
|
||||||
public LiveData<String> getName() {
|
public LiveData<String> getName() {
|
||||||
return mName;
|
return mName;
|
||||||
}
|
}
|
||||||
|
private MutableLiveData<String> mMeta;
|
||||||
|
public LiveData<String> getMeta() {
|
||||||
|
return mMeta;
|
||||||
|
}
|
||||||
|
|
||||||
private Monster mMonster;
|
private Monster mMonster;
|
||||||
public void setMonster(Monster monster) {
|
public void setMonster(Monster monster) {
|
||||||
mMonster = monster;
|
mMonster = monster;
|
||||||
mName.setValue(mMonster.getName());
|
mName.setValue(mMonster.getName());
|
||||||
|
mMeta.setValue(mMonster.getMeta());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,5 +26,20 @@
|
|||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
tools:text="Pixie" />
|
tools:text="Pixie" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/meta"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:layout_marginEnd="8dp"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:textSize="15sp"
|
||||||
|
android:textStyle="italic"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/name"
|
||||||
|
tools:text="Tiny fey, neutral good" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
Reference in New Issue
Block a user