59 lines
1.5 KiB
Dart
59 lines
1.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:qpay/http/http.dart';
|
|
import 'package:qpay/screens/home/home_controller.dart' as hc;
|
|
|
|
part 'pay_controller.freezed.dart';
|
|
part 'pay_controller.g.dart';
|
|
|
|
class PayScreenModel {
|
|
List<BillProduct> products = [];
|
|
late BillProduct selectedProduct;
|
|
dynamic formData = {};
|
|
bool isLoading = false;
|
|
String? errorMessage;
|
|
}
|
|
|
|
@freezed
|
|
abstract class BillProduct with _$BillProduct {
|
|
const factory BillProduct({
|
|
required String uid,
|
|
required String name,
|
|
required String description,
|
|
required String displayName,
|
|
required double defaultAmount,
|
|
required bool requiresAmount,
|
|
}) = _BillProduct;
|
|
|
|
factory BillProduct.fromJson(Map<String, dynamic> json) =>
|
|
_$BillProductFromJson(json);
|
|
}
|
|
|
|
class PayController extends ChangeNotifier {
|
|
final PayScreenModel model = PayScreenModel();
|
|
final Http http = Http();
|
|
|
|
void updateSelectedProduct(BillProduct product) {
|
|
model.selectedProduct = product;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> getProducts(String providerId) async {
|
|
try {
|
|
model.isLoading = true;
|
|
|
|
List<dynamic> response = await http.get(
|
|
'/providers/$providerId/products',
|
|
);
|
|
model.products = response.map((e) => BillProduct.fromJson(e)).toList();
|
|
} catch (e) {
|
|
logger.e(e);
|
|
model.errorMessage = e.toString();
|
|
}
|
|
|
|
model.isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
}
|