This commit is contained in:
2020-12-23 00:43:59 -05:00
parent 0fd880f57b
commit 86c845b49b
54 changed files with 3638 additions and 107 deletions

View File

@@ -0,0 +1,98 @@
import 'dart:async';
import 'package:flutter/material.dart';
/// Allows the user to close the app by double tapping the back-button.
///
/// You must specify a [SnackBar], so it can be shown when the user taps the
/// back-button. Notice that the value you set for [SnackBar.duration] is going
/// to be considered to decide whether the snack-bar is currently visible or
/// not.
///
/// Since the back-button is an Android feature, this Widget is going to be
/// nothing but the own [child] if the current platform is anything but Android.
class DoubleBackToCloseApp extends StatefulWidget {
/// The [SnackBar] shown when the user taps the back-button.
final SnackBar snackBar;
/// The widget below this widget in the tree.
final Widget child;
/// Creates a widget that allows the user to close the app by double tapping
/// the back-button.
const DoubleBackToCloseApp({
Key key,
@required this.snackBar,
@required this.child,
}) : assert(snackBar != null),
assert(child != null),
super(key: key);
@override
_DoubleBackToCloseAppState createState() => _DoubleBackToCloseAppState();
}
class _DoubleBackToCloseAppState extends State<DoubleBackToCloseApp> {
/// The last time the user tapped Android's back-button.
DateTime _lastTimeBackButtonWasTapped;
/// Returns whether the current platform is Android.
bool get _isAndroid => Theme.of(context).platform == TargetPlatform.android;
/// Returns whether the [DoubleBackToCloseApp.snackBar] is currently visible.
///
/// The snack-bar is going to be considered visible if the duration of the
/// snack-bar is greater than the difference from now to the
/// [_lastTimeBackButtonWasTapped].
///
/// This is not quite accurate since the snack-bar could've been dismissed by
/// the user, so this algorithm needs to be improved, as described in #2.
bool get _isSnackBarVisible =>
(_lastTimeBackButtonWasTapped != null) &&
(widget.snackBar.duration >
DateTime.now().difference(_lastTimeBackButtonWasTapped));
/// Returns whether the next back navigation of this route will be handled
/// internally.
///
/// Returns true when there's a widget that inserted an entry into the
/// local-history of the current route, in order to handle pop. This is done
/// by [Drawer], for example, so it can close on pop.
bool get _willHandlePopInternally =>
ModalRoute.of(context).willHandlePopInternally;
@override
Widget build(BuildContext context) {
_ensureThatContextContainsScaffold();
if (_isAndroid) {
return WillPopScope(
onWillPop: _handleWillPop,
child: widget.child,
);
} else {
return widget.child;
}
}
/// Handles [WillPopScope.onWillPop].
Future<bool> _handleWillPop() async {
if (_isSnackBarVisible || _willHandlePopInternally) {
return true;
} else {
_lastTimeBackButtonWasTapped = DateTime.now();
// Scaffold.of(context).showSnackBar(widget.snackBar);
ScaffoldMessenger.of(context).showSnackBar(widget.snackBar);
return false;
}
}
/// Throws a [FlutterError] if this widget was not wrapped in a [Scaffold].
void _ensureThatContextContainsScaffold() {
if (Scaffold.maybeOf(context) == null) {
throw FlutterError(
'`DoubleBackToCloseApp` must be wrapped in a `Scaffold`.',
);
}
}
}

370
lib/utils/http_util.dart Normal file
View File

