You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.4 KiB
Dart
75 lines
2.4 KiB
Dart
|
|
import 'package:car_provider_app/repositories/subscription_repo.dart';
|
|
import 'package:mc_common_app/models/subscription_model.dart';
|
|
import 'package:mc_common_app/utils/enums.dart';
|
|
import 'package:mc_common_app/view_models/base_view_model.dart';
|
|
import 'package:mc_common_app/widgets/dropdown/dropdow_field.dart';
|
|
|
|
class SubscriptionsVM extends BaseVM {
|
|
final SubscriptionRepo subscriptionRepo;
|
|
|
|
SubscriptionsVM({required this.subscriptionRepo});
|
|
|
|
//All Subscriptions
|
|
int selectedIndex = 0;
|
|
late DropValue selectedMothlyTab;
|
|
List<DropValue> monthlyTabs = [];
|
|
late SubscriptionModel allSubscriptions;
|
|
List<Subscription> tempSubscriptions = [];
|
|
|
|
//My Subscriptions
|
|
|
|
//All Subscriptions
|
|
getAllAvailableSubscriptions(String? serviceProviderID) async {
|
|
selectedIndex = 0;
|
|
setState(ViewState.busy);
|
|
allSubscriptions = await subscriptionRepo.getAllSubscriptions(serviceProviderID);
|
|
if (allSubscriptions.messageStatus == 1) {
|
|
monthlyTabs.clear();
|
|
var idSet = <int>{};
|
|
for (var d in allSubscriptions.data ?? []) {
|
|
if (idSet.add(d.durationDays ?? 0)) {
|
|
monthlyTabs.add(DropValue(d.durationDays, _convertDaysToMonths(d.durationDays ?? 0), ""));
|
|
}
|
|
}
|
|
monthlyTabs.sort((a, b) => a.value.compareTo(b.value));
|
|
selectedMothlyTab = monthlyTabs.first;
|
|
filterSubscriptions();
|
|
setState(ViewState.idle);
|
|
} else {
|
|
setState(ViewState.error);
|
|
}
|
|
}
|
|
|
|
String _convertDaysToMonths(int days) {
|
|
final int months = days ~/ 30;
|
|
final int remainingDays = days % 30;
|
|
|
|
String _result = months > 0 ? '$months Month${months > 1 ? 's' : ''}${remainingDays > 0 ? ' & ' : ''}' : '';
|
|
_result += remainingDays > 0 ? '$remainingDays Day${remainingDays > 1 ? 's' : ''}' : '';
|
|
return _result;
|
|
}
|
|
|
|
filterSubscriptions() {
|
|
tempSubscriptions.clear();
|
|
for (var element in allSubscriptions.data!) {
|
|
if (selectedMothlyTab.id == element.durationDays) {
|
|
tempSubscriptions.add(element);
|
|
}
|
|
}
|
|
}
|
|
|
|
//My Subscriptions
|
|
getMySubscriptions(String? serviceProviderID) async {
|
|
selectedIndex = 0;
|
|
setState(ViewState.busy);
|
|
allSubscriptions = await subscriptionRepo.getAllSubscriptions(serviceProviderID);
|
|
if (allSubscriptions.messageStatus == 1) {
|
|
// allSubscriptions.data!.sort((a, b) => a.value.compareTo(b.value));
|
|
setState(ViewState.idle);
|
|
} else {
|
|
setState(ViewState.error);
|
|
}
|
|
}
|
|
}
|