gecko/lib/providers/home.dart

162 lines
4.7 KiB
Dart
Raw Normal View History

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';
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';
2021-01-26 21:00:26 +01:00
import 'dart:async';
import 'package:gecko/globals.dart';
2021-12-20 21:33:03 +01:00
import 'package:hive_flutter/hive_flutter.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
2021-12-20 21:33:03 +01:00
import 'package:path_provider/path_provider.dart' as pp;
import 'package:package_info_plus/package_info_plus.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]));
Widget appBarExplorer =
Text('Explorateur', style: TextStyle(color: Colors.grey[850]));
bool isFirstBuild = true;
2021-09-12 06:47:50 +02:00
// AudioCache player = AudioCache(prefix: 'sounds/');
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
}
}
Future<String> getAppVersion() async {
2021-04-19 23:22:45 +02:00
String version;
String buildNumber;
PackageInfo packageInfo = await PackageInfo.fromPlatform();
version = packageInfo.version;
buildNumber = packageInfo.buildNumber;
2021-01-26 21:00:26 +01:00
notifyListeners();
return version + '+' + buildNumber;
}
2021-12-23 12:36:09 +01:00
Future<String?> getValidEndpoint() async {
List _listEndpoints = await rootBundle
.loadString('config/gva_endpoints.json')
.then((jsonStr) => jsonDecode(jsonStr));
_listEndpoints.shuffle();
2021-01-26 21:00:26 +01:00
int i = 0;
2021-12-23 12:36:09 +01:00
String? _endpoint;
2021-02-13 01:04:40 +01:00
int _statusCode = 0;
2021-11-14 19:21:20 +01:00
final _client = HttpClient();
_client.connectionTimeout = const Duration(milliseconds: 1000);
2021-01-26 21:00:26 +01:00
do {
i++;
2021-04-02 12:05:37 +02:00
log.d(i.toString() + ' ème essai de recherche de endpoint GVA.');
log.d('Try GVA endpoint: ${_listEndpoints[i - 1]}');
int listLenght = _listEndpoints.length - 1;
if (i > listLenght) {
2021-04-02 12:05:37 +02:00
log.e('NO VALID GVA ENDPOINT FOUND');
_endpoint = 'HS';
break;
}
if (i != 0) {
await Future.delayed(const Duration(milliseconds: 300));
}
try {
2021-02-13 01:04:40 +01:00
final request = await _client.postUrl(Uri.parse(_listEndpoints[i]));
final response = await request.close();
_endpoint = _listEndpoints[i];
2021-02-13 01:04:40 +01:00
_statusCode = response.statusCode;
} on TimeoutException catch (_) {
2021-04-02 12:05:37 +02:00
log.e('This endpoint is timeout, next');
2021-02-13 01:04:40 +01:00
_statusCode = 50;
continue;
} on SocketException catch (_) {
2021-04-02 12:05:37 +02:00
log.e('This endpoint is a bad endpoint, next');
2021-02-13 01:04:40 +01:00
_statusCode = 70;
continue;
} on Exception {
2021-04-02 12:05:37 +02:00
log.e('Unknown error');
2021-02-13 01:04:40 +01:00
_statusCode = 60;
continue;
}
2021-02-13 01:04:40 +01:00
} while (_statusCode != 400);
2021-12-23 12:36:09 +01:00
log.i('ENDPOINT: ' + _endpoint!);
2021-01-26 21:00:26 +01:00
return _endpoint;
}
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-02-16 04:57:13 +01:00
void handleSearchEnd() {
searchIcon = Icon(
Icons.search,
color: Colors.grey[850],
);
appBarTitle = Text('Ğecko', style: TextStyle(color: Colors.grey[850]));
appBarExplorer =
Text('Explorateur', style: TextStyle(color: Colors.grey[850]));
2021-02-16 04:57:13 +01:00
isSearching = false;
searchQuery.clear();
notifyListeners();
}
2021-04-05 20:48:28 +02:00
void snackNode(context) {
if (isFirstBuild) {
String _message;
if (endPointGVA == 'HS') {
_message =
"Aucun noeud Duniter disponible, veuillez réessayer ultérieurement";
} else {
_message = "Vous êtes connecté au noeud\n${endPointGVA.split('/')[2]}";
}
final snackBar = SnackBar(
content: Text(_message), duration: const Duration(seconds: 2));
isFirstBuild = false;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
2021-04-05 20:48:28 +02:00
void rebuildWidget() {
notifyListeners();
}
2021-01-26 21:00:26 +01:00
}