@@ -0,0 +1,370 @@
import 'dart:async';
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';
import '../store/store.dart';
import 'utils.dart';
import 'package:hive/hive.dart';
import 'package:universal_io/io.dart';
import '../constants.dart';
typedef void PostCallback(Response response);
String generateSignature(String string, {String key}) {
if (key == null) {
key = Constants.API_SECRET;
}
Hmac mac = new Hmac(sha256, utf8.encode(key));
Digest digest = mac.convert(utf8.encode(string + key));
String base64Mac = base64.encode(utf8.encode("$digest"));
return base64Mac;
}
class HttpUtil {
static String platformName = Utils.getPlatformName();
static final Map<String, String> headers = {
'Accept': 'application/json',
// 'Content-Type': 'application/json',
'Http-Signature': '',
// 'Http-Group-Id': Constants.GROUP_ID,
'Http-Business-Id': '0',
'Http-App-Key': '',
'Http-Contact-Authorization': '',
'Http-Device-Type': Platform.isIOS ? 'ios' : (Platform.isAndroid ? 'android' : 'web'),
'Http-Api-Branch': 'flutter',
'Http-Language-Code': store.state.locale.languageCode,
};
static Future<dynamic> httpGet(String url,
{
Map<String, dynamic> queryParameters,
int businessId = 0,
Function(int, int) receiveProgress,
bool returnError = false,
Map<String, String> additionalHeaders,
}) async {
Map<String, dynamic> localHeaders = json.decode(json.encode(headers));
if (additionalHeaders != null) {
localHeaders.addAll(additionalHeaders);
}
if (!url.startsWith('http')) {
localHeaders['Http-Signature'] = generateSignature(url);
}
localHeaders['Http-Business-Id'] = businessId.toString();
Box box = await Utils.getBox();
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
localHeaders['Http-App-Key'] = platformName;
localHeaders['Http-Device-Type'] = platformName;
String requestUrl = Constants.BASE_API_URL + url;
if (url.startsWith('http')) {
requestUrl = url;
}
Utils.jsonPrettyPrint(queryParameters);
Dio dio = Dio();
try {
Response response = await dio.get(requestUrl,
queryParameters: queryParameters,
options: Options(
headers: localHeaders
),
onReceiveProgress: receiveProgress,
).timeout(const Duration(seconds: 30));
print('HttpGet success. Request: $requestUrl, Response: ${response.statusCode}, Headers: ${response.request.headers}');
// print(response.data);
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
return response.data;
} on DioError catch(e) {
print('error $e');
if (returnError) {
return e;
}
throw e;
}
}
static Future<dynamic> httpPost(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
int businessId = 0,
bool isFormData = false,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
Function(int, int) sendProgress,
Function(int, int) receiveProgress,
bool returnError = false,
}) async {
Map<String, dynamic> localHeaders = json.decode(json.encode(headers));
if (additionalHeaders != null) {
localHeaders.addAll(additionalHeaders);
}
if (!url.startsWith('http')) {
localHeaders['Http-Signature'] = generateSignature(url);
}
localHeaders['Http-Business-Id'] = businessId.toString();
Box box = await Utils.getBox();
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
localHeaders['Http-App-Key'] = platformName;
localHeaders['Http-Device-Type'] = platformName;
String requestUrl = Constants.BASE_API_URL + url;
if (url.startsWith('http')) {
requestUrl = url;
}
// Don't print body will cause upload Multipart file failed
//Utils.jsonPrettyPrint(body);
Dio dio = Dio();
dio.interceptors.add(LogInterceptor());
try {
Response response = await dio.post(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
data: isFormData ? FormData.fromMap(body) : json.encode(body),
options: Options(
headers: localHeaders,
// contentType: isFormData ? Headers.formUrlEncodedContentType : Headers.jsonContentType,
),
onSendProgress: sendProgress,
onReceiveProgress: receiveProgress,
).timeout(Duration(seconds: 30));
print('HttpPost Success. Request: ${response.request.uri}, Response: ${response.statusCode}, Headers: ${response.request.headers}');
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e) {
if (e.response != null) {
print('HttpPost failed. Request: ${e.request.uri}, Headers: ${e.response.request.headers}');
try {
Utils.jsonPrettyPrint(e.response.data);
} catch (err) {
print(e.response.data);
}
}
if (returnError) {
return e;
}
throw e;
}
}
static Future<dynamic> httpPut(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
int businessId = 0,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
Function(int, int) sendProgress,
Function(int, int) receiveProgress,
bool returnError = false,
}) async {
Map<String, dynamic> localHeaders = json.decode(json.encode(headers));
if (additionalHeaders != null) {
localHeaders.addAll(additionalHeaders);
}
if (!url.startsWith('http')) {
localHeaders['Http-Signature'] = generateSignature(url);
}
localHeaders['Http-Business-Id'] = businessId.toString();
Box box = await Utils.getBox();
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
localHeaders['Http-App-Key'] = platformName;
localHeaders['Http-Device-Type'] = platformName;
localHeaders['Content-Type'] = Headers.jsonContentType;
String requestUrl = Constants.BASE_API_URL + url;
if (url.startsWith('http')) {
requestUrl = url;
}
Dio dio = Dio();
try {
Response response = await dio.put(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
data: json.encode(body),
options: Options(
headers: localHeaders,
),
onSendProgress: sendProgress,
onReceiveProgress: receiveProgress,
).timeout(Duration(seconds: 30));
print('HttpPost Success. Request: ${response.request.uri}, Response: ${response.statusCode}, Headers: ${response.request.headers}');
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e) {
print('HttpPost failed. Request: ${e.request.uri}');
if (e.response != null) {
Utils.jsonPrettyPrint(e.response.data);
}
if (returnError) {
return e;
}
throw e;
}
}
static Future<dynamic> httpPatch(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
int businessId = 0,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
Function(int, int) sendProgress,
Function(int, int) receiveProgress,
bool returnError = false,
}) async {
Map<String, dynamic> localHeaders = json.decode(json.encode(headers));
if (additionalHeaders != null) {
localHeaders.addAll(additionalHeaders);
}
if (!url.startsWith('http')) {
localHeaders['Http-Signature'] = generateSignature(url);
}
localHeaders['Http-Business-Id'] = businessId.toString();
Box box = await Utils.getBox();
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
localHeaders['Http-App-Key'] = platformName;
localHeaders['Http-Device-Type'] = platformName;
localHeaders['Content-Type'] = Headers.jsonContentType;
String requestUrl = Constants.BASE_API_URL + url;
if (url.startsWith('http')) {
requestUrl = url;
}
Utils.jsonPrettyPrint(body);
Dio dio = Dio();
try {
Response response = await dio.patch(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
data: json.encode(body),
options: Options(
headers: localHeaders,
),
onSendProgress: sendProgress,
onReceiveProgress: receiveProgress,
).timeout(Duration(seconds: 30));
print('HttpPost Success. Request: ${response.request.uri}, Response: ${response.statusCode}, Headers: ${response.request.headers}');
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e) {
print('HttpPost failed. Request: ${e.request.uri}');
if (e.response != null) {
Utils.jsonPrettyPrint(e.response.data);
}
if (returnError) {
return e;
}
throw e;
}
}
static Future<dynamic> httpDelete(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
int businessId = 0,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
bool returnError = false,
}) async {
Map<String, dynamic> localHeaders = json.decode(json.encode(headers));
if (additionalHeaders != null) {
localHeaders.addAll(additionalHeaders);
}
if (!url.startsWith('http')) {
localHeaders['Http-Signature'] = generateSignature(url);
}
localHeaders['Http-Business-Id'] = businessId.toString();
Box box = await Utils.getBox();
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
localHeaders['Http-App-Key'] = platformName;
localHeaders['Http-Device-Type'] = platformName;
localHeaders['Content-Type'] = Headers.jsonContentType;
String requestUrl = Constants.BASE_API_URL + url;
if (url.startsWith('http')) {
requestUrl = url;
}
Utils.jsonPrettyPrint(body);
Dio dio = Dio();
try {
Response response = await dio.delete(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
data: json.encode(body),
options: Options(
headers: localHeaders,
),
).timeout(Duration(seconds: 30));
print('HttpPost Success. Request: ${response.request.uri}, Response: ${response.statusCode}, Headers: ${response.request.headers}');
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e) {
print('HttpPost failed. Request: ${e.request.uri}');
if (e.response != null) {
Utils.jsonPrettyPrint(e.response.data);
}
if(returnError) {
return e;
}
throw e;
}
}
}

