Skip to content

Commit

Permalink
Added imageType to the sticker url to fix Discord GIFs & using sticke…
Browse files Browse the repository at this point in the history
…r code to search in Gboard
  • Loading branch information
Drakota committed Mar 1, 2021
1 parent bf969e3 commit 7f813fd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MainActivity: FlutterActivity() {
call, result ->
if (call.method == "generatePack") {
val intent = Intent(baseContext, StickerIndexingService::class.java)
intent.putExtra("emotes", call.arguments as ArrayList<String>)
intent.putExtra("emotes", call.arguments as ArrayList<HashMap<String, String>>)
startService(intent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ class StickerIndexingService() : IntentService("StickerIndexingService") {
private val STICKER_PACK_NAME = "BTTV Stickers"
private val STICKER_PACK_URL = "bttvstickers://emote/pack/bttvstickers"
private val STICKER_PACK_IMAGE = "android.resource://com.drakota.bttvstickers/" + R.mipmap.ic_launcher
private val STICKER_IMAGE_URL_PATTERN = "https://cdn.betterttv.net/emote/%s/3x"
private val STICKER_URL_PATTERN = "bttvstickers://emote/%s"

override fun onHandleIntent(intent: Intent?) {
try {
clearStickerPack()
val emotes = intent?.getStringArrayListExtra("emotes")!!
val emotes = intent?.getSerializableExtra("emotes")!! as ArrayList<HashMap<String, String>>
if (emotes.isEmpty()) {
// If we are clearing the pack, just clear it and return
return
Expand All @@ -46,19 +45,19 @@ class StickerIndexingService() : IntentService("StickerIndexingService") {
}
}

private fun getIndexableStickers(emotes: ArrayList<String>): List<Indexable> {
private fun getIndexableStickers(emotes: ArrayList<HashMap<String, String>>): List<Indexable> {
val stickerBuilders = getStickerBuilders(emotes)
return stickerBuilders.map { it.build() }
}

private fun getStickerBuilders(emotes: ArrayList<String>): List<StickerBuilder> {
private fun getStickerBuilders(emotes: ArrayList<HashMap<String, String>>): List<StickerBuilder> {
val stickerBuilders = arrayListOf<StickerBuilder>()

for ((index, value) in emotes.withIndex()) {
for (emote in emotes) {
val stickerBuilder = Indexables.stickerBuilder()
.setName("$index")
.setUrl(String.format(STICKER_URL_PATTERN, value))
.setImage(String.format(STICKER_IMAGE_URL_PATTERN, value))
.setName(emote["code"])
.setUrl(String.format(STICKER_URL_PATTERN, emote["id"]))
.setImage(emote["imageUrl"])
.setIsPartOf(Indexables.stickerPackBuilder()
.setName(STICKER_PACK_NAME)
.setUrl(STICKER_PACK_URL))
Expand All @@ -68,7 +67,7 @@ class StickerIndexingService() : IntentService("StickerIndexingService") {
return stickerBuilders
}

private fun getIndexableStickerPack(emotes: ArrayList<String>): Indexable {
private fun getIndexableStickerPack(emotes: ArrayList<HashMap<String, String>>): Indexable {
val packImageUri = Uri.parse(STICKER_PACK_IMAGE).toString()
val stickerBuilders = getStickerBuilders(emotes)
return Indexables.stickerPackBuilder()
Expand Down
14 changes: 12 additions & 2 deletions lib/models/emote.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:bttvstickers/constants.dart';
import 'package:bttvstickers/models/json_serializable.dart';

class Emote {
class Emote implements JsonSerializable {
final String id;
final String code;
final String imageType;
Expand All @@ -13,7 +14,16 @@ class Emote {
id: json['id'],
code: json['code'],
imageType: json['imageType'],
imageUrl: "$kEmoteCdnUrl/${json['id']}/3x",
imageUrl: "$kEmoteCdnUrl/${json['id']}/3x.${json['imageType']}",
);
}

toJson() {
return {
'id': id,
'code': code,
'imageType': imageType,
'imageUrl': imageUrl
};
}
}
41 changes: 27 additions & 14 deletions lib/models/pack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Pack extends ChangeNotifier implements JsonSerializable {
List<String> _added = [];
List<String> _selected = [];
List<Emote> _added = [];
List<Emote> _selected = [];
String _fileName = kPackFileName;
MethodChannel _platform = const MethodChannel(kStickerIndexingChannel);

Pack() {
fromJson(getJsonFromFile(_fileName));
}

List<String> get selected => _selected;
List<String> get added => _added;
List<Emote> get selected => _selected;
List<Emote> get added => _added;

toggleSelection(Emote emote) {
if (_added.contains(emote.id)) {
_added.removeWhere((emoteId) => emoteId == emote.id);
if (isEmoteAdded(emote)) {
_added.removeWhere((e) => e.id == emote.id);
_selected += _added;
_added.clear();
} else if (!_selected.contains(emote.id)) {
_selected.add(emote.id);
} else if (!isEmoteSelected(emote)) {
_selected.add(emote);
} else {
_selected.removeWhere((emoteId) => emoteId == emote.id);
_selected.removeWhere((e) => e.id == emote.id);
}
notifyListeners();
}
Expand All @@ -36,8 +36,8 @@ class Pack extends ChangeNotifier implements JsonSerializable {
_selected.clear();
saveModelToJsonFile(this, _fileName);
_platform.invokeMethod(
"generatePack",
_added,
'generatePack',
_added.map((e) => e.toJson()).toList(),
);
notifyListeners();
}
Expand All @@ -46,19 +46,32 @@ class Pack extends ChangeNotifier implements JsonSerializable {
_selected.clear();
_added.clear();
saveModelToJsonFile(this, _fileName);
_platform.invokeMethod("generatePack", []);
_platform.invokeMethod('generatePack', []);
notifyListeners();
}

isEmoteAdded(Emote emote) {
var item = _added.firstWhere((e) => e.id == emote.id, orElse: () => null);
return item != null;
}

isEmoteSelected(Emote emote) {
var item =
_selected.firstWhere((e) => e.id == emote.id, orElse: () => null);
return item != null;
}

fromJson(Future<Map<String, dynamic>> futureJson) async {
var json = await futureJson;
_added = (json['added'] ?? []).cast<String>();
_added = ((json['emotes'] ?? []) as List)
.map((item) => Emote.fromJson(item))
.toList();
notifyListeners();
}

toJson() {
return {
'added': _added,
'emotes': _added.map((e) => e.toJson()).toList(),
};
}
}
4 changes: 2 additions & 2 deletions lib/widgets/emote_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ class _EmoteListState extends State<EmoteList> {
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => EmoteTile(
emote: _emotes[index],
selected: pack.selected.contains(_emotes[index].id),
added: pack.added.contains(_emotes[index].id),
selected: pack.isEmoteSelected(_emotes[index]),
added: pack.isEmoteAdded(_emotes[index]),
),
childCount: _emotes.length,
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
version: 1.0.1+1

environment:
sdk: ">=2.7.0 <3.0.0"
Expand Down

0 comments on commit 7f813fd

Please sign in to comment.