Adds languages to monster cards.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package com.majinnaibu.monstercards.models;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Language implements Comparator<Language>, Comparable<Language> {
|
||||
|
||||
public Language(String name, boolean speaks) {
|
||||
mName = name;
|
||||
mSpeaks = speaks;
|
||||
}
|
||||
|
||||
private String mName;
|
||||
public String getName() {
|
||||
return mName;
|
||||
}
|
||||
public void setName(String value) {
|
||||
mName = value;
|
||||
}
|
||||
|
||||
private boolean mSpeaks;
|
||||
public boolean getSpeaks() {
|
||||
return mSpeaks;
|
||||
}
|
||||
public void setSpeaks(boolean value) {
|
||||
mSpeaks = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Language o) {
|
||||
return this.getName().compareToIgnoreCase(o.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(Language o1, Language o2) {
|
||||
return o1.getName().compareToIgnoreCase(o2.getName());
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ public class Monster {
|
||||
mSkills = new HashSet<>();
|
||||
mDamageTypes = new HashSet<>();
|
||||
mConditionImmunities = new HashSet<>();
|
||||
mLanguages = new HashSet<>();
|
||||
}
|
||||
|
||||
private String mName;
|
||||
@@ -730,22 +731,6 @@ public class Monster {
|
||||
mTruesight = value;
|
||||
}
|
||||
|
||||
private int mTelepathy;
|
||||
public int getTelepathy() {
|
||||
return mTelepathy;
|
||||
}
|
||||
public void setTelepathy(int value) {
|
||||
mTelepathy = value;
|
||||
}
|
||||
|
||||
private String mUnderstandsBut;
|
||||
public String getUnderstandsBut() {
|
||||
return mUnderstandsBut;
|
||||
}
|
||||
public void setUnderstandsBut(String value) {
|
||||
mUnderstandsBut = value;
|
||||
}
|
||||
|
||||
public String getSensesDescription() {
|
||||
ArrayList<String> parts = new ArrayList<>();
|
||||
|
||||
@@ -770,4 +755,89 @@ public class Monster {
|
||||
return StringHelper.join(", ", parts);
|
||||
}
|
||||
|
||||
private HashSet<Language> mLanguages;
|
||||
public Set<Language> getLanguages() {
|
||||
return mLanguages;
|
||||
}
|
||||
public void addLanguage(Language value) {
|
||||
mLanguages.add(value);
|
||||
}
|
||||
public void removeLanguage(Language value) {
|
||||
mLanguages.remove(value);
|
||||
}
|
||||
public void clearLanguages() {
|
||||
mLanguages.clear();
|
||||
}
|
||||
|
||||
private int mTelepathy;
|
||||
public int getTelepathy() {
|
||||
return mTelepathy;
|
||||
}
|
||||
public void setTelepathy(int value) {
|
||||
mTelepathy = value;
|
||||
}
|
||||
|
||||
private String mUnderstandsBut;
|
||||
public String getUnderstandsBut() {
|
||||
return mUnderstandsBut;
|
||||
}
|
||||
public void setUnderstandsBut(String value) {
|
||||
mUnderstandsBut = value;
|
||||
}
|
||||
|
||||
public String getLanguagesDescription() {
|
||||
ArrayList<String> spokenLanguages = new ArrayList<>();
|
||||
ArrayList<String> understoodLanguages = new ArrayList<>();
|
||||
for (Language language : mLanguages) {
|
||||
if (language != null) {
|
||||
if (language.getSpeaks()) {
|
||||
spokenLanguages.add(language.getName());
|
||||
} else {
|
||||
understoodLanguages.add(language.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Collections.sort(spokenLanguages);
|
||||
Collections.sort(understoodLanguages);
|
||||
|
||||
String spokenLanguagesString = StringHelper.oxfordJoin(", ", ", and ", " and ", spokenLanguages);
|
||||
String understoodLanguagesString = StringHelper.oxfordJoin(", ", ", and ", " and ", understoodLanguages);
|
||||
|
||||
String understandsBut = getUnderstandsBut();
|
||||
boolean hasUnderstandsBut = understandsBut.length() > 0;
|
||||
int telepathy = getTelepathy();
|
||||
boolean hasTelepathy = telepathy > 0;
|
||||
String telepathyString = String.format(Locale.US, ", telepathy %d ft.", telepathy);
|
||||
|
||||
if (spokenLanguages.size() > 0) {
|
||||
if (understoodLanguages.size() > 0) {
|
||||
return String.format(
|
||||
"%s, understands %s%s%s",
|
||||
spokenLanguagesString,
|
||||
understoodLanguagesString,
|
||||
hasUnderstandsBut ? " but " + understandsBut : "",
|
||||
hasTelepathy ? telepathyString : "");
|
||||
} else {
|
||||
return String.format(
|
||||
"%s%s%s",
|
||||
spokenLanguagesString,
|
||||
hasUnderstandsBut ? " but " + understandsBut : "",
|
||||
hasTelepathy ? telepathyString : "");
|
||||
}
|
||||
} else {
|
||||
if (understoodLanguages.size() > 0) {
|
||||
return String.format(
|
||||
"understands %s%s%s",
|
||||
understoodLanguagesString,
|
||||
hasUnderstandsBut ? " but " + understandsBut : "",
|
||||
hasTelepathy ? telepathyString : "");
|
||||
} else {
|
||||
return String.format(
|
||||
"%S%s",
|
||||
hasUnderstandsBut ? "none but " + understandsBut : "",
|
||||
hasTelepathy ? telepathyString : "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import androidx.lifecycle.ViewModelProvider;
|
||||
import com.majinnaibu.monstercards.R;
|
||||
import com.majinnaibu.monstercards.helpers.StringHelper;
|
||||
import com.majinnaibu.monstercards.models.DamageType;
|
||||
import com.majinnaibu.monstercards.models.Language;
|
||||
import com.majinnaibu.monstercards.models.Monster;
|
||||
import com.majinnaibu.monstercards.models.SavingThrow;
|
||||
import com.majinnaibu.monstercards.models.Skill;
|
||||
@@ -91,6 +92,13 @@ public class MonsterFragment extends Fragment {
|
||||
monster.setTruesight("40");
|
||||
monster.setTelepathy(20);
|
||||
monster.setUnderstandsBut("doesn't care");
|
||||
// Languages
|
||||
monster.addLanguage(new Language("English", true));
|
||||
monster.addLanguage(new Language("Steve", false));
|
||||
monster.addLanguage(new Language("Spanish", true));
|
||||
monster.addLanguage(new Language("French", true));
|
||||
monster.addLanguage(new Language("Mermataur", false));
|
||||
monster.addLanguage(new Language("Goldfish", false));
|
||||
|
||||
// Challenge Rating
|
||||
monster.setChallengeRating("*");
|
||||
@@ -280,6 +288,18 @@ public class MonsterFragment extends Fragment {
|
||||
}
|
||||
});
|
||||
|
||||
final TextView monsterLanguages = root.findViewById(R.id.languages);
|
||||
monsterViewModel.getLanguages().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
@Override
|
||||
public void onChanged(String languages) {
|
||||
if (StringHelper.isNullOrEmpty(languages)) {
|
||||
monsterLanguages.setVisibility(View.GONE);
|
||||
} else {
|
||||
monsterLanguages.setVisibility(View.VISIBLE);
|
||||
}
|
||||
monsterLanguages.setText(Html.fromHtml("<b>Languages</b> " + languages));
|
||||
}
|
||||
});
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ public class MonsterViewModel extends ViewModel {
|
||||
mConditionImmunities.setValue("");
|
||||
mSenses = new MutableLiveData<>();
|
||||
mSenses.setValue("");
|
||||
mLanguages = new MutableLiveData<>();
|
||||
mLanguages.setValue("");
|
||||
}
|
||||
|
||||
private MutableLiveData<String> mName;
|
||||
@@ -120,6 +122,10 @@ public class MonsterViewModel extends ViewModel {
|
||||
public LiveData<String> getSenses() {
|
||||
return mSenses;
|
||||
}
|
||||
private MutableLiveData<String> mLanguages;
|
||||
public LiveData<String> getLanguages() {
|
||||
return mLanguages;
|
||||
}
|
||||
|
||||
private Monster mMonster;
|
||||
public void setMonster(Monster monster) {
|
||||
@@ -142,5 +148,6 @@ public class MonsterViewModel extends ViewModel {
|
||||
mDamageImmunities.setValue(mMonster.getDamageImmunitiesDescription());
|
||||
mConditionImmunities.setValue(mMonster.getConditionImmunitiesDescription());
|
||||
mSenses.setValue(monster.getSensesDescription());
|
||||
mLanguages.setValue(mMonster.getLanguagesDescription());
|
||||
}
|
||||
}
|
||||
@@ -396,5 +396,18 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/condition_immunities"
|
||||
tools:text="Senses" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/languages"
|
||||
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"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/senses"
|
||||
tools:text="Languages" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</ScrollView>
|
||||
Reference in New Issue
Block a user