Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init: resolve issues of initialization process #245

Merged
merged 4 commits into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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
@@ -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 ->
@@ -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 {
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -3,6 +3,7 @@

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

import 'package:flutter/services.dart';
import 'package:violet/database/query.dart';
@@ -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) {
32 changes: 24 additions & 8 deletions lib/component/hitomi/related.dart
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

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

import 'package:flutter/services.dart';

@@ -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) {
55 changes: 41 additions & 14 deletions lib/component/hitomi/tag_translate.dart
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

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

import 'package:flutter/services.dart';
import 'package:tuple/tuple.dart';
@@ -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) {
60 changes: 49 additions & 11 deletions lib/pages/database_download/database_download_page.dart
Original file line number Diff line number Diff line change
@@ -4,10 +4,12 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:ui';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -18,6 +20,7 @@ import 'package:violet/database/query.dart';
import 'package:violet/locale/locale.dart';
import 'package:violet/log/log.dart';
import 'package:violet/pages/database_download/decompress.dart';
import 'package:violet/platform/misc.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/variables.dart';
import 'package:violet/version/sync.dart';
@@ -35,16 +38,18 @@ class DataBaseDownloadPage extends StatefulWidget {
}) : super(key: key);

@override
State<DataBaseDownloadPage> createState() => DataBaseDownloadPagepState();
State<DataBaseDownloadPage> createState() => DataBaseDownloadPageState();
}

class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
bool downloading = false;
var baseString = '';
var progressString = '';
var downString = '';
var speedString = '';

var _showCloseAppButton = false;

@override
void initState() {
super.initState();
@@ -95,7 +100,8 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
Timer timer = Timer.periodic(
const Duration(seconds: 1),
(Timer timer) => setState(() {
speedString = '${tlatest / 1024} KB/S';
final speed = tlatest / 1024;
speedString = '${_formatNumberWithComma(speed)} KB/s';
tlatest = tnu;
tnu = 0;
}));
@@ -113,8 +119,10 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
setState(
() {
downloading = true;
progressString = '${((rec / total) * 100).toStringAsFixed(0)}%';
downString = '[${numberWithComma(rec)}/${numberWithComma(total)}]';
final progressPercent = (rec / total) * 100;
progressString = '${_formatNumberWithComma(progressPercent)}%';
downString =
'[${_formatNumberWithComma(rec)}/${_formatNumberWithComma(total)}]';
},
);
});
@@ -163,6 +171,7 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
} else {
setState(() {
baseString = Translations.instance!.trans('dbdcomplete');
_showCloseAppButton = true;
});
}

@@ -194,7 +203,8 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
Timer timer = Timer.periodic(
const Duration(seconds: 1),
(Timer timer) => setState(() {
speedString = '${tlatest / 1024} KB/S';
final speed = tlatest / 1024;
speedString = '${_formatNumberWithComma(speed)} KB/s';
tlatest = tnu;
tnu = 0;
}));
@@ -212,8 +222,10 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
setState(
() {
downloading = true;
progressString = '${((rec / total) * 100).toStringAsFixed(0)}%';
downString = '[${numberWithComma(rec)}/${numberWithComma(total)}]';
final progressPercent = (rec / total) * 100;
progressString = '${_formatNumberWithComma(progressPercent)}%';
downString =
'[${_formatNumberWithComma(rec)}/${_formatNumberWithComma(total)}]';
},
);
});
@@ -239,6 +251,7 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
} else {
setState(() {
baseString = Translations.instance!.trans('dbdcomplete');
_showCloseAppButton = true;
});
}

@@ -486,15 +499,28 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {

setState(() {
baseString = Translations.instance!.trans('dbdcomplete');
_showCloseAppButton = true;
});
break;
}
i++;
}
}

String numberWithComma(int param) {
return NumberFormat('###,###,###,###').format(param).replaceAll(' ', '');
static final _commaFormatter = NumberFormat('#,###.#');

String _formatNumberWithComma(num param) {
return _commaFormatter.format(param).replaceAll(' ', '');
}

Future<void> _exitApplication() async {
if (Platform.isAndroid) {
await PlatformMiscMethods.instance.finishMainActivity();
} else if (Platform.isIOS) {
exit(0);
} else {
ServicesBinding.instance.exitApplication(AppExitType.required);
}
}

@override
@@ -543,7 +569,19 @@ class DataBaseDownloadPagepState extends State<DataBaseDownloadPage> {
),
),
)
: Text(baseString),
: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(baseString),
if (_showCloseAppButton) ...[
const SizedBox(height: 32.0),
ElevatedButton(
onPressed: _exitApplication,
child: Text(Translations.of(context).trans('exitTheApp')),
),
],
],
),
),
);
}
19 changes: 19 additions & 0 deletions lib/platform/misc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'dart:io';

import 'package:flutter/services.dart';

class PlatformMiscMethods {
const PlatformMiscMethods._();

static const instance = PlatformMiscMethods._();

final _methodChannel = const MethodChannel('xyz.project.violet/misc');

Future<void> finishMainActivity() async {
if (!Platform.isAndroid) {
throw UnsupportedError('Android only');
}

await _methodChannel.invokeMethod('finishMainActivity');
}
}