mobileApplicationsKonectarApp/lib/utils/util.dart

113 lines
3.4 KiB
Dart
Raw Permalink Normal View History

2024-01-04 07:06:37 +00:00
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:pwa_ios/model/json_form_data.dart';
import 'package:pwa_ios/model/save_interaction.dart';
import 'package:pwa_ios/viewmodel/interactionprovider.dart';
import 'package:pwa_ios/viewmodel/viewinteractionprovider.dart';
2024-01-04 07:06:37 +00:00
import 'package:shared_preferences/shared_preferences.dart';
Future<bool> isNetworkAvailable() async {
// check if there is a valid network connection
final connectivityResult = await Connectivity().checkConnectivity();
if (connectivityResult != ConnectivityResult.mobile &&
connectivityResult != ConnectivityResult.wifi) {
return false;
}
// check if the network is really connected to Internet
try {
final result = await InternetAddress.lookup('example.com');
if (result.isEmpty || result[0].rawAddress.isEmpty) {
return false;
}
} on SocketException catch (_) {
return false;
}
return true;
}
Future<bool> isPWAInstalled() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool('isInstalled') ?? false;
}
void setPWAInstalled({bool installed = true}) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('isInstalled', installed);
}
bool get isTablet {
final firstView = WidgetsBinding.instance.platformDispatcher.views.first;
final logicalShortestSide =
firstView.physicalSize.shortestSide / firstView.devicePixelRatio;
// print("size:${logicalShortestSide > 600} tablet ");
return logicalShortestSide > 600;
}
Future<String> formJsonForSync() async {
final provider = ViewInteractionProvider();
final prov = InteractionProvider();
List<SaveInteractionFormJson> senSavedList = [];
List<SaveInteraction> savedList = await provider.getAllRecords();
for (var obj in savedList) {
senSavedList.add(prov.formJson(obj));
}
SendSaveJson jsonData = SendSaveJson(savedList: senSavedList);
DataJson dataJson = DataJson(sendSaveJson: jsonData);
String jsonstr = saveFormJsonToJson(jsonData);
print(jsonstr);
return jsonstr;
}
Future<bool> checkRecordsAvailable() async {
final provider = ViewInteractionProvider();
List<SaveInteraction> savedList = await provider.getAllRecords();
return savedList.isNotEmpty;
}
deleteRecords() async {
final provider = ViewInteractionProvider();
List<SaveInteraction> savedList = await provider.getAllRecords();
for (var obj in savedList) {
provider.deleteRecord(obj);
}
}
2024-01-04 07:06:37 +00:00
Future<int> getNextAutoIncrementValue() async {
var counterBox = await Hive.openBox<int>('counterBox');
if (!counterBox.containsKey('counter')) {
counterBox.put('counter', 0);
}
int? counter = counterBox.get('counter');
counterBox.put('counter', counter! + 1);
await counterBox.close();
return counter;
}
class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF$hexColor";
}
return int.parse(hexColor, radix: 16);
}
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
extension Iterables<E> on Iterable<E> {
Map<K, List<E>> groupBy<K>(K Function(E) keyFunction) => fold(
<K, List<E>>{},
(Map<K, List<E>> map, E element) =>
map..putIfAbsent(keyFunction(element), () => <E>[]).add(element));
}