import 'package:cake_wallet/ionia/ionia_service.dart'; import 'package:cake_wallet/ionia/ionia_create_state.dart'; import 'package:cake_wallet/ionia/ionia_merchant.dart'; import 'package:cake_wallet/ionia/ionia_virtual_card.dart'; import 'package:mobx/mobx.dart'; part 'ionia_view_model.g.dart'; class IoniaViewModel = IoniaViewModelBase with _$IoniaViewModel; abstract class IoniaViewModelBase with Store { IoniaViewModelBase({this.ioniaService}) : createUserState = IoniaCreateStateSuccess(), otpState = IoniaOtpSendDisabled(), cardState = IoniaNoCardState(), ioniaMerchants = [], scrollOffsetFromTop = 0.0 { _getMerchants().then((value) { ioniaMerchants = value; }); _getAuthStatus().then((value) => isLoggedIn = value); } final IoniaService ioniaService; @observable double scrollOffsetFromTop; @observable IoniaCreateAccountState createUserState; @observable IoniaOtpState otpState; @observable IoniaCreateCardState createCardState; @observable IoniaFetchCardState cardState; @observable List ioniaMerchants; @observable String email; @observable String otp; @observable bool isLoggedIn; @action Future createUser(String email) async { createUserState = IoniaCreateStateLoading(); try { await ioniaService.createUser(email); createUserState = IoniaCreateStateSuccess(); } on Exception catch (e) { createUserState = IoniaCreateStateFailure(error: e.toString()); } } @action Future verifyEmail(String code) async { try { otpState = IoniaOtpValidating(); await ioniaService.verifyEmail(code); otpState = IoniaOtpSuccess(); } catch (_) { otpState = IoniaOtpFailure(error: 'Invalid OTP. Try again'); } } Future _getAuthStatus() async { return await ioniaService.isLogined(); } @action Future createCard() async { createCardState = IoniaCreateCardLoading(); try { final card = await ioniaService.createCard(); createCardState = IoniaCreateCardSuccess(); return card; } on Exception catch (e) { createCardState = IoniaCreateCardFailure(error: e.toString()); } return null; } Future _getCard() async { cardState = IoniaFetchingCard(); try { final card = await ioniaService.getCard(); cardState = IoniaCardSuccess(card: card); } catch (_) { cardState = IoniaFetchCardFailure(); } } Future> _getMerchants() async { return await ioniaService.getMerchants(); } void setScrollOffsetFromTop(double scrollOffset) { scrollOffsetFromTop = scrollOffset; } }