Skip to content

Commit

Permalink
Merge pull request #245 from sliverappbar/init
Browse files Browse the repository at this point in the history
init: resolve issues of initialization process
  • Loading branch information
violet-dev authored Oct 15, 2023
2 parents 15d7966 + 27c44df commit a7e2382
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 47 deletions.
14 changes: 14 additions & 0 deletions android/app/src/main/kotlin/xyz/project/violet/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.WindowManager
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugins.GeneratedPluginRegistrant
Expand All @@ -17,6 +18,7 @@ class MainActivity : FlutterFragmentActivity() {
private val VOLUME_CHANNEL = "xyz.project.violet/volume"
private val NATIVELIBDIR_CHANNEL = "xyz.project.violet/nativelibdir"
private val EXTERNAL_STORAGE_DIRECTORY_CHANNEL = "xyz.project.violet/externalStorageDirectory"
private val MISC_CHANNEL = "xyz.project.violet/misc"

private val EXTERNAL_STORAGE_DIRECTORY_METHODS = mapOf(
"getExternalStorageDirectory" to MethodCallHandler { call, result ->
Expand Down Expand Up @@ -76,6 +78,13 @@ class MainActivity : FlutterFragmentActivity() {
result.notImplemented()
}
}

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, MISC_CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"finishMainActivity" -> finishMainActivity(call, result)
else -> result.notImplemented()
}
}
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
Expand All @@ -86,4 +95,9 @@ class MainActivity : FlutterFragmentActivity() {

return super.onKeyDown(keyCode, event)
}

private fun finishMainActivity(call: MethodCall, result: MethodChannel.Result) {
finish()
result.success(null)
}
}
3 changes: 2 additions & 1 deletion assets/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
3 changes: 2 additions & 1 deletion assets/locale/eo.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
3 changes: 2 additions & 1 deletion assets/locale/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
3 changes: 2 additions & 1 deletion assets/locale/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
3 changes: 2 additions & 1 deletion assets/locale/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "실험실",
"realtimeuserrecord": "실시간 유저 레코드",
"manualupdate": "수동 업데이트",
"cannotuseios": "iOS에선 사용할 수 없어요 :("
"cannotuseios": "iOS에선 사용할 수 없어요 :(",
"exitTheApp": "앱 종료하기"
}
3 changes: 2 additions & 1 deletion assets/locale/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
3 changes: 2 additions & 1 deletion assets/locale/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
3 changes: 2 additions & 1 deletion assets/locale/zh_Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
3 changes: 2 additions & 1 deletion assets/locale/zh_Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@
"lab": "Laboratory",
"realtimeuserrecord": "Real Time User Record",
"manualupdate": "Manual Update",
"cannotuseios": "You cannot use update on iOS :("
"cannotuseios": "You cannot use update on iOS :(",
"exitTheApp": "Exit the app"
}
28 changes: 23 additions & 5 deletions lib/component/hitomi/population.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/services.dart';
import 'package:violet/database/query.dart';
Expand All @@ -15,18 +16,35 @@ class Population {
String data;

if (Platform.environment.containsKey('FLUTTER_TEST')) {
var file = File('/home/ubuntu/violet/assets/rank/population.json');
final file = File('/home/ubuntu/violet/assets/rank/population.json');
data = await file.readAsString();
} else {
data = await rootBundle.loadString('assets/rank/population.json');
}

List<dynamic> dataPopulation = json.decode(data);
population = <int, int>{};
Future<Map<int, int>> decodeJsonData() async {
final population = <int, int>{};

for (int i = 0; i < dataPopulation.length; i++) {
population[dataPopulation[i] as int] = i;
json.decode(
data,
reviver: (keyObject, valueObject) {
if (keyObject == null) {
return null;
}

int key = keyObject as int;
int value = (valueObject as num).toInt();

population[value] = key;

return null;
},
);

return population;
}

population = await Isolate.run(decodeJsonData);
}

static void sortByPopulation(List<QueryResult> qr) {
Expand Down
32 changes: 24 additions & 8 deletions lib/component/hitomi/related.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/services.dart';

Expand All @@ -13,20 +14,35 @@ class Related {
String data;

if (Platform.environment.containsKey('FLUTTER_TEST')) {
var file = File('/home/ubuntu/violet/assets/rank/related.json');
final file = File('/home/ubuntu/violet/assets/rank/related.json');
data = await file.readAsString();
} else {
data = await rootBundle.loadString('assets/rank/related.json');
}

Map<String, dynamic> dataMap = json.decode(data);

related = <int, List<int>>{};
Future<Map<int, List<int>>> decodeJsonData() async {
final related = <int, List<int>>{};

json.decode(
data,
reviver: (key, value) {
if (key == null) {
return value;
}

if (key is String) {
related[int.parse(key)] = (value as List<dynamic>).cast();
return null;
} else {
return value;
}
},
);

return related;
}

dataMap.entries.forEach((element) {
related[int.parse(element.key)] =
(element.value as List<dynamic>).map((e) => e as int).toList();
});
related = await Isolate.run(decodeJsonData);
}

static bool existsRelated(int articleId) {
Expand Down
55 changes: 41 additions & 14 deletions lib/component/hitomi/tag_translate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/services.dart';
import 'package:tuple/tuple.dart';
Expand All @@ -21,25 +22,51 @@ class TagTranslate {
String data;

if (Platform.environment.containsKey('FLUTTER_TEST')) {
var file = File('/home/ubuntu/violet/assets/locale/tag/korean.json');
final file = File('/home/ubuntu/violet/assets/locale/tag/korean.json');
data = await file.readAsString();
} else {
data = await rootBundle
.loadString('assets/locale/tag/$defaultLanguage.json');
}
Map<String, dynamic> result = json.decode(data);

_translateMap = <String, String>{};
_reverseAndroMap = <String, String>{};

result.entries.forEach((element) {
if (element.value.toString().trim() == '') return;
if (_translateMap.containsKey(element.key)) return;
_translateMap[element.key] = element.value as String;
_reverseAndroMap[disassembly((element.value as String)
.replaceAll('female:', '')
.replaceAll('male:', ''))] = element.key;
});

Future<(Map<String, String>, Map<String, String>)> decodeJsonData() async {
final translateMap = <String, String>{};
final reverseAndroMap = <String, String>{};

json.decode(
data,
reviver: (keyObject, valueObject) {
if (keyObject == null) {
return null;
}

final key = keyObject.toString();
final value = valueObject.toString();

if (value.trim().isEmpty) {
return null;
}

if (!translateMap.containsKey(key)) {
return null;
}

translateMap[key] = value;
reverseAndroMap[disassembly(value)
.replaceAll('female:', '')
.replaceAll('male:', '')] = key;

return null;
},
);

return (translateMap, reverseAndroMap);
}

final (translateMap, reverseAndroMap) = await Isolate.run(decodeJsonData);

_translateMap = translateMap;
_reverseAndroMap = reverseAndroMap;
}

static String of(String classification, String key) {
Expand Down
Loading

0 comments on commit a7e2382

Please sign in to comment.