46
lib/utils/util_io.dart Normal file
View File

@@ -0,0 +1,46 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import '../routes.dart';
import 'utils.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
class Util {
static Future<Box> getBox() async {
final dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
Box box = await Hive.openBox('app_data');
return box;
}
static Widget showImage(String imageUrl, {double width, double height,
BoxFit fit, Widget Function(BuildContext, String, dynamic) errorWidget}) {
if (imageUrl == null || imageUrl.isEmpty) {
return Container(
width: width,
height: height,
child: Text(''),
);
}
return CachedNetworkImage(
imageUrl: imageUrl,
width: width,
height: width,
fit: fit,
placeholder: (context, url) => Utils.imageLoadingIndicator(),
errorWidget: errorWidget != null ? errorWidget : (context, url, error) {
return Image.asset(
'assets/images/not_found.png',
width: width,
height: height,
fit: fit,
);
},
);
}
static void openWebUrl(BuildContext context, String link) {
Routes.router.navigateTo(context, '/webview/$link');
}
}

52
lib/utils/util_web.dart Normal file
View File

@@ -0,0 +1,52 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'utils.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
class Util {
static Future<Box> getBox() async {
Hive.initFlutter();
Box box = await Hive.openBox('wisetronic_app_data');
return box;
}
static Widget showImage(String imageUrl, {double width, double height,
BoxFit fit, Widget Function(BuildContext, String, dynamic) errorWidget}) {
return Image.network(imageUrl,
fit: fit,
width: width,
height: height,
cacheWidth: width != null ? width.round() : null,
cacheHeight: height != null ? height.round() : null,
loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: Utils.imageLoadingIndicator(),
);
},
errorBuilder: (BuildContext context, Object object, StackTrace stackTrace) {
if (errorWidget != null) {
return errorWidget(context, null, null);
}
return Image.asset(
'assets/images/not_found.png',
width: width,
height: height,
fit: fit,
);
},
);
}
static void openWebUrl(BuildContext context, String link) async {
var url = 'https://${link}';
if (await canLaunch(url)) {
await launch('$url');
} else {
print('Could not launch $url');
}
}
}

277
lib/utils/utils.dart Normal file
View File

@@ -0,0 +1,277 @@
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 '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);
});
}
}
class RuntimeError extends Error{
final int code;
final String message;
RuntimeError(this.message, {this.code});
String toString() => "Runtime Error: $message";
}