redesigned payment page + a lot of fixes
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
import 'package:pshared/config/constants.dart';
|
||||
import 'package:pshared/models/organization/organization.dart';
|
||||
import 'package:pshared/models/auth/state.dart';
|
||||
import 'package:pshared/provider/account.dart';
|
||||
import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/organization.dart';
|
||||
import 'package:pshared/service/secure_storage.dart';
|
||||
@@ -16,6 +22,7 @@ class OrganizationsProvider extends ChangeNotifier {
|
||||
|
||||
List<Organization> get organizations => _resource.data ?? [];
|
||||
String? _currentOrg;
|
||||
AccountProvider? _accountProvider;
|
||||
|
||||
Organization get current => isOrganizationSet ? _current! : throw StateError('Organization is not set');
|
||||
|
||||
@@ -26,6 +33,13 @@ class OrganizationsProvider extends ChangeNotifier {
|
||||
bool get isLoading => _resource.isLoading;
|
||||
Object? get error => _resource.error;
|
||||
|
||||
void updateAccount(AccountProvider accountProvider) {
|
||||
if (!identical(_accountProvider, accountProvider)) {
|
||||
_accountProvider = accountProvider;
|
||||
}
|
||||
_triggerLoadIfNeeded(accountProvider);
|
||||
}
|
||||
|
||||
void _setResource(Resource<List<Organization>> newResource) {
|
||||
_resource = newResource;
|
||||
notifyListeners();
|
||||
@@ -52,6 +66,14 @@ class OrganizationsProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
void _triggerLoadIfNeeded(AccountProvider accountProvider) {
|
||||
if (accountProvider.authState != AuthState.ready) return;
|
||||
if (accountProvider.account == null) return;
|
||||
if (isLoading || isOrganizationSet) return;
|
||||
if (error != null) return;
|
||||
unawaited(load());
|
||||
}
|
||||
|
||||
Future<Organization> loadByInvitation(String invitationRef) async {
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
@@ -88,4 +110,55 @@ class OrganizationsProvider extends ChangeNotifier {
|
||||
// Best-effort cleanup of stored selection to avoid using stale org on next login.
|
||||
await SecureStorageService.delete(Constants.currentOrgKey);
|
||||
}
|
||||
|
||||
Future<Organization> uploadLogo(XFile logoFile) async {
|
||||
if (!isOrganizationSet) {
|
||||
throw StateError('Organization is not set');
|
||||
}
|
||||
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
final updated = await OrganizationService.uploadLogoAndUpdate(current, logoFile);
|
||||
final updatedList = organizations
|
||||
.map((org) => org.id == updated.id ? updated : org)
|
||||
.toList(growable: false);
|
||||
_setResource(Resource(data: updatedList, isLoading: false));
|
||||
_currentOrg = updated.id;
|
||||
return updated;
|
||||
} catch (e) {
|
||||
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Organization> updateCurrent({
|
||||
String? name,
|
||||
String? description,
|
||||
String? timeZone,
|
||||
String? logoUrl,
|
||||
}) async {
|
||||
if (!isOrganizationSet) {
|
||||
throw StateError('Organization is not set');
|
||||
}
|
||||
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
final updated = await OrganizationService.updateSettings(
|
||||
current,
|
||||
name: name,
|
||||
description: description,
|
||||
timeZone: timeZone,
|
||||
logoUrl: logoUrl,
|
||||
);
|
||||
final updatedList = organizations
|
||||
.map((org) => org.id == updated.id ? updated : org)
|
||||
.toList(growable: false);
|
||||
_setResource(Resource(data: updatedList, isLoading: false));
|
||||
_currentOrg = updated.id;
|
||||
return updated;
|
||||
} catch (e) {
|
||||
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user