Commit a6797435 authored by DatHV's avatar DatHV
Browse files

refactor print, log, request

parent f0334970
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:mypoint_flutter_app/widgets/image_loader.dart'; import 'package:mypoint_flutter_app/widgets/image_loader.dart';
import '../../base/base_screen.dart'; import '../../base/base_screen.dart';
......
...@@ -42,9 +42,7 @@ class MembershipViewModel extends RestfulApiViewModel { ...@@ -42,9 +42,7 @@ class MembershipViewModel extends RestfulApiViewModel {
orElse: () => levels!.first orElse: () => levels!.first
); );
} catch (e) { } catch (e) {
if (kDebugMode) { debugPrint('Failed to select level: $e');
print('Failed to select level: $e');
}
selectedLevel = levels!.isNotEmpty ? levels!.first : null; selectedLevel = levels!.isNotEmpty ? levels!.first : null;
} }
} }
......
import 'package:get/get_rx/src/rx_types/rx_types.dart'; import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:mypoint_flutter_app/configs/constants.dart'; import 'package:mypoint_flutter_app/configs/constants.dart';
import 'package:mypoint_flutter_app/networking/restful_api_client_all_request.dart'; import 'package:mypoint_flutter_app/networking/api/product_api.dart' deferred as product_api;
import 'package:mypoint_flutter_app/screen/mobile_card/models/product_mobile_card_model.dart'; import 'package:mypoint_flutter_app/screen/mobile_card/models/product_mobile_card_model.dart';
import '../../base/base_response_model.dart';
import '../../networking/restful_api_viewmodel.dart'; import '../../networking/restful_api_viewmodel.dart';
import '../../preference/point/point_manager.dart'; import '../../preference/point/point_manager.dart';
import 'models/mobile_service_redeem_data.dart'; import 'models/mobile_service_redeem_data.dart';
...@@ -17,6 +18,19 @@ class ProductMobileCardViewModel extends RestfulApiViewModel { ...@@ -17,6 +18,19 @@ class ProductMobileCardViewModel extends RestfulApiViewModel {
return groupedSection[selectedBrandCode.value] ?? []; return groupedSection[selectedBrandCode.value] ?? [];
} }
ProductMobileCardModel? selectedProduct; ProductMobileCardModel? selectedProduct;
bool _productApiLoaded = false;
Future<void> _ensureProductApiLoaded() async {
if (_productApiLoaded) return;
await product_api.loadLibrary();
_productApiLoaded = true;
}
Future<BaseResponseModel<T>> _callProductApi<T>(Future<BaseResponseModel<T>> Function(dynamic api) fn) async {
await _ensureProductApiLoaded();
final api = product_api.ProductApi(client);
return fn(api);
}
int get payPoint { int get payPoint {
return int.tryParse(selectedProduct?.prices?.firstOrNull?.payPoint ?? "0") ?? 0; return int.tryParse(selectedProduct?.prices?.firstOrNull?.payPoint ?? "0") ?? 0;
...@@ -33,7 +47,7 @@ class ProductMobileCardViewModel extends RestfulApiViewModel { ...@@ -33,7 +47,7 @@ class ProductMobileCardViewModel extends RestfulApiViewModel {
Future<void> redeemProductMobileCard() async { Future<void> redeemProductMobileCard() async {
await callApi<MobileServiceRedeemData>( await callApi<MobileServiceRedeemData>(
request: () => client.redeemMobileCard((selectedProduct?.id ?? 0).toString()), request: () => _callProductApi((api) => api.redeemMobileCard((selectedProduct?.id ?? 0).toString())),
onSuccess: (data, _) async { onSuccess: (data, _) async {
final itemId = data.itemId ?? ""; final itemId = data.itemId ?? "";
if (itemId.isEmpty) { if (itemId.isEmpty) {
...@@ -50,7 +64,7 @@ class ProductMobileCardViewModel extends RestfulApiViewModel { ...@@ -50,7 +64,7 @@ class ProductMobileCardViewModel extends RestfulApiViewModel {
Future<void> _getMobileCardCode(String itemId) async { Future<void> _getMobileCardCode(String itemId) async {
await callApi<RedeemProductResponseModel>( await callApi<RedeemProductResponseModel>(
request: () => client.getMobileCardCode(itemId), request: () => _callProductApi((api) => api.getMobileCardCode(itemId)),
onSuccess: (data, _) { onSuccess: (data, _) {
final item = data.item; final item = data.item;
if (item != null) { if (item != null) {
...@@ -67,7 +81,7 @@ class ProductMobileCardViewModel extends RestfulApiViewModel { ...@@ -67,7 +81,7 @@ class ProductMobileCardViewModel extends RestfulApiViewModel {
Future<void> getProductMobileCard() async { Future<void> getProductMobileCard() async {
await callApi<ProductMobileCardResponse>( await callApi<ProductMobileCardResponse>(
request: () => client.productMobileCardGetList(), request: () => _callProductApi((api) => api.productMobileCardGetList()),
onSuccess: (data, _) { onSuccess: (data, _) {
final result = data.products ?? []; final result = data.products ?? [];
final seen = <String>{}; final seen = <String>{};
......
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:mypoint_flutter_app/networking/restful_api_client_all_request.dart'; import 'package:mypoint_flutter_app/networking/api/website_api.dart' deferred as website_api;
import '../../networking/restful_api_viewmodel.dart'; import '../../networking/restful_api_viewmodel.dart';
import '../faqs/faqs_model.dart'; import '../faqs/faqs_model.dart';
...@@ -10,6 +10,14 @@ class NewsListViewModel extends RestfulApiViewModel { ...@@ -10,6 +10,14 @@ class NewsListViewModel extends RestfulApiViewModel {
var _canLoadMore = true; var _canLoadMore = true;
int limit = 20; int limit = 20;
bool _websiteApiLoaded = false;
Future<void> _ensureWebsiteApiLoaded() async {
if (_websiteApiLoaded) return;
await website_api.loadLibrary();
_websiteApiLoaded = true;
}
NewsListViewModel({this.folderUri = "TIN-TUC"}); NewsListViewModel({this.folderUri = "TIN-TUC"});
@override @override
...@@ -22,13 +30,13 @@ class NewsListViewModel extends RestfulApiViewModel { ...@@ -22,13 +30,13 @@ class NewsListViewModel extends RestfulApiViewModel {
if (isLoading.value) return; if (isLoading.value) return;
if (!isRefresh && !_canLoadMore) return; if (!isRefresh && !_canLoadMore) return;
isLoading(true); isLoading(true);
final body = { final body = {"folder_uri": folderUri, "start": isRefresh ? 0 : newsList.length, "limit": limit};
"folder_uri": folderUri,
"start": isRefresh ? 0 : newsList.length,
"limit": limit,
};
await callApi<FAQItemModelResponse>( await callApi<FAQItemModelResponse>(
request: () => client.websiteFolderGetPageList(body), request: () async {
await _ensureWebsiteApiLoaded();
final api = website_api.WebsiteApi(client);
return api.websiteFolderGetPageList(body);
},
onSuccess: (data, _) { onSuccess: (data, _) {
_canLoadMore = (data.items?.length ?? 0) == limit; _canLoadMore = (data.items?.length ?? 0) == limit;
if (isRefresh) { if (isRefresh) {
...@@ -39,7 +47,7 @@ class NewsListViewModel extends RestfulApiViewModel { ...@@ -39,7 +47,7 @@ class NewsListViewModel extends RestfulApiViewModel {
withLoading: false, withLoading: false,
onComplete: () { onComplete: () {
isLoading(false); isLoading(false);
} },
); );
} }
} }
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment