diff --git a/android/app/src/main/kotlin/com/drakota/bttvstickers/MainActivity.kt b/android/app/src/main/kotlin/com/drakota/bttvstickers/MainActivity.kt index 1c61e3e..e4de9e6 100644 --- a/android/app/src/main/kotlin/com/drakota/bttvstickers/MainActivity.kt +++ b/android/app/src/main/kotlin/com/drakota/bttvstickers/MainActivity.kt @@ -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) + intent.putExtra("emotes", call.arguments as ArrayList>) startService(intent) } } diff --git a/android/app/src/main/kotlin/com/drakota/bttvstickers/StickerIndexingService.kt b/android/app/src/main/kotlin/com/drakota/bttvstickers/StickerIndexingService.kt index 7f4acc5..54b25b3 100644 --- a/android/app/src/main/kotlin/com/drakota/bttvstickers/StickerIndexingService.kt +++ b/android/app/src/main/kotlin/com/drakota/bttvstickers/StickerIndexingService.kt @@ -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> if (emotes.isEmpty()) { // If we are clearing the pack, just clear it and return return @@ -46,19 +45,19 @@ class StickerIndexingService() : IntentService("StickerIndexingService") { } } - private fun getIndexableStickers(emotes: ArrayList): List { + private fun getIndexableStickers(emotes: ArrayList>): List { val stickerBuilders = getStickerBuilders(emotes) return stickerBuilders.map { it.build() } } - private fun getStickerBuilders(emotes: ArrayList): List { + private fun getStickerBuilders(emotes: ArrayList>): List { val stickerBuilders = arrayListOf() - 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)) @@ -68,7 +67,7 @@ class StickerIndexingService() : IntentService("StickerIndexingService") { return stickerBuilders } - private fun getIndexableStickerPack(emotes: ArrayList): Indexable { + private fun getIndexableStickerPack(emotes: ArrayList>): Indexable { val packImageUri = Uri.parse(STICKER_PACK_IMAGE).toString() val stickerBuilders = getStickerBuilders(emotes) return Indexables.stickerPackBuilder() diff --git a/lib/models/emote.dart b/lib/models/emote.dart index 9142e10..1abafe1 100644 --- a/lib/models/emote.dart +++ b/lib/models/emote.dart @@ -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; @@ -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 + }; + } } diff --git a/lib/models/pack.dart b/lib/models/pack.dart index 1d351b8..3c24c6e 100644 --- a/lib/models/pack.dart +++ b/lib/models/pack.dart @@ -6,8 +6,8 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; class Pack extends ChangeNotifier implements JsonSerializable { - List _added = []; - List _selected = []; + List _added = []; + List _selected = []; String _fileName = kPackFileName; MethodChannel _platform = const MethodChannel(kStickerIndexingChannel); @@ -15,18 +15,18 @@ class Pack extends ChangeNotifier implements JsonSerializable { fromJson(getJsonFromFile(_fileName)); } - List get selected => _selected; - List get added => _added; + List get selected => _selected; + List 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(); } @@ -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(); } @@ -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> futureJson) async { var json = await futureJson; - _added = (json['added'] ?? []).cast(); + _added = ((json['emotes'] ?? []) as List) + .map((item) => Emote.fromJson(item)) + .toList(); notifyListeners(); } toJson() { return { - 'added': _added, + 'emotes': _added.map((e) => e.toJson()).toList(), }; } } diff --git a/lib/widgets/emote_list.dart b/lib/widgets/emote_list.dart index efece49..4cbf356 100644 --- a/lib/widgets/emote_list.dart +++ b/lib/widgets/emote_list.dart @@ -148,8 +148,8 @@ class _EmoteListState extends State { 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, ), diff --git a/pubspec.yaml b/pubspec.yaml index 1815ac0..61b2a18 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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"