diff --git a/.DS_Store b/.DS_Store index b2d3ca66..d4d2f162 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Frontend/lib/mih_components/mih_providers/mzansi_wallet_provider.dart b/Frontend/lib/mih_components/mih_providers/mzansi_wallet_provider.dart index f09a3135..e7904531 100644 --- a/Frontend/lib/mih_components/mih_providers/mzansi_wallet_provider.dart +++ b/Frontend/lib/mih_components/mih_providers/mzansi_wallet_provider.dart @@ -33,11 +33,6 @@ class MzansiWalletProvider extends ChangeNotifier { notifyListeners(); } - void addLoyaltyCard({required MIHLoyaltyCard newCard}) { - loyaltyCards.add(newCard); - notifyListeners(); - } - void deleteLoyaltyCard({required int cardId}) { loyaltyCards.removeWhere((card) => card.idloyalty_cards == cardId); notifyListeners(); diff --git a/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart b/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart index f44e7afb..11cdb53c 100644 --- a/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart +++ b/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart @@ -58,8 +58,12 @@ class _BuildLoyaltyCardListState extends State { ); } - void editCardWindow(MzansiProfileProvider mzansiProfileProvider, - BuildContext ctxt, int index, double width) { + void editCardWindow( + MzansiProfileProvider mzansiProfileProvider, + MzansiWalletProvider walletProvider, + BuildContext ctxt, + int index, + double width) { showDialog( context: context, barrierDismissible: false, @@ -144,6 +148,7 @@ class _BuildLoyaltyCardListState extends State { if (_formKey.currentState!.validate()) { int statusCode = await MIHMzansiWalletApis .updateLoyaltyCardAPICall( + walletProvider, mzansiProfileProvider.user!, widget.cardList[index].idloyalty_cards, widget.cardList[index].shop_name, @@ -211,7 +216,7 @@ class _BuildLoyaltyCardListState extends State { } void deleteCardWindow(MzansiProfileProvider mzansiProfileProvider, - BuildContext ctxt, int index) { + MzansiWalletProvider walletProvider, BuildContext ctxt, int index) { showDialog( context: context, barrierDismissible: false, @@ -221,6 +226,7 @@ class _BuildLoyaltyCardListState extends State { onTap: () async { int statusCode = await MIHMzansiWalletApis.deleteLoyaltyCardAPICall( + walletProvider, mzansiProfileProvider.user!, widget.cardList[index].idloyalty_cards, context, @@ -238,7 +244,7 @@ class _BuildLoyaltyCardListState extends State { } void addToFavCardWindow(MzansiProfileProvider mzansiProfileProvider, - BuildContext ctxt, int index) { + MzansiWalletProvider walletProvider, BuildContext ctxt, int index) { showDialog( context: context, barrierDismissible: false, @@ -270,6 +276,7 @@ class _BuildLoyaltyCardListState extends State { onPressed: () async { int statusCode = await MIHMzansiWalletApis.updateLoyaltyCardAPICall( + walletProvider, mzansiProfileProvider.user!, widget.cardList[index].idloyalty_cards, widget.cardList[index].shop_name, @@ -283,6 +290,7 @@ class _BuildLoyaltyCardListState extends State { context.pop(); context.pop(); await MIHMzansiWalletApis.getFavouriteLoyaltyCards( + walletProvider, mzansiProfileProvider.user!.app_id, context, ); @@ -323,7 +331,7 @@ class _BuildLoyaltyCardListState extends State { } void removeFromFavCardWindow(MzansiProfileProvider mzansiProfileProvider, - BuildContext ctxt, int index) { + MzansiWalletProvider walletProvider, BuildContext ctxt, int index) { showDialog( context: context, barrierDismissible: false, @@ -355,6 +363,7 @@ class _BuildLoyaltyCardListState extends State { onPressed: () async { int statusCode = await MIHMzansiWalletApis.updateLoyaltyCardAPICall( + walletProvider, mzansiProfileProvider.user!, widget.cardList[index].idloyalty_cards, widget.cardList[index].shop_name, @@ -368,6 +377,7 @@ class _BuildLoyaltyCardListState extends State { context.pop(); context.pop(); await MIHMzansiWalletApis.getFavouriteLoyaltyCards( + walletProvider, mzansiProfileProvider.user!.app_id, context, ); @@ -396,8 +406,8 @@ class _BuildLoyaltyCardListState extends State { ); } - void viewCardWindow( - MzansiProfileProvider mzansiProfileProvider, int index, double width) { + void viewCardWindow(MzansiProfileProvider mzansiProfileProvider, + MzansiWalletProvider walletProvider, int index, double width) { //print(widget.cardList[index].card_number); String formattedCardNumber = ""; for (int i = 0; i <= widget.cardList[index].card_number.length - 1; i++) { @@ -441,12 +451,14 @@ class _BuildLoyaltyCardListState extends State { if (widget.cardList[index].favourite == "") { addToFavCardWindow( mzansiProfileProvider, + walletProvider, context, index, ); } else { removeFromFavCardWindow( mzansiProfileProvider, + walletProvider, context, index, ); @@ -476,6 +488,7 @@ class _BuildLoyaltyCardListState extends State { }); editCardWindow( mzansiProfileProvider, + walletProvider, context, index, width, @@ -501,6 +514,7 @@ class _BuildLoyaltyCardListState extends State { onTap: () { deleteCardWindow( mzansiProfileProvider, + walletProvider, context, index, ); @@ -705,9 +719,11 @@ class _BuildLoyaltyCardListState extends State { // final double width = size.width; //final double height = size.height; if (widget.cardList.isNotEmpty) { - return Consumer( + return Consumer2( builder: (BuildContext context, - MzansiProfileProvider mzansiProfileProvider, Widget? child) { + MzansiProfileProvider mzansiProfileProvider, + MzansiWalletProvider walletProvider, + Widget? child) { return GridView.builder( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, @@ -732,6 +748,7 @@ class _BuildLoyaltyCardListState extends State { setScreenBrightness(1.0); viewCardWindow( mzansiProfileProvider, + walletProvider, index, size.width, ); diff --git a/Frontend/lib/mih_packages/mzansi_wallet/components/mih_add_card_window.dart b/Frontend/lib/mih_packages/mzansi_wallet/components/mih_add_card_window.dart index b1e69c35..d97ded85 100644 --- a/Frontend/lib/mih_packages/mzansi_wallet/components/mih_add_card_window.dart +++ b/Frontend/lib/mih_packages/mzansi_wallet/components/mih_add_card_window.dart @@ -10,6 +10,7 @@ import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_ import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_text_form_field.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_pop_up_messages/mih_error_message.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_providers/mzansi_profile_provider.dart'; +import 'package:mzansi_innovation_hub/mih_components/mih_providers/mzansi_wallet_provider.dart'; import 'package:mzansi_innovation_hub/mih_config/mih_colors.dart'; import 'package:mzansi_innovation_hub/mih_packages/mzansi_wallet/components/mih_card_display.dart'; import 'package:mzansi_innovation_hub/mih_services/mih_alert_services.dart'; @@ -72,9 +73,6 @@ class _MihAddCardWindowState extends State { child: MihButton( onPressed: () { context.pop(); - context.goNamed( - 'mzansiWallet', - ); }, buttonColor: MihColors.getGreenColor( MzansiInnovationHub.of(context)!.theme.mode == "Dark"), @@ -141,9 +139,11 @@ class _MihAddCardWindowState extends State { padding: MzansiInnovationHub.of(context)!.theme.screenType == "desktop" ? EdgeInsets.symmetric(horizontal: width * 0.05) : EdgeInsets.symmetric(horizontal: width * 0), - child: Consumer( + child: Consumer2( builder: (BuildContext context, - MzansiProfileProvider mzansiProfileProvider, Widget? child) { + MzansiProfileProvider mzansiProfileProvider, + MzansiWalletProvider walletProvider, + Widget? child) { return Column( children: [ MihForm( @@ -158,9 +158,7 @@ class _MihAddCardWindowState extends State { return MihValidationServices().isEmpty(value); }, requiredText: true, - dropdownOptions: const [ - "+More", - "Apple Tree", + dropdownOptions: const [ "+More", "Apple Tree", "Auchan", @@ -206,7 +204,7 @@ class _MihAddCardWindowState extends State { "Total Energies", "Toys R Us", "Woermann Brock", - "Woolworths" + "Woolworths", ], ), ValueListenableBuilder( @@ -306,6 +304,7 @@ class _MihAddCardWindowState extends State { } else { int statusCode = await MIHMzansiWalletApis .addLoyaltyCardAPICall( + walletProvider, mzansiProfileProvider.user!, mzansiProfileProvider.user!.app_id, _shopController.text, @@ -317,7 +316,7 @@ class _MihAddCardWindowState extends State { ); if (statusCode == 201) { context.pop(); - KenLogger.error("Card Added Successfully"); + KenLogger.success("Card Added Successfully"); successPopUp( "Successfully Added Card", "The loyalty card has been added to your favourites.", diff --git a/Frontend/lib/mih_packages/mzansi_wallet/mih_wallet.dart b/Frontend/lib/mih_packages/mzansi_wallet/mih_wallet.dart index bfc58d34..e744a248 100644 --- a/Frontend/lib/mih_packages/mzansi_wallet/mih_wallet.dart +++ b/Frontend/lib/mih_packages/mzansi_wallet/mih_wallet.dart @@ -25,24 +25,31 @@ class _MihWalletState extends State { bool isLoading = true; Future setLoyaltyCards( - MzansiProfileProvider mzansiProfileProvider) async { + MzansiProfileProvider mzansiProfileProvider, + MzansiWalletProvider walletProvider, + ) async { await MIHMzansiWalletApis.getLoyaltyCards( - mzansiProfileProvider.user!.app_id, context); + walletProvider, mzansiProfileProvider.user!.app_id, context); } Future setFavouritesCards( - MzansiProfileProvider mzansiProfileProvider) async { + MzansiProfileProvider mzansiProfileProvider, + MzansiWalletProvider walletProvider, + ) async { await MIHMzansiWalletApis.getFavouriteLoyaltyCards( - mzansiProfileProvider.user!.app_id, context); + walletProvider, mzansiProfileProvider.user!.app_id, context); } @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) async { - var mzansiProfileProvider = context.read(); - await setLoyaltyCards(mzansiProfileProvider); - await setFavouritesCards(mzansiProfileProvider); + MzansiProfileProvider mzansiProfileProvider = + context.read(); + MzansiWalletProvider walletProvider = + context.read(); + await setLoyaltyCards(mzansiProfileProvider, walletProvider); + await setFavouritesCards(mzansiProfileProvider, walletProvider); context.read().loadBannerAd(); setState(() { isLoading = false; diff --git a/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart b/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart index 089a382d..18f59a8f 100644 --- a/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart +++ b/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart @@ -29,19 +29,18 @@ class MihCards extends StatefulWidget { class _MihCardsState extends State { final TextEditingController cardSearchController = TextEditingController(); final FocusNode searchFocusNode = FocusNode(); - List listOfCards = []; final ValueNotifier> searchShopName = ValueNotifier([]); final MobileScannerController scannerController = MobileScannerController( detectionSpeed: DetectionSpeed.unrestricted, ); final boxFit = BoxFit.contain; - void searchShop() { + void searchShop(List allCards) { if (cardSearchController.text.isEmpty) { - searchShopName.value = listOfCards; + searchShopName.value = allCards; } else { List temp = []; - for (var item in listOfCards) { + for (var item in allCards) { if (item.shop_name .toLowerCase() .contains(cardSearchController.text.toLowerCase()) || @@ -130,24 +129,23 @@ class _MihCardsState extends State { @override void dispose() { - cardSearchController.removeListener(searchShop); + final walletProvider = context.read(); + cardSearchController.removeListener(() { + searchShop(walletProvider.loyaltyCards); + }); cardSearchController.dispose(); searchShopName.dispose(); searchFocusNode.dispose(); super.dispose(); } - void getLoyaltyCards(BuildContext context) async { - setState(() { - listOfCards = context.read().loyaltyCards; - }); - searchShop(); - } - @override void initState() { - getLoyaltyCards(context); - cardSearchController.addListener(searchShop); + final walletProvider = context.read(); + searchShopName.value = walletProvider.loyaltyCards; + cardSearchController.addListener(() { + searchShop(walletProvider.loyaltyCards); + }); super.initState(); } @@ -162,76 +160,89 @@ class _MihCardsState extends State { } Widget getBody(double width) { - return Stack( - children: [ - MihSingleChildScroll( - child: Column( - children: [ - Padding( - padding: EdgeInsets.symmetric(horizontal: width / 20), - child: MihSearchBar( - controller: cardSearchController, - hintText: "Search Cards", - // prefixIcon: Icons.search, - prefixIcon: Icons.search, - fillColor: MihColors.getSecondaryColor( - MzansiInnovationHub.of(context)!.theme.mode == "Dark"), - hintColor: MihColors.getPrimaryColor( - MzansiInnovationHub.of(context)!.theme.mode == "Dark"), - onPrefixIconTap: () { - // print("Search Icon Pressed: ${cardSearchController.text}"); - }, - searchFocusNode: searchFocusNode, - ), - ), - const SizedBox(height: 10), - Consumer( - builder: (context, mzansiWalletProvider, child) { - listOfCards = mzansiWalletProvider.loyaltyCards; - return ValueListenableBuilder>( - valueListenable: searchShopName, - builder: (context, filteredCards, child) { - return BuildLoyaltyCardList( - cardList: filteredCards, //listOfCards, - navIndex: 0, - favouritesMode: false, - searchText: cardSearchController, - ); - }); - }, - ), - ], - ), - ), - Positioned( - right: 10, - bottom: 10, - child: MihFloatingMenu( - animatedIcon: AnimatedIcons.menu_close, - children: [ - SpeedDialChild( - child: Icon( - Icons.add, - color: MihColors.getPrimaryColor( - MzansiInnovationHub.of(context)!.theme.mode == "Dark"), + return Consumer( + builder: (BuildContext context, MzansiWalletProvider walletProvider, + Widget? child) { + if (cardSearchController.text.isEmpty) { + searchShopName.value = walletProvider.loyaltyCards; + } else { + // Re-run search with updated card list + searchShop(walletProvider.loyaltyCards); + } + return Stack( + children: [ + MihSingleChildScroll( + child: Column( + children: [ + Padding( + padding: EdgeInsets.symmetric(horizontal: width / 20), + child: MihSearchBar( + controller: cardSearchController, + hintText: "Search Cards", + // prefixIcon: Icons.search, + prefixIcon: Icons.search, + fillColor: MihColors.getSecondaryColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + hintColor: MihColors.getPrimaryColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + onPrefixIconTap: () { + // print("Search Icon Pressed: ${cardSearchController.text}"); + }, + searchFocusNode: searchFocusNode, + ), ), - label: "Add Loyalty Card", - labelBackgroundColor: MihColors.getGreenColor( - MzansiInnovationHub.of(context)!.theme.mode == "Dark"), - labelStyle: TextStyle( - color: MihColors.getPrimaryColor( - MzansiInnovationHub.of(context)!.theme.mode == "Dark"), - fontWeight: FontWeight.bold, + const SizedBox(height: 10), + ValueListenableBuilder>( + valueListenable: searchShopName, + builder: (context, filteredCards, child) { + return BuildLoyaltyCardList( + cardList: filteredCards, //listOfCards, + navIndex: 0, + favouritesMode: false, + searchText: cardSearchController, + ); + }, ), - backgroundColor: MihColors.getGreenColor( - MzansiInnovationHub.of(context)!.theme.mode == "Dark"), - onTap: () { - addCardWindow(context, width); - }, - ) - ]), - ) - ], + ], + ), + ), + Positioned( + right: 10, + bottom: 10, + child: MihFloatingMenu( + animatedIcon: AnimatedIcons.menu_close, + children: [ + SpeedDialChild( + child: Icon( + Icons.add, + color: MihColors.getPrimaryColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + ), + label: "Add Loyalty Card", + labelBackgroundColor: MihColors.getGreenColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + labelStyle: TextStyle( + color: MihColors.getPrimaryColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + fontWeight: FontWeight.bold, + ), + backgroundColor: MihColors.getGreenColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + onTap: () { + addCardWindow(context, width); + }, + ) + ]), + ) + ], + ); + }, ); } } diff --git a/Frontend/lib/mih_services/mih_mzansi_wallet_services.dart b/Frontend/lib/mih_services/mih_mzansi_wallet_services.dart index 7dcdd4e1..1acf927b 100644 --- a/Frontend/lib/mih_services/mih_mzansi_wallet_services.dart +++ b/Frontend/lib/mih_services/mih_mzansi_wallet_services.dart @@ -1,14 +1,13 @@ import 'dart:convert'; import 'package:go_router/go_router.dart'; +import 'package:ken_logger/ken_logger.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_objects/app_user.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_objects/loyalty_card.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_pop_up_messages/mih_loading_circle.dart'; import 'package:flutter/material.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_providers/mzansi_wallet_provider.dart'; -import 'package:provider/provider.dart'; import 'package:supertokens_flutter/http.dart' as http; - import '../mih_components/mih_pop_up_messages/mih_error_message.dart'; import '../mih_components/mih_pop_up_messages/mih_success_message.dart'; import '../mih_config/mih_env.dart'; @@ -17,6 +16,7 @@ class MIHMzansiWalletApis { final baseAPI = AppEnviroment.baseApiUrl; static Future getLoyaltyCards( + MzansiWalletProvider walletProvider, String app_id, BuildContext context, ) async { @@ -26,7 +26,7 @@ class MIHMzansiWalletApis { Iterable l = jsonDecode(response.body); List myCards = List.from( l.map((model) => MIHLoyaltyCard.fromJson(model))); - context.read().setLoyaltyCards(cards: myCards); + walletProvider.setLoyaltyCards(cards: myCards); // return myCards; } else { throw Exception('failed to fatch loyalty cards'); @@ -34,6 +34,7 @@ class MIHMzansiWalletApis { } static Future getFavouriteLoyaltyCards( + MzansiWalletProvider walletProvider, String app_id, BuildContext context, ) async { @@ -44,7 +45,7 @@ class MIHMzansiWalletApis { Iterable l = jsonDecode(response.body); List myCards = List.from( l.map((model) => MIHLoyaltyCard.fromJson(model))); - context.read().setFavouriteCards(cards: myCards); + walletProvider.setFavouriteCards(cards: myCards); } // else { // throw Exception('failed to fatch loyalty cards'); @@ -60,6 +61,7 @@ class MIHMzansiWalletApis { /// /// Returns VOID (TRIGGERS NOTIGICATIOPN ON SUCCESS) static Future deleteLoyaltyCardAPICall( + MzansiWalletProvider walletProvider, AppUser signedInUser, int idloyalty_cards, BuildContext context, @@ -77,9 +79,7 @@ class MIHMzansiWalletApis { //print(response.statusCode); context.pop(); if (response.statusCode == 200) { - context - .read() - .deleteLoyaltyCard(cardId: idloyalty_cards); + walletProvider.deleteLoyaltyCard(cardId: idloyalty_cards); } return response.statusCode; // if (response.statusCode == 200) { @@ -111,6 +111,7 @@ class MIHMzansiWalletApis { /// /// Returns VOID (TRIGGERS SUCCESS pop up) static Future addLoyaltyCardAPICall( + MzansiWalletProvider walletProvider, AppUser signedInUser, String app_id, String shop_name, @@ -137,6 +138,10 @@ class MIHMzansiWalletApis { }), ); context.pop(); + KenLogger.success("Response: $response"); + if (response.statusCode == 201) { + await getLoyaltyCards(walletProvider, app_id, context); + } return response.statusCode; // if (response.statusCode == 201) { // // Navigator.pop(context); @@ -164,6 +169,7 @@ class MIHMzansiWalletApis { /// /// Returns VOID (TRIGGERS NOTIGICATIOPN ON SUCCESS) static Future updateLoyaltyCardAPICall( + MzansiWalletProvider walletProvider, AppUser signedInUser, int idloyalty_cards, String shopName, @@ -190,17 +196,17 @@ class MIHMzansiWalletApis { ); context.pop(); if (response.statusCode == 200) { - context.read().editLoyaltyCard( - updatedCard: MIHLoyaltyCard( - idloyalty_cards: idloyalty_cards, - app_id: signedInUser.app_id, - shop_name: shopName, - card_number: card_number, - favourite: favourite, - priority_index: priority_index, - nickname: nickname, - ), - ); + walletProvider.editLoyaltyCard( + updatedCard: MIHLoyaltyCard( + idloyalty_cards: idloyalty_cards, + app_id: signedInUser.app_id, + shop_name: shopName, + card_number: card_number, + favourite: favourite, + priority_index: priority_index, + nickname: nickname, + ), + ); } return response.statusCode; }