Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix current ward loading #487

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions apps/tasks/lib/services/current_ward_svc.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tasks/config/config.dart';
Expand All @@ -22,6 +23,8 @@ class CurrentWardInformation {

String get organizationName => "${organization.name} (${organization.shortName})";

bool get isEmpty => wardId == "" || organizationId == "";

bool get hasFullInformation => ward.name != "" && organization.name != "";

CurrentWardInformation(this.ward, this.organization);
Expand Down Expand Up @@ -74,17 +77,17 @@ class _CurrentWardPreferences {
///
/// Changes the [CurrentWardInformation] globally
class CurrentWardService extends Listenable {
/// Whether this Controller has been initialized
bool _isInitialized = false;

/// A storage for the current ward
final _CurrentWardPreferences _preferences = _CurrentWardPreferences();

/// The current ward information
CurrentWardInformation? _currentWard;

/// Whether this Controller has been initialized
bool get isInitialized => _isInitialized;
bool get isInitialized => currentWard != null && !currentWard!.isEmpty;

/// Whether all information are loaded
bool get isLoaded => currentWard != null && currentWard!.hasFullInformation;

/// Listeners
final List<VoidCallback> _listeners = [];
Expand All @@ -108,24 +111,26 @@ class CurrentWardService extends Listenable {
}
}
_currentWard = currentWard;
_isInitialized = _currentWard != null;
if(_currentWard != null && !_currentWard!.hasFullInformation){
if(!isLoaded){
fetch();
}
if(kDebugMode){
print(currentWard);
}
notifyListeners();
}

CurrentWardInformation? get currentWard => _currentWard;

/// Load the preferences with the [ThemePreferences]
/// Load the preferences with the [_CurrentWardPreferences]
Future<void> load() async {
// everything is done in the setter
currentWard = await _preferences.getInformation();
currentWard = DEV_MODE ? null : await _preferences.getInformation();
}

/// Fetch [Ward] and [Organization] from backend
Future<void> fetch() async {
if(!_isInitialized || _currentWard == null){
if(!isInitialized){
return;
}
Organization organization = await OrganizationService().getOrganization(id: currentWard!.organizationId);
Expand All @@ -136,7 +141,6 @@ class CurrentWardService extends Listenable {

/// Clears the [CurrentWardInformation]
void clear() {
_isInitialized = false;
currentWard = null;
}

Expand Down Expand Up @@ -179,7 +183,7 @@ class CurrentWardController extends ChangeNotifier {

CurrentWardInformation? get currentWard => service.currentWard;

/// Load the preferences with the [ThemePreferences]
/// Load the information
Future<void> load() async {
service.load();
}
Expand Down
2 changes: 1 addition & 1 deletion apps/tasks/lib/services/organization_svc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OrganizationService {
GetOrganizationRequest request = GetOrganizationRequest(id: id);
GetOrganizationResponse response = await organizationService.getOrganization(
request,
options: CallOptions(metadata: GRPCClientService().getUserServiceMetaData()),
options: CallOptions(metadata: GRPCClientService().getUserServiceMetaData(organizationId: id)),
);

// TODO use full information of request
Expand Down
6 changes: 2 additions & 4 deletions apps/tasks/lib/services/user_session_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class UserSessionService {

static final UserSessionService _userSessionService = UserSessionService._ensureInitialized();

UserSessionService._ensureInitialized() {
_hasTriedTokens = DEV_MODE;
}
UserSessionService._ensureInitialized();

factory UserSessionService() => _userSessionService;

Expand All @@ -33,7 +31,7 @@ class UserSessionService {
if (!DEV_MODE) {
_identity = await _authService.tokenLogin();
// new login required thus delete all saved information
if(_identity == null){
if (_identity == null) {
_authService.clearFromStorage();
CurrentWardService().clear();
}
Expand Down
Loading