cake_wallet/lib/view_model/ionia/ionia_view_model.dart

102 lines
2.5 KiB
Dart

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 = [] {
_getMerchants().then((value){
ioniaMerchants = value;
});
_getAuthStatus().then((value) => isLoggedIn = value);
}
final IoniaService ioniaService;
@observable
IoniaCreateAccountState createUserState;
@observable
IoniaOtpState otpState;
@observable
IoniaCreateCardState createCardState;
@observable
IoniaFetchCardState cardState;
@observable
List<IoniaMerchant> ioniaMerchants;
@observable
String email;
@observable
String otp;
@observable
bool isLoggedIn;
@action
Future<void> createUser(String email) async {
createUserState = IoniaCreateStateLoading();
try {
await ioniaService.createUser(email);
createUserState = IoniaCreateStateSuccess();
} on Exception catch (e) {
createUserState = IoniaCreateStateFailure(error: e.toString());
}
}
@action
Future<void> verifyEmail(String code) async {
try {
otpState = IoniaOtpValidating();
await ioniaService.verifyEmail(code);
otpState = IoniaOtpSuccess();
} catch (_) {
otpState = IoniaOtpFailure(error: 'Invalid OTP. Try again');
}
}
Future<bool> _getAuthStatus() async {
return await ioniaService.isLogined();
}
@action
Future<IoniaVirtualCard> 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<void> _getCard() async {
cardState = IoniaFetchingCard();
try {
final card = await ioniaService.getCard();
cardState = IoniaCardSuccess(card: card);
} catch (_) {
cardState = IoniaFetchCardFailure();
}
}
Future<List<IoniaMerchant>> _getMerchants()async{
return await ioniaService.getMerchants();
}
}