diff --git a/lib/backend/DataManager.dart b/lib/backend/DataManager.dart index 8d7f311..ada7fb2 100644 --- a/lib/backend/DataManager.dart +++ b/lib/backend/DataManager.dart @@ -180,7 +180,7 @@ class DataManager{ - static void CalculateEarnings() async{ + static Future CalculateEarnings() async{ Debug.Log("Going to calculate earnings from games"); while(UserJson == null){ await Future.delayed(const Duration(seconds: 1)); @@ -262,6 +262,27 @@ class DataManager{ return (response.body.toString()); } + static Future RequestWd(String coin) async{ + + Map body = { + "id": UserJson['id'], + "username" : UserJson['name'], + "coin": coin + }; + var response = (await http.post( + Uri.parse('${API_ENDPOINT}request_wd.php'), + body: body)); + Debug.Log("Request WD response: " +response.body.toString()); + + if(response.body.toString().contains("{")){ + UserJson['earnings'] =(response.body.toString()); + await CalculateEarnings(); + return true; + }else{ + return false; + } + } + static Future> GetWithdrawalHistory() async{ Map body = { diff --git a/lib/backend/login_mgr.dart b/lib/backend/login_mgr.dart index 94002d7..000b2b9 100644 --- a/lib/backend/login_mgr.dart +++ b/lib/backend/login_mgr.dart @@ -82,6 +82,7 @@ class LoginManager { try{ DataManager.UserJson = jsonDecode(loginResponse.body.toString()); await DataManager.GetGamesProgress(); + await DataManager.CalculateEarnings(); Debug.LogResponse(DataManager.UserJson); prefs.setString("username", username); prefs.setString("password", password); diff --git a/lib/home.dart b/lib/home.dart index f42119a..7a98606 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -401,9 +401,7 @@ class _HomeState extends State with WidgetsBindingObserver { return; } - Navigator.of(context).push(MaterialPageRoute( - builder: (BuildContext context) => - WithdrawalPortal())); + Navigator.of(context).push(wdRoute); }, child: Text("Withdraw"),height: 40, width: 200, color: Colors.greenAccent), GlassButton(onTap: (){ Navigator.of(context).push(MaterialPageRoute( diff --git a/lib/wd_portal.dart b/lib/wd_portal.dart index dfd098e..78c8503 100644 --- a/lib/wd_portal.dart +++ b/lib/wd_portal.dart @@ -4,7 +4,8 @@ import 'package:fhub/backend/Dialogs.dart'; import 'package:fhub/backend/helpers.dart'; import 'package:fhub/src/CustomWidgets.dart'; import 'package:flutter/material.dart'; - +import 'package:flutter_spinkit/flutter_spinkit.dart'; +var wdRoute = MaterialPageRoute(builder: (context) => WithdrawalPortal()); class WithdrawalPortal extends StatefulWidget { const WithdrawalPortal({Key? key}) : super(key: key); @@ -100,7 +101,10 @@ class _WithdrawalPortalState extends State { ) ]), GlassButton(onTap: (){ - + if(selectedCurrencies.length > 0){ + ShowConfirmationDialog();}else{ + Dialogs.showAlertDialog(context, "No Cryptos Selected", "Please select desired cryptos first to withdraw"); + } }, child: Text("Withdraw"), width: 300,height: 50,color: selectedCurrencies.length > 0 ? Colors.greenAccent: Colors.blueGrey) ], ))), @@ -111,6 +115,107 @@ class _WithdrawalPortalState extends State { ); } + + void ShowConfirmationDialog(){ + // set up the AlertDialog + AlertDialog alert = AlertDialog( + backgroundColor: Color(0xFF1F1F1F), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)), + title: Text("Are you Sure?",textAlign: TextAlign.center,), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text("You are about to Withdraw ${selectedCurrenciesTxt()} to ${DataManager.UserJson['wd_address']} Coinbase account.\n",textAlign: TextAlign.center,), + Text("Warning: Make sure the address is correct and a valid coinbase account. This transaction is non-reversible.\n\n",style:TextStyle(color: Colors.orange)), + Text("Press 'Confirm' To Initiate the Withdrawal",style: TextStyle(fontWeight: FontWeight.bold),textAlign: TextAlign.center,) + ], + ), + actions: withdrawing ? [Text("Processing...",style: TextStyle(color: Colors.deepPurpleAccent),)] : [ + TextButton( + onPressed: Confirmed, + child: Text("Confirm"), + ), + TextButton( + child: Text("Cancel"), + onPressed: () { + Navigator.of(context).pop(); + }, + ) + ], + + ); + + // show the dialog + showDialog( + barrierDismissible: false, + context: context, + builder: (BuildContext context) { + return alert; + }, + ); + } + bool withdrawing = false; + + void Confirmed() async{ + Navigator.of(context).pop(); + + ShowProcessingDialog(); + // await Future.delayed(const Duration(seconds: 2)); + bool success =false; + + + for(int i=0; i < selectedCurrencies.length; i++){ + success = await DataManager.RequestWd(selectedCurrencies[i]); + } + + Navigator.of(context).pop(); + if(success) { + Dialogs.showAlertDialog(context, "Withdrawal Success", + "Your Withdrawal is now processing. Cryptos will be in your coinbase account within 5 business days.\n\nEnjoy!"); + }else{ + Dialogs.showAlertDialog(context, "Failed", "Something went wrong with your withdrawal. Please contact faucethub@playpoolstudios.com for support"); + } + Navigator.of(context).removeRoute(wdRoute); + setState(() { + + }); + } + + + void ShowProcessingDialog(){ + AlertDialog alert = AlertDialog( + backgroundColor: Color(0xFF1F1F1F), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)), + title: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SpinKitCircle(color: Colors.deepPurpleAccent,), + SizedBox(width: 50,), + Text("Processing",), + ], + ), + + ); + + showDialog(context: context, builder: (BuildContext context){ + return alert; + }, + barrierDismissible: false); + } + + String selectedCurrenciesTxt(){ + String out = ""; + selectedCurrencies.forEach((element) { + if(out.length <= 0){ + out+=element; + }else{ + out+="," + element; + } + }); + + return out; + } + List selectedCurrencies = []; Widget WalletCurrencyListItem(String coin) {