1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

Merge branch 'master' into pr/814

This commit is contained in:
Austin Huang 2021-03-21 19:15:23 -04:00
commit 915e893cc1
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
6 changed files with 125 additions and 10 deletions

View File

@ -2,7 +2,6 @@ package awais.instagrabber.customviews.emoji;
import androidx.annotation.NonNull;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
@ -12,10 +11,12 @@ public class Emoji {
private final List<Emoji> variants;
private GoogleCompatEmojiDrawable drawable;
public Emoji(final String unicode, final String name) {
public Emoji(final String unicode,
final String name,
final List<Emoji> variants) {
this.unicode = unicode;
this.name = name;
this.variants = new LinkedList<>();
this.variants = variants;
}
public String getUnicode() {
@ -35,7 +36,7 @@ public class Emoji {
}
public GoogleCompatEmojiDrawable getDrawable() {
if (drawable == null) {
if (drawable == null && unicode != null) {
drawable = new GoogleCompatEmojiDrawable(unicode);
}
return drawable;
@ -60,6 +61,7 @@ public class Emoji {
return "Emoji{" +
"unicode='" + unicode + '\'' +
", name='" + name + '\'' +
", variants=" + variants +
'}';
}
}

View File

@ -1,8 +1,8 @@
package awais.instagrabber.customviews.emoji;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
@ -10,12 +10,13 @@ import awais.instagrabber.R;
public class EmojiCategory {
private final EmojiCategoryType type;
private final Map<String, Emoji> emojis = new LinkedHashMap<>();
private final Map<String, Emoji> emojis;
@DrawableRes
private int drawableRes;
public EmojiCategory(final EmojiCategoryType type) {
public EmojiCategory(final EmojiCategoryType type, final Map<String, Emoji> emojis) {
this.type = type;
this.emojis = emojis;
}
public EmojiCategoryType getType() {
@ -73,4 +74,13 @@ public class EmojiCategory {
public int hashCode() {
return Objects.hash(type);
}
@NonNull
@Override
public String toString() {
return "EmojiCategory{" +
"type=" + type +
", emojis=" + emojis +
'}';
}
}

View File

@ -378,6 +378,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe
if (getArguments() == null) return;
final HashTagFragmentArgs fragmentArgs = HashTagFragmentArgs.fromBundle(getArguments());
hashtag = fragmentArgs.getHashtag();
if (hashtag.charAt(0) == '#') hashtag = hashtag.substring(1);
fetchHashtagModel();
}

View File

@ -0,0 +1,52 @@
package awais.instagrabber.utils.emoji;
import android.util.Log;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import awais.instagrabber.customviews.emoji.Emoji;
import awais.instagrabber.customviews.emoji.EmojiCategory;
import awais.instagrabber.customviews.emoji.EmojiCategoryType;
public class EmojiCategoryDeserializer implements JsonDeserializer<EmojiCategory> {
private static final String TAG = EmojiCategoryDeserializer.class.getSimpleName();
@Override
public EmojiCategory deserialize(final JsonElement json,
final Type typeOfT,
final JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
final JsonElement typeElement = jsonObject.get("type");
final JsonObject emojisObject = jsonObject.getAsJsonObject("emojis");
if (typeElement == null || emojisObject == null) {
throw new JsonParseException("Invalid json for EmojiCategory");
}
final String typeString = typeElement.getAsString();
EmojiCategoryType type;
try {
type = EmojiCategoryType.valueOf(typeString);
} catch (IllegalArgumentException e) {
Log.e(TAG, "deserialize: ", e);
type = EmojiCategoryType.OTHERS;
}
final Map<String, Emoji> emojis = new LinkedHashMap<>();
for (final Map.Entry<String, JsonElement> emojiObjectEntry : emojisObject.entrySet()) {
final String unicode = emojiObjectEntry.getKey();
final JsonElement value = emojiObjectEntry.getValue();
if (unicode == null || value == null) {
throw new JsonParseException("Invalid json for EmojiCategory");
}
final Emoji emoji = context.deserialize(value, Emoji.class);
emojis.put(unicode, emoji);
}
return new EmojiCategory(type, emojis);
}
}

View File

@ -0,0 +1,44 @@
package awais.instagrabber.utils.emoji;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;
import awais.instagrabber.customviews.emoji.Emoji;
public class EmojiDeserializer implements JsonDeserializer<Emoji> {
@Override
public Emoji deserialize(final JsonElement json,
final Type typeOfT,
final JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
final JsonElement unicodeElement = jsonObject.get("unicode");
final JsonElement nameElement = jsonObject.get("name");
if (unicodeElement == null || nameElement == null) {
throw new JsonParseException("Invalid json for Emoji class");
}
final JsonElement variantsElement = jsonObject.get("variants");
final List<Emoji> variants = new LinkedList<>();
if (variantsElement != null) {
final JsonArray variantsArray = variantsElement.getAsJsonArray();
for (final JsonElement variantElement : variantsArray) {
final Emoji variant = context.deserialize(variantElement, Emoji.class);
if (variant != null) {
variants.add(variant);
}
}
}
return new Emoji(
unicodeElement.getAsString(),
nameElement.getAsString(),
variants
);
}
}

View File

@ -3,10 +3,11 @@ package awais.instagrabber.utils.emoji;
import android.util.Log;
import com.google.common.collect.ImmutableList;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Collection;
@ -52,7 +53,12 @@ public final class EmojiParser {
}
try (final InputStream in = classLoader.getResourceAsStream(file)) {
final String json = NetworkUtils.readFromInputStream(in);
final Gson gson = new Gson();
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(EmojiCategory.class, new EmojiCategoryDeserializer())
.registerTypeAdapter(Emoji.class, new EmojiDeserializer())
.setLenient()
.create();
final Type type = new TypeToken<Map<EmojiCategoryType, EmojiCategory>>() {}.getType();
categoryMap = gson.fromJson(json, type);
// Log.d(TAG, "EmojiParser: " + categoryMap);
@ -68,7 +74,7 @@ public final class EmojiParser {
.build()
.stream())
.collect(Collectors.toMap(Emoji::getUnicode, Function.identity()));
} catch (IOException e) {
} catch (Exception e) {
Log.e(TAG, "EmojiParser: ", e);
}
}