fix rotation error in stable

This commit is contained in:
Phillip Thelen 2019-04-19 11:04:54 +02:00
parent 6d2b26fe64
commit 28967504b7
3 changed files with 94 additions and 94 deletions

View file

@ -129,22 +129,19 @@ class StableRecyclerFragment : BaseFragment() {
val castedAnimal = animal as? OwnedObject ?: return@forEach
animalMap[castedAnimal.key ?: ""] = castedAnimal
}
return@map animalMap
animalMap
}
compositeSubscription.add(observable.zipWith(ownedObservable, BiFunction<RealmResults<out Animal>, Map<String, OwnedObject>, ArrayList<Any>> { unsortedAnimals, ownedAnimals ->
return@BiFunction mapAnimals(unsortedAnimals, ownedAnimals)
mapAnimals(unsortedAnimals, ownedAnimals)
}).subscribe(Consumer { items -> adapter?.setItemList(items) }, RxErrorHandler.handleEmptyError()))
}
private fun mapAnimals(unsortedAnimals: RealmResults<out Animal>, ownedAnimals: Map<String, OwnedObject>): ArrayList<Any> {
val items = ArrayList<Any>()
if (unsortedAnimals.size == 0) {
return items
}
var lastAnimal: Animal = unsortedAnimals[0] ?: return items
var lastSectionTitle = ""
var lastAnimal: Animal = unsortedAnimals[0]!!
for (animal in unsortedAnimals) {
val identifier = if (animal.animal.isNotEmpty()) animal.animal else animal.key
val lastIdentifier = if (lastAnimal.animal.isNotEmpty()) lastAnimal.animal else lastAnimal.key
@ -161,19 +158,17 @@ class StableRecyclerFragment : BaseFragment() {
items.add(animal.type)
lastSectionTitle = animal.type
}
if (user != null && user?.items != null) {
when (itemType) {
"pets" -> {
val ownedPet = ownedAnimals[animal?.key] as? OwnedPet
if (ownedPet?.trained ?: 0 > 0) {
lastAnimal.numberOwned += 1
}
when (itemType) {
"pets" -> {
val ownedPet = ownedAnimals[animal?.key] as? OwnedPet
if (ownedPet?.trained ?: 0 > 0) {
lastAnimal.numberOwned += 1
}
"mounts" -> {
val ownedMount = ownedAnimals[animal?.key] as? OwnedMount
if (ownedMount?.owned == true) {
lastAnimal.numberOwned = lastAnimal.numberOwned + 1
}
}
"mounts" -> {
val ownedMount = ownedAnimals[animal?.key] as? OwnedMount
if (ownedMount?.owned == true) {
lastAnimal.numberOwned = lastAnimal.numberOwned + 1
}
}
}

View file

@ -1,76 +0,0 @@
package com.habitrpg.android.habitica.utils;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class DateDeserializer implements JsonDeserializer<Date>, JsonSerializer<Date> {
private final DateFormat dateFormat;
private final DateFormat alternativeFormat;
private final DateFormat nextDueFormat;
public DateDeserializer() {
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
alternativeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
alternativeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
nextDueFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss zzzz", Locale.US);
nextDueFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}
@Override
public synchronized Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) {
if (jsonElement.isJsonArray()) {
if (jsonElement.getAsJsonArray().size() == 0) {
return null;
}
jsonElement = jsonElement.getAsJsonArray().get(0);
}
if (jsonElement.getAsString().length() == 0) {
return null;
}
try {
return dateFormat.parse(jsonElement.getAsString());
} catch (ParseException e) {
try {
return alternativeFormat.parse(jsonElement.getAsString());
} catch (ParseException e1) {
try {
return nextDueFormat.parse(jsonElement.getAsString());
} catch (ParseException e2) {
try {
Long timestamp = jsonElement.getAsLong();
if (timestamp > 0) {
return new Date(timestamp);
} else {
return null;
}
} catch (NumberFormatException e3) {
return null;
}
}
}
}
}
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return new JsonPrimitive("");
}
return new JsonPrimitive(this.dateFormat.format(src));
}
}

View file

@ -0,0 +1,81 @@
package com.habitrpg.android.habitica.utils
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import java.lang.reflect.Type
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone
class DateDeserializer : JsonDeserializer<Date>, JsonSerializer<Date> {
private val dateFormat: DateFormat
private val alternativeFormat: DateFormat
private val nextDueFormat: DateFormat
init {
dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
alternativeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
alternativeFormat.timeZone = TimeZone.getTimeZone("UTC")
nextDueFormat = SimpleDateFormat("E MMM dd yyyy HH:mm:ss zzzz", Locale.US)
nextDueFormat.timeZone = TimeZone.getTimeZone("UTC")
}
@Synchronized
@Suppress("ReturnCount")
override fun deserialize(jsonElement: JsonElement, type: Type, jsonDeserializationContext: JsonDeserializationContext): Date? {
var element = jsonElement
if (element.isJsonArray) {
if (element.asJsonArray.size() == 0) {
return null
}
element = element.asJsonArray.get(0)
}
if (element.asString.isEmpty()) {
return null
}
val jsonString = element.asString
return try {
dateFormat.parse(jsonString)
} catch (e: ParseException) {
try {
alternativeFormat.parse(jsonString)
} catch (e1: ParseException) {
try {
nextDueFormat.parse(jsonString)
} catch (e2: ParseException) {
try {
val timestamp = jsonElement.asLong
if (timestamp > 0) {
Date(timestamp)
} else {
null
}
} catch (e3: NumberFormatException) {
null
}
}
}
}
}
override fun serialize(src: Date?, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
return if (src == null) {
JsonPrimitive("")
} else JsonPrimitive(this.dateFormat.format(src))
}
}