2022-08-06 02:22:41 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2021-02-12 21:31:42 +01:00
|
|
|
import 'dart:convert';
|
2021-01-26 21:00:26 +01:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math';
|
2021-09-12 06:47:50 +02:00
|
|
|
// import 'package:audioplayers/audio_cache.dart';
|
|
|
|
// import 'package:audioplayers/audioplayers.dart';
|
2022-06-17 01:13:14 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-01-26 21:00:26 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-02-02 20:33:09 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2022-11-30 05:22:37 +01:00
|
|
|
|
2021-01-26 21:00:26 +01:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:gecko/globals.dart';
|
2022-09-12 12:04:08 +02:00
|
|
|
import 'package:gecko/providers/substrate_sdk.dart';
|
2022-09-11 13:14:52 +02:00
|
|
|
import 'package:gecko/providers/wallet_options.dart';
|
2021-12-20 21:33:03 +01:00
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
2022-05-28 21:15:47 +02:00
|
|
|
import 'package:flutter/foundation.dart' show kDebugMode, kIsWeb;
|
2021-12-20 21:33:03 +01:00
|
|
|
import 'package:path_provider/path_provider.dart' as pp;
|
2021-12-23 15:13:58 +01:00
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
2022-05-28 19:13:30 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2022-05-27 16:58:03 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
class HomeProvider with ChangeNotifier {
|
2021-12-23 12:36:09 +01:00
|
|
|
bool? isSearching;
|
2021-11-14 19:21:20 +01:00
|
|
|
Icon searchIcon = const Icon(Icons.search);
|
|
|
|
final TextEditingController searchQuery = TextEditingController();
|
2021-02-16 04:57:13 +01:00
|
|
|
Widget appBarTitle = Text('Ğecko', style: TextStyle(color: Colors.grey[850]));
|
2022-06-17 01:13:14 +02:00
|
|
|
String homeMessage = "loading".tr();
|
|
|
|
String defaultMessage = "noLizard".tr();
|
2022-12-07 23:10:28 +01:00
|
|
|
bool isWalletBoxInit = false;
|
2021-02-22 08:49:29 +01:00
|
|
|
|
2021-12-20 21:33:03 +01:00
|
|
|
Future<void> initHive() async {
|
2021-12-23 12:36:09 +01:00
|
|
|
late Directory hivePath;
|
2021-12-20 21:33:03 +01:00
|
|
|
|
2021-12-21 06:56:42 +01:00
|
|
|
if (!kIsWeb) {
|
|
|
|
if (Platform.isLinux || Platform.isMacOS) {
|
|
|
|
final home = Platform.environment['HOME'];
|
|
|
|
hivePath = Directory('$home/.gecko/db');
|
|
|
|
} else if (Platform.isWindows) {
|
|
|
|
final home = Platform.environment['UserProfile'];
|
|
|
|
hivePath = Directory('$home/.gecko/db');
|
|
|
|
} else if (Platform.isAndroid || Platform.isIOS) {
|
|
|
|
final home = await pp.getApplicationDocumentsDirectory();
|
|
|
|
hivePath = Directory('${home.path}/db');
|
|
|
|
}
|
|
|
|
if (!await hivePath.exists()) {
|
|
|
|
await hivePath.create(recursive: true);
|
|
|
|
}
|
|
|
|
await Hive.initFlutter(hivePath.path);
|
|
|
|
} else {
|
|
|
|
await Hive.initFlutter();
|
2021-12-20 21:33:03 +01:00
|
|
|
}
|
2022-05-28 19:13:30 +02:00
|
|
|
|
|
|
|
// Init app folders
|
|
|
|
final documentDir = await getApplicationDocumentsDirectory();
|
|
|
|
imageDirectory = Directory('${documentDir.path}/images');
|
|
|
|
|
|
|
|
if (!await imageDirectory.exists()) {
|
|
|
|
await imageDirectory.create();
|
|
|
|
}
|
2021-12-20 21:33:03 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:04:08 +02:00
|
|
|
Future changeCurrencyUnit(BuildContext context) async {
|
|
|
|
final sub = Provider.of<SubstrateSdk>(context, listen: false);
|
2022-12-06 04:33:10 +01:00
|
|
|
final walletOptions =
|
|
|
|
Provider.of<WalletOptionsProvider>(context, listen: false);
|
2022-09-11 13:14:52 +02:00
|
|
|
final bool isUdUnit = configBox.get('isUdUnit') ?? false;
|
|
|
|
await configBox.put('isUdUnit', !isUdUnit);
|
2022-12-06 04:33:10 +01:00
|
|
|
walletOptions.balanceCache = {};
|
2022-09-12 12:04:08 +02:00
|
|
|
sub.getBalanceRatio();
|
2022-09-11 13:14:52 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:03:05 +01:00
|
|
|
Future<String> getAppVersion() async {
|
2021-04-19 23:22:45 +02:00
|
|
|
String version;
|
|
|
|
String buildNumber;
|
2021-12-23 15:13:58 +01:00
|
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
|
|
version = packageInfo.version;
|
2022-05-28 21:15:47 +02:00
|
|
|
buildNumber = kDebugMode
|
|
|
|
? packageInfo.buildNumber
|
|
|
|
: (int.parse(packageInfo.buildNumber) - 1000).toString();
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
notifyListeners();
|
2022-08-06 02:22:41 +02:00
|
|
|
return '$version+$buildNumber';
|
2021-01-26 21:00:26 +01:00
|
|
|
}
|
|
|
|
|
2022-05-29 04:30:03 +02:00
|
|
|
Future changeMessage(String newMessage, int seconds) async {
|
|
|
|
homeMessage = newMessage;
|
|
|
|
notifyListeners();
|
|
|
|
await Future.delayed(Duration(seconds: seconds));
|
2022-06-01 21:00:17 +02:00
|
|
|
if (seconds != 0) homeMessage = defaultMessage;
|
2022-05-29 04:30:03 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-05-27 06:11:09 +02:00
|
|
|
Future<List?> getValidEndpoints() async {
|
2022-05-28 00:04:37 +02:00
|
|
|
await configBox.delete('endpoint');
|
2022-07-16 17:25:58 +02:00
|
|
|
if (!configBox.containsKey('autoEndpoint')) {
|
|
|
|
configBox.put('autoEndpoint', true);
|
|
|
|
}
|
2022-05-28 00:04:37 +02:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
List listEndpoints = [];
|
2022-05-27 11:22:43 +02:00
|
|
|
if (!configBox.containsKey('endpoint') ||
|
|
|
|
configBox.get('endpoint') == [] ||
|
|
|
|
configBox.get('endpoint') == '') {
|
2022-08-06 02:22:41 +02:00
|
|
|
listEndpoints = await rootBundle
|
2022-05-27 06:11:09 +02:00
|
|
|
.loadString('config/gdev_endpoints.json')
|
|
|
|
.then((jsonStr) => jsonDecode(jsonStr));
|
2022-08-06 02:22:41 +02:00
|
|
|
listEndpoints.shuffle();
|
|
|
|
configBox.put('endpoint', listEndpoints);
|
2022-05-27 06:11:09 +02:00
|
|
|
}
|
2021-02-12 21:31:42 +01:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
log.i('ENDPOINT: $listEndpoints');
|
|
|
|
return listEndpoints;
|
2021-01-26 21:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T getRandomElement<T>(List<T> list) {
|
2021-02-16 04:57:13 +01:00
|
|
|
final random = Random();
|
2021-01-26 21:00:26 +01:00
|
|
|
var i = random.nextInt(list.length);
|
|
|
|
return list[i];
|
|
|
|
}
|
2021-02-16 04:57:13 +01:00
|
|
|
|
|
|
|
void handleSearchStart() {
|
|
|
|
isSearching = true;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:47:50 +02:00
|
|
|
// void playSound(String customSound, double volume) async {
|
|
|
|
// await player.play('$customSound.wav',
|
|
|
|
// volume: volume, mode: PlayerMode.LOW_LATENCY, stayAwake: false);
|
|
|
|
// }
|
2021-04-04 15:35:38 +02:00
|
|
|
|
2022-09-09 01:12:17 +02:00
|
|
|
void reload() {
|
2021-04-05 20:48:28 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-01-26 21:00:26 +01:00
|
|
|
}
|