feat: added authentification and redirection
This commit is contained in:
parent
1c9c5ce5fe
commit
ef641d4023
24 changed files with 731 additions and 173 deletions
|
|
@ -1,12 +1,19 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||
import 'package:seshat/data/repositories/owner_repository.dart';
|
||||
import 'package:seshat/domain/models/book.dart';
|
||||
import 'package:seshat/domain/models/owner.dart';
|
||||
import 'package:seshat/utils/command.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
class AddViewModel extends ChangeNotifier {
|
||||
AddViewModel();
|
||||
AddViewModel({required OwnerRepository ownerRepository})
|
||||
: _ownerRepository = ownerRepository {
|
||||
load = Command0(_load)..execute();
|
||||
}
|
||||
|
||||
final OwnerRepository _ownerRepository;
|
||||
|
||||
/*
|
||||
* ====================
|
||||
|
|
@ -21,37 +28,37 @@ class AddViewModel extends ChangeNotifier {
|
|||
notifyListeners();
|
||||
}
|
||||
|
||||
final List<Owner> _owners = [];
|
||||
List<Owner> _owners = [];
|
||||
|
||||
List<Owner>? get owners => _owners;
|
||||
|
||||
Owner addOwner(String firstName, String lastName, String contact) {
|
||||
if (_owners.isEmpty) {
|
||||
_owners.add(
|
||||
Owner(
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
contact: contact,
|
||||
id: 1,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
_owners.add(
|
||||
Owner(
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
contact: contact,
|
||||
id: _owners.last.id + 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
notifyListeners();
|
||||
return Owner(
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
contact: contact,
|
||||
id: 0,
|
||||
Future<Result<Owner>> addOwner(
|
||||
String firstName,
|
||||
String lastName,
|
||||
String contact,
|
||||
) async {
|
||||
final result = await _ownerRepository.postOwner(
|
||||
firstName,
|
||||
lastName,
|
||||
contact,
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok():
|
||||
final secondResult = await _ownerRepository.getOwners();
|
||||
|
||||
switch (secondResult) {
|
||||
case Ok():
|
||||
_owners = secondResult.value;
|
||||
_currentOwner = result.value;
|
||||
notifyListeners();
|
||||
return Result.ok(result.value);
|
||||
case Error():
|
||||
return Result.error(secondResult.error);
|
||||
}
|
||||
case Error():
|
||||
return Result.error(result.error);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -87,8 +94,38 @@ class AddViewModel extends ChangeNotifier {
|
|||
);
|
||||
}
|
||||
|
||||
/// Sens an api request with
|
||||
/// Sends an api request with
|
||||
// Result<BookInstance> newBookInstance() {
|
||||
|
||||
// };
|
||||
|
||||
/*
|
||||
* =================================
|
||||
* =====[ COMMAND AND LOADING ]=====
|
||||
* =================================
|
||||
*/
|
||||
|
||||
late final Command0 load;
|
||||
bool isLoaded = false;
|
||||
|
||||
Future<Result<void>> _load() async {
|
||||
return await _loadOwners();
|
||||
}
|
||||
|
||||
Future<Result<void>> _loadOwners() async {
|
||||
final result = await _ownerRepository.getOwners();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_owners = result.value;
|
||||
isLoaded = true;
|
||||
case Error():
|
||||
debugPrint("Oupsie daysie, ${result.error}");
|
||||
}
|
||||
notifyListeners();
|
||||
_ownerRepository.liveOwners().listen((Owner owner) {
|
||||
_owners.add(owner);
|
||||
notifyListeners();
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue