319 lines
8.8 KiB
Dart
319 lines
8.8 KiB
Dart
|
|
import 'dart:convert';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
|
import 'package:flutter_wisetronic/generated/l10n.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:hive/hive.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
import 'package:universal_io/io.dart';
|
|
import '../constants.dart';
|
|
import 'util_web.dart' if (dart.library.io) 'util_io.dart';
|
|
|
|
import '../routes.dart';
|
|
import 'http_util.dart';
|
|
|
|
typedef void OnSuccess(Response response);
|
|
typedef void OnError(dynamic error);
|
|
typedef void OnComplete(dynamic data);
|
|
|
|
typedef void OnOk();
|
|
|
|
class Utils {
|
|
|
|
static bool equalsIgnoreCase(String a, String b) =>
|
|
(a == null && b == null) ||
|
|
(a != null && b != null && a.toLowerCase() == b.toLowerCase());
|
|
|
|
static int selectionsContains(Map<String, dynamic> selections, String key, String name) {
|
|
if (selections.containsKey(key.toUpperCase())) {
|
|
for (var i = 0; i < (selections[key.toUpperCase()] as List).length; i++) {
|
|
Map<String, dynamic> item = (selections[key.toUpperCase()] as List)[i];
|
|
if (Utils.equalsIgnoreCase(item['name'], name)) {
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static List<String> getSelectedAttributeValue(Map<String, dynamic> selections, String key) {
|
|
List<String> valueArr = [];
|
|
if (selections.containsKey(key.toUpperCase())) {
|
|
for (var i = 0; i < (selections[key.toUpperCase()] as List).length; i++) {
|
|
valueArr.add((selections[key.toUpperCase()][i]['name'] as String).toLowerCase());
|
|
}
|
|
}
|
|
return valueArr;
|
|
}
|
|
|
|
static bool selectionsNotEmptyAt(Map<String, dynamic> selections, String key) {
|
|
if (selections.containsKey(key.toUpperCase()) && (selections[key.toUpperCase()] as List).length > 0) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static Map<String, dynamic> stringToJson(String string) {
|
|
if (string == null || string.isEmpty) {
|
|
return null;
|
|
}
|
|
return json.decode(string);
|
|
}
|
|
|
|
static void jsonPrettyPrint(Map<String, dynamic> map) {
|
|
JsonEncoder encoder = new JsonEncoder.withIndent(' ');
|
|
String prettyPrint = encoder.convert(map);
|
|
debugPrint(prettyPrint);
|
|
}
|
|
|
|
static launchURL(String url) async {
|
|
if (await canLaunch(url)) {
|
|
await launch(url);
|
|
} else {
|
|
throw 'Could not launch $url';
|
|
}
|
|
}
|
|
|
|
static String getPlatformName() {
|
|
String platformName = '';
|
|
if (kIsWeb) {
|
|
platformName = 'web';
|
|
} else if (Platform.isAndroid) {
|
|
platformName = 'android';
|
|
} else if (Platform.isIOS) {
|
|
platformName = 'ios';
|
|
}
|
|
return platformName;
|
|
}
|
|
|
|
static Future<Box> getBox() async {
|
|
return Util.getBox();
|
|
}
|
|
|
|
static Widget imageLoadingIndicator() {
|
|
return SizedBox(
|
|
width: 30,
|
|
height: 30,
|
|
child: Container(
|
|
child: CupertinoActivityIndicator(),
|
|
color: Colors.transparent,
|
|
),
|
|
);
|
|
}
|
|
|
|
static String safePhoneNumber(String phone) {
|
|
if (phone.length < 8) {
|
|
return phone;
|
|
}
|
|
String start = phone.substring(0, 3);
|
|
String end = phone.substring(phone.length - 3);
|
|
return '${start}****${end}';
|
|
}
|
|
|
|
static String safeString(String string) {
|
|
if (string == null || string.length == 0) {
|
|
return '';
|
|
}
|
|
String start = string.substring(0, 1);
|
|
String end = string.substring(string.length - 1);
|
|
return '${start}****${end}';
|
|
}
|
|
|
|
static String smartRound(double amount, int decimalPlace) {
|
|
double a = double.parse(amount.toStringAsFixed(decimalPlace));
|
|
double b = double.parse(amount.toStringAsFixed(0));
|
|
if (a - b == 0) {
|
|
return amount.toStringAsFixed(0);
|
|
}
|
|
return amount.toStringAsFixed(decimalPlace);
|
|
}
|
|
|
|
static void createOrUpdateStripePaymentMethod(
|
|
String paymentMethodId,
|
|
String cardBrand,
|
|
String paymentMethodType,
|
|
String cardCountry,
|
|
int cardExpMonth,
|
|
int cardExpYear,
|
|
String cardFunding,
|
|
String cardLast4,
|
|
) {
|
|
HttpUtil.httpPost('v1/create-update-stripe-payment-method', (response) {
|
|
if (response.statusCode == 200) {
|
|
print('create or update customer stripe payment method success. ${response.data}');
|
|
}
|
|
},
|
|
body: {
|
|
'payment_method_id': paymentMethodId,
|
|
'card_brand': cardBrand,
|
|
'payment_method_type': paymentMethodType,
|
|
'card_country': cardCountry,
|
|
'card_exp_month': cardExpMonth,
|
|
'card_exp_year': cardExpYear,
|
|
'card_funding': cardFunding,
|
|
'card_last4': cardLast4,
|
|
},
|
|
isFormData: true,
|
|
).catchError((error) {
|
|
print('Error: ${error}');
|
|
});
|
|
}
|
|
|
|
static showSubmitDialog(BuildContext context) {
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
content: WillPopScope(
|
|
child: Container(
|
|
width: 300.0,
|
|
height: 150.0,
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: <Widget>[
|
|
SpinKitThreeBounce(
|
|
color: Colors.lightBlueAccent,
|
|
size: 30.0,
|
|
),
|
|
Text(
|
|
S.of(context).submitting,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
onWillPop: () async {
|
|
Fluttertoast.showToast(
|
|
msg: S.of(context).submitting_please_wait,
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.CENTER,
|
|
backgroundColor: Colors.red,
|
|
textColor: Colors.white
|
|
);
|
|
return false;
|
|
}
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
static showLoadingDialog(BuildContext context, {String message}) {
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
content: WillPopScope(
|
|
child: Container(
|
|
width: 80.0,
|
|
height: 80.0,
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: <Widget>[
|
|
SpinKitThreeBounce(
|
|
color: Colors.lightBlueAccent,
|
|
size: 30.0,
|
|
),
|
|
Text(
|
|
message != null ? message : S.of(context).recalculating,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
onWillPop: () async {
|
|
Fluttertoast.showToast(
|
|
msg: S.of(context).loading_please_wait,
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.CENTER,
|
|
backgroundColor: Colors.red,
|
|
textColor: Colors.white
|
|
);
|
|
return false;
|
|
}
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
static getTitleFromBody(String body) {
|
|
if (body.length <= 30) {
|
|
return body;
|
|
}
|
|
return body.substring(0, 29);
|
|
}
|
|
|
|
static void getMiniLink(String realLink, OnSuccess onSuccess, OnError onError) {
|
|
HttpUtil.httpPost('get-minilink/', (response) {
|
|
onSuccess(response);
|
|
},
|
|
body: {
|
|
'link': realLink
|
|
},
|
|
isFormData: true,
|
|
).catchError((error) {
|
|
onError(error);
|
|
});
|
|
}
|
|
|
|
static String getOs({bool checkWeb = false}) {
|
|
if (checkWeb && kIsWeb) {
|
|
return 'web';
|
|
}
|
|
if (Platform.isAndroid) {
|
|
return 'android';
|
|
}
|
|
if (Platform.isIOS) {
|
|
return 'ios';
|
|
}
|
|
if (Platform.isLinux) {
|
|
return 'ubuntu';
|
|
}
|
|
if (Platform.isMacOS) {
|
|
return 'mac';
|
|
}
|
|
if (Platform.isWindows) {
|
|
return 'windows';
|
|
}
|
|
return 'web';
|
|
}
|
|
|
|
static int getOsFontHex(String os) {
|
|
if (os == 'mac') {
|
|
return Constants.FONT_APPLE;
|
|
}
|
|
if (os == 'windows') {
|
|
return Constants.FONT_WINDOWS;
|
|
}
|
|
if (os == 'ubuntu') {
|
|
return Constants.FONT_UBUNTU;
|
|
}
|
|
if (os == 'android') {
|
|
return Constants.FONT_ANDROID;
|
|
}
|
|
if (os == 'ios') {
|
|
return Constants.FONT_IOS;
|
|
}
|
|
return Constants.FONT_TOPTONS;
|
|
}
|
|
}
|
|
|
|
class RuntimeError extends Error{
|
|
final int code;
|
|
final String message;
|
|
RuntimeError(this.message, {this.code});
|
|
String toString() => "Runtime Error: $message";
|
|
} |