mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 11:37:56 +00:00
initialized Application theme
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class CacheHelper {
|
||||
static late SharedPreferences sharedPreferences;
|
||||
|
||||
static init() async {
|
||||
sharedPreferences = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
static Object? getData({
|
||||
required String key,
|
||||
}) async {
|
||||
return sharedPreferences.get(key);
|
||||
}
|
||||
|
||||
static Future setData({
|
||||
required String key,
|
||||
required value,
|
||||
}) async {
|
||||
if (value is String) return await sharedPreferences.setString(key, value);
|
||||
if (value is int) return await sharedPreferences.setInt(key, value);
|
||||
if (value is bool) return await sharedPreferences.setBool(key, value);
|
||||
return await sharedPreferences.setDouble(key, value);
|
||||
}
|
||||
|
||||
static dynamic removeData({
|
||||
required String key,
|
||||
}) async {
|
||||
return sharedPreferences.remove(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
// import 'dart:io';
|
||||
// import 'dart:math';
|
||||
// import 'package:dio/dio.dart';
|
||||
// import 'package:file_picker/file_picker.dart';
|
||||
// import 'package:flutter/material.dart';
|
||||
// import 'package:flutter/services.dart';
|
||||
//
|
||||
// import 'package:path_provider/path_provider.dart';
|
||||
//
|
||||
// class FileHelper {
|
||||
// static Future<Map<String, Uint8List>> takePicture() async {
|
||||
// try {
|
||||
// XFile? pickedImage = await ImagePicker().pickImage(source: ImageSource.camera);
|
||||
// if (pickedImage != null) {
|
||||
// final fileTemp = File(pickedImage.path);
|
||||
// final bytes = fileTemp.readAsBytesSync();
|
||||
// return {pickedImage.path: bytes};
|
||||
// } else {
|
||||
// return {};
|
||||
// }
|
||||
// } on PlatformException catch (_) {
|
||||
// return {};
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// static Future<Map<String, Uint8List>> pickFile({bool gallery = false}) async {
|
||||
// try {
|
||||
// FilePickerResult? pickedFile;
|
||||
// XFile? pickedFromGallery;
|
||||
// if (gallery) {
|
||||
// final picker = ImagePicker();
|
||||
// pickedFromGallery =
|
||||
// await picker.pickImage(source: ImageSource.gallery, requestFullMetadata: false);
|
||||
// } else {
|
||||
// pickedFile = await FilePicker.platform.pickFiles(
|
||||
// allowedExtensions: ['png', 'jpg', 'jpeg', 'pdf'],
|
||||
// type: FileType.custom,
|
||||
// withReadStream: true,
|
||||
// );
|
||||
// }
|
||||
// if (pickedFile != null) {
|
||||
// String path = pickedFile.files.single.path ?? '';
|
||||
// final fileTemp = File(path);
|
||||
// final bytes = fileTemp.readAsBytesSync();
|
||||
// if (path.isEmpty) {
|
||||
// return {};
|
||||
// }
|
||||
// return {path: bytes};
|
||||
// } else if (pickedFromGallery != null) {
|
||||
// String path = pickedFromGallery.path;
|
||||
// final fileTemp = File(path);
|
||||
// final bytes = fileTemp.readAsBytesSync();
|
||||
// if (path.isEmpty) {
|
||||
// return {};
|
||||
// }
|
||||
// return {path: bytes};
|
||||
// } else {
|
||||
// return {};
|
||||
// }
|
||||
// } on PlatformException catch (_) {
|
||||
// return {};
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Future<String> downloadFile(String url) async {
|
||||
// String filePath;
|
||||
// try {
|
||||
// filePath = await _prepareSaveDir(url);
|
||||
// await HTTPService()
|
||||
// .downloadRequest(path: url, savePath: filePath, expectedResponseModel: (json) {});
|
||||
// } catch (err) {
|
||||
// CustomSnackBar.displaySnackBar('Something went wrong please try again!');
|
||||
// return '';
|
||||
// }
|
||||
// return filePath;
|
||||
// }
|
||||
//
|
||||
// String getRandomString(int length) {
|
||||
// const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
|
||||
// Random rnd = Random();
|
||||
//
|
||||
// return String.fromCharCodes(
|
||||
// Iterable.generate(length, (_) => chars.codeUnitAt(rnd.nextInt(chars.length))));
|
||||
// }
|
||||
//
|
||||
// Future<String> _prepareSaveDir(String url) async {
|
||||
// try {
|
||||
// final localPath = (await _findLocalPath(url))!;
|
||||
//
|
||||
// final savedDir = Directory(localPath);
|
||||
// bool hasExisted = await savedDir.exists();
|
||||
// if (!hasExisted) {
|
||||
// await File(savedDir.path).create();
|
||||
// }
|
||||
// return savedDir.path;
|
||||
// } catch (err) {
|
||||
// return '';
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Future<String?> _findLocalPath(String url) async {
|
||||
// String fileName = getRandomString(10);
|
||||
// String fileNameWithExtension = url.contains('pdf') ? '$fileName.pdf' : '$fileName.jpeg';
|
||||
//
|
||||
// if (Platform.isAndroid) {
|
||||
// return "/sdcard/download/$fileNameWithExtension";
|
||||
// } else {
|
||||
// var directory = await getApplicationDocumentsDirectory();
|
||||
// return '${directory.path}${Platform.pathSeparator}$fileNameWithExtension';
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// static Future<Uint8List?> fetchPdfContent(final String url) async {
|
||||
// try {
|
||||
// final Response<List<int>> response = await Dio().get<List<int>>(
|
||||
// url,
|
||||
// options: Options(responseType: ResponseType.bytes),
|
||||
// );
|
||||
// return Uint8List.fromList(response.data ?? []);
|
||||
// } catch (e) {
|
||||
// debugPrint(e.toString());
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LifecycleEventHandler extends WidgetsBindingObserver {
|
||||
final AsyncCallback resumeCallBack;
|
||||
final AsyncCallback suspendingCallBack;
|
||||
final AsyncCallback onPauseCallBack;
|
||||
final AsyncCallback inactiveCallBack;
|
||||
|
||||
LifecycleEventHandler(
|
||||
{required this.resumeCallBack,
|
||||
required this.suspendingCallBack,
|
||||
required this.onPauseCallBack,
|
||||
required this.inactiveCallBack});
|
||||
|
||||
@override
|
||||
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
|
||||
switch (state) {
|
||||
case AppLifecycleState.resumed:
|
||||
await resumeCallBack();
|
||||
break;
|
||||
case AppLifecycleState.inactive:
|
||||
await inactiveCallBack();
|
||||
break;
|
||||
case AppLifecycleState.paused:
|
||||
await onPauseCallBack();
|
||||
break;
|
||||
case AppLifecycleState.detached:
|
||||
await suspendingCallBack();
|
||||
break;
|
||||
case AppLifecycleState.hidden:
|
||||
// TODO: Handle this case.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class LocalizationService {
|
||||
static saveLocale(Locale locale) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString("Locale Language Code", locale.languageCode);
|
||||
if (locale.countryCode != null) {
|
||||
await prefs.setString("Locale Country Code", locale.countryCode!);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Locale> savedLocale() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
// var savedLanguageCode = prefs.getString(Constants.languageCode);
|
||||
// var savedCountryCode = prefs.getString(Constants.countryCode);
|
||||
var savedLocale = const Locale("ar", "JO");
|
||||
|
||||
// if (savedCountryCode != null && savedLanguageCode != null) {
|
||||
// savedLocale = Locale(savedLanguageCode, savedCountryCode);
|
||||
// }
|
||||
|
||||
return savedLocale;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class StringHelpers {
|
||||
// TODO ( to Mohammad Salameh): convert this to extension method
|
||||
static String enhanceFileName(File file) {
|
||||
var fileName = " ";
|
||||
final filePath = file.path;
|
||||
debugPrint(filePath);
|
||||
final fileStringArray = filePath.split("/");
|
||||
fileName = fileStringArray.last;
|
||||
if (fileName.length > 20) {
|
||||
// crop the name and keep the extension
|
||||
final firstPart = fileName.substring(0, 17);
|
||||
fileName = "$firstPart...";
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
|
||||
static String returnEnglishStringFromArabicNumber(String arabicString) {
|
||||
var englishStringNumber = arabicString;
|
||||
englishStringNumber = englishStringNumber.replaceAll("٠", "0");
|
||||
englishStringNumber = englishStringNumber.replaceAll("١", "1");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٢", "2");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٣", "3");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٤", "4");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٥", "5");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٦", "6");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٧", "7");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٨", "8");
|
||||
englishStringNumber = englishStringNumber.replaceAll("٩", "9");
|
||||
return englishStringNumber;
|
||||
}
|
||||
|
||||
static String returnTheFirstThreeWords(String completeString) {
|
||||
// We use this method to return a nice readable word that is shorter than 26 characters
|
||||
// split the string to substrings with the space as the pattern
|
||||
final arrayOfSubStrings = completeString.split(" ");
|
||||
String newString = "";
|
||||
// keep adding the substrings until 26 is reached
|
||||
var index = 0;
|
||||
while (newString.length < 20 && index < arrayOfSubStrings.length) {
|
||||
newString = "$newString + ${arrayOfSubStrings[index]} ";
|
||||
index++;
|
||||
}
|
||||
return newString;
|
||||
}
|
||||
|
||||
static int getIntFromString(String? numberString) {
|
||||
if (numberString != null) {
|
||||
final numberInt = int.tryParse(numberString);
|
||||
if (numberInt != null) {
|
||||
return numberInt;
|
||||
} else {
|
||||
final numberDouble = double.tryParse(numberString);
|
||||
if (numberDouble != null) {
|
||||
return numberDouble.toInt();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class ParserHelper {
|
||||
//todo : convert to extension
|
||||
static int parseInt(String value) {
|
||||
int result;
|
||||
try {
|
||||
result = int.parse(value);
|
||||
} catch (err) {
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static double parseDouble(String value) {
|
||||
double result;
|
||||
try {
|
||||
result = double.parse(value);
|
||||
} catch (err) {
|
||||
result = 0.0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static String roundNumber(dynamic value) {
|
||||
String valueToString = value.toString();
|
||||
num number = num.tryParse(valueToString.isNotEmpty
|
||||
? valueToString.contains(',')
|
||||
? valueToString.replaceAll(',', '')
|
||||
: valueToString
|
||||
: '0') ??
|
||||
0;
|
||||
int roundedNum = number.round();
|
||||
|
||||
return NumberFormat("#,###,###", 'en_US').format(roundedNum);
|
||||
}
|
||||
|
||||
static String roundArea(dynamic value) {
|
||||
if (value.isNotEmpty && value.contains(' ')) {
|
||||
List<String> split = value!.split(' ');
|
||||
String formattedArea = ParserHelper.roundNumber(split[0]);
|
||||
|
||||
return '$formattedArea ${split[1]}';
|
||||
} else {
|
||||
String valueToString = value.toString();
|
||||
num number = num.tryParse(valueToString.isNotEmpty
|
||||
? valueToString.contains(',')
|
||||
? valueToString.replaceAll(',', '')
|
||||
: valueToString
|
||||
: '0') ??
|
||||
0;
|
||||
int roundedNum = number.round();
|
||||
|
||||
return NumberFormat("#,###,###", 'en_US').format(roundedNum);
|
||||
}
|
||||
}
|
||||
|
||||
static String oneDecimal(num value) {
|
||||
String numWithOneDecimal = value.toStringAsFixed(1);
|
||||
if (numWithOneDecimal.contains('.')) {
|
||||
List<String> numList = [];
|
||||
numList = numWithOneDecimal.split('.');
|
||||
// Example: if the number is 12.0 change it to 12
|
||||
if (numList[1] == '0') {
|
||||
numWithOneDecimal = numList[0];
|
||||
}
|
||||
}
|
||||
return numWithOneDecimal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class PhoneNumberTextInputFormatter extends TextInputFormatter {
|
||||
final String mask;
|
||||
final String separator;
|
||||
PhoneNumberTextInputFormatter({
|
||||
required this.mask,
|
||||
required this.separator,
|
||||
});
|
||||
@override
|
||||
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
|
||||
if (newValue.text.isNotEmpty) {
|
||||
if (newValue.text.length > oldValue.text.length) {
|
||||
if (newValue.text.length > mask.length) return oldValue;
|
||||
if (newValue.text.length < mask.length && mask[newValue.text.length - 1] == separator) {
|
||||
return TextEditingValue(
|
||||
text: '${oldValue.text}$separator${newValue.text.substring(newValue.text.length - 1)}',
|
||||
selection: TextSelection.collapsed(
|
||||
offset: newValue.selection.end + 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return newValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class SharedPreferencesHelper {
|
||||
// isRegistered flag to know if the user has entered his fullname and email or not (is he registered or not)
|
||||
static bool? isRegistered;
|
||||
|
||||
// flags to detect the mode {regular, onboarding, demo}
|
||||
static bool? isDemo;
|
||||
static bool? isOnBoarding;
|
||||
|
||||
// isDemoAndUseToken flag to use the user token in the header of the request in the demo experience, instead of "Demo" header in situations (1. openning the profile request 2. updating th user data -registeration- request)
|
||||
static bool? isDemoAndUseToken;
|
||||
|
||||
// addedFirstProperty flag to detect if the user has added his first property or not
|
||||
static bool? addedFirstProperty;
|
||||
|
||||
// stores the user id value
|
||||
static String? userIdValue;
|
||||
|
||||
// stores the phone number value
|
||||
static String? phoneNumber;
|
||||
|
||||
//stores the country code of the user phone
|
||||
static String? countryCode;
|
||||
|
||||
// static Future<bool?> getIsDemoValue() async {
|
||||
// isDemo = await readBoolFromSP(KeyConstants.isDemoFlag) ?? false;
|
||||
// return isDemo;
|
||||
// }
|
||||
//
|
||||
// static Future<bool?> getIsOnboardingValue() async {
|
||||
// isOnBoarding = await readBoolFromSP(KeyConstants.isOnboardingFlag);
|
||||
// return isOnBoarding;
|
||||
// }
|
||||
|
||||
static saveStringToSP(String key, String value) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(key, value);
|
||||
}
|
||||
|
||||
static saveBoolToSP(String key, bool value) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(key, value);
|
||||
}
|
||||
|
||||
static saveIntToSP(String key, int value) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(key, value);
|
||||
}
|
||||
|
||||
static saveDoubleToSP(String key, double value) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setDouble(key, value);
|
||||
}
|
||||
|
||||
static saveStringListToSP(String key, List<String> value) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(key, value);
|
||||
}
|
||||
|
||||
static Future<String> readStringFromSP(String key) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
String value = prefs.getString(key) ?? '';
|
||||
return value;
|
||||
}
|
||||
|
||||
static Future<bool?> readBoolFromSP(String key) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
bool? value = prefs.getBool(key);
|
||||
return value;
|
||||
}
|
||||
|
||||
static Future<int> readIntFromSP(String key) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
int value = prefs.getInt(key) ?? 0;
|
||||
return value;
|
||||
}
|
||||
|
||||
static Future<List<String>> readStringListFromSP(String key) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
List<String>? value = prefs.getStringList(key) ?? [];
|
||||
return value;
|
||||
}
|
||||
|
||||
static Future<bool> removeValueFromSP(String key) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
class URLLauncher {
|
||||
static Future<void> sendWhatsapp(
|
||||
{String phoneNumber = '971581497062',
|
||||
String text = 'Hi%2C%20I%20would%20like%20to',
|
||||
String from = ''}) async {
|
||||
Uri launchUri;
|
||||
launchUri = Uri.parse("whatsapp://send?phone=%2B$phoneNumber&text=$text");
|
||||
// Launch the App
|
||||
try {
|
||||
// SegmentEvents.sendTrackEvent(SegmentEventModel(
|
||||
// eventName: 'Whatsapp opened', properties: {'From': from}));
|
||||
//
|
||||
// if (await canLaunchUrl(launchUri) == false) {
|
||||
// CustomSnackBar.displaySnackBar('Could not open whatsapp');
|
||||
// return;
|
||||
// }
|
||||
if (Platform.isAndroid) {
|
||||
await launch(
|
||||
launchUri.toString(),
|
||||
);
|
||||
} else {
|
||||
await launchUrl(
|
||||
launchUri,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
// CustomSnackBar.displaySnackBar('Could not open whatsapp');
|
||||
throw 'Could not launch $phoneNumber';
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> sendEmail(
|
||||
{required String email, String subject = '', body = ''}) async {
|
||||
String? encodeQueryParameters(Map<String, String> params) {
|
||||
return params.entries
|
||||
.map((MapEntry<String, String> e) =>
|
||||
'${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
|
||||
.join('&');
|
||||
}
|
||||
|
||||
final emailLaunchUri = Uri(
|
||||
scheme: 'mailto',
|
||||
path: email,
|
||||
query: encodeQueryParameters(<String, String>{
|
||||
subject: body,
|
||||
}),
|
||||
);
|
||||
try {
|
||||
bool canLaunch = await canLaunchUrl(emailLaunchUri);
|
||||
if (canLaunch) {
|
||||
await launchUrl(emailLaunchUri);
|
||||
}
|
||||
} catch (err) {
|
||||
throw 'Could not launch $emailLaunchUri';
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> launch(String url) async {
|
||||
Uri parsedUrl = Uri.parse(url);
|
||||
try {
|
||||
bool canLaunch = await canLaunchUrl(parsedUrl);
|
||||
if (canLaunch) {
|
||||
await launchUrl(
|
||||
parsedUrl,
|
||||
// mode: LaunchMode.externalApplication,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw 'Url launcher error: ${err.toString()}';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user