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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,130 +32,137 @@ class _AddPageState extends State<AddPage> {
|
|||
// builder: (context, screen, child) {
|
||||
return Scaffold(
|
||||
bottomNavigationBar: AppNavigationBar(startIndex: 1),
|
||||
body: Stack(
|
||||
children: [
|
||||
ColoredBox(color: Colors.black),
|
||||
MobileScanner(
|
||||
controller: controller,
|
||||
onDetect: (barcodes) async {
|
||||
if (widget.viewModel.currentOwner == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Attention : vous devez choisir un·e propriétaire",
|
||||
),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
void setPrice(num newPrice) async {
|
||||
setState(() {
|
||||
price = newPrice;
|
||||
});
|
||||
}
|
||||
|
||||
Result<Book> result = await widget.viewModel.scanBook(barcodes);
|
||||
|
||||
switch (result) {
|
||||
case Ok():
|
||||
await _confirmationDialogBuilder(
|
||||
context,
|
||||
setPrice,
|
||||
controller,
|
||||
widget.viewModel,
|
||||
result.value,
|
||||
);
|
||||
break;
|
||||
case Error():
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("Erreur : ${result.error}"),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
},
|
||||
),
|
||||
SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Center(
|
||||
child: Card(
|
||||
margin: EdgeInsets.symmetric(horizontal: 50),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListenableBuilder(
|
||||
listenable: widget.viewModel,
|
||||
builder: (context, child) => ListTile(
|
||||
leading: Icon(Icons.person),
|
||||
title: TextButton(
|
||||
child: Text(
|
||||
(widget.viewModel.currentOwner == null)
|
||||
? "Aucun"
|
||||
: "${widget.viewModel.currentOwner!.firstName} ${widget.viewModel.currentOwner!.lastName}",
|
||||
),
|
||||
onPressed: () => _ownerDialogBuilder(
|
||||
context,
|
||||
controller,
|
||||
widget.viewModel,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.attach_money),
|
||||
title: TextButton(
|
||||
child: Text(
|
||||
(widget.viewModel.askPrice)
|
||||
? "Demander à chaque fois"
|
||||
: "Prix libre toujours",
|
||||
),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
widget.viewModel.askPrice =
|
||||
!widget.viewModel.askPrice;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
body: ListenableBuilder(
|
||||
listenable: widget.viewModel,
|
||||
builder: (context, child) => switch (widget.viewModel.isLoaded) {
|
||||
false => CircularProgressIndicator(),
|
||||
true => Stack(
|
||||
children: [
|
||||
ColoredBox(color: Colors.black),
|
||||
MobileScanner(
|
||||
controller: controller,
|
||||
onDetect: (barcodes) async {
|
||||
if (widget.viewModel.currentOwner == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Attention : vous devez choisir un·e propriétaire",
|
||||
),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 100),
|
||||
SvgPicture.asset('assets/scan-overlay.svg'),
|
||||
],
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
void setPrice(num newPrice) async {
|
||||
setState(() {
|
||||
price = newPrice;
|
||||
});
|
||||
}
|
||||
|
||||
Result<Book> result = await widget.viewModel.scanBook(
|
||||
barcodes,
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok():
|
||||
await _confirmationDialogBuilder(
|
||||
context,
|
||||
setPrice,
|
||||
controller,
|
||||
widget.viewModel,
|
||||
result.value,
|
||||
);
|
||||
break;
|
||||
case Error():
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("Erreur : ${result.error}"),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
SafeArea(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Center(
|
||||
child: TextButton(
|
||||
style: ButtonStyle(
|
||||
backgroundColor: WidgetStatePropertyAll(theme.cardColor),
|
||||
),
|
||||
onPressed: () => _formDialogBuilder(
|
||||
context,
|
||||
controller,
|
||||
widget.viewModel,
|
||||
),
|
||||
child: Text("Enregistrer manuellement"),
|
||||
SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Center(
|
||||
child: Card(
|
||||
margin: EdgeInsets.symmetric(horizontal: 50),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListTile(
|
||||
leading: Icon(Icons.person),
|
||||
title: TextButton(
|
||||
child: Text(
|
||||
(widget.viewModel.currentOwner == null)
|
||||
? "Aucun"
|
||||
: "${widget.viewModel.currentOwner!.firstName} ${widget.viewModel.currentOwner!.lastName}",
|
||||
),
|
||||
onPressed: () => _ownerDialogBuilder(
|
||||
context,
|
||||
controller,
|
||||
widget.viewModel,
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.attach_money),
|
||||
title: TextButton(
|
||||
child: Text(
|
||||
(widget.viewModel.askPrice)
|
||||
? "Demander à chaque fois"
|
||||
: "Prix libre toujours",
|
||||
),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
widget.viewModel.askPrice =
|
||||
!widget.viewModel.askPrice;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 100),
|
||||
SvgPicture.asset('assets/scan-overlay.svg'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SafeArea(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Center(
|
||||
child: TextButton(
|
||||
style: ButtonStyle(
|
||||
backgroundColor: WidgetStatePropertyAll(
|
||||
theme.cardColor,
|
||||
),
|
||||
),
|
||||
onPressed: () => _formDialogBuilder(
|
||||
context,
|
||||
controller,
|
||||
widget.viewModel,
|
||||
),
|
||||
child: Text("Enregistrer manuellement"),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
);
|
||||
// },
|
||||
|
|
|
|||
|
|
@ -141,11 +141,14 @@ class _OwnerPopupState extends State<OwnerPopup> {
|
|||
),
|
||||
SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
onPressed: () async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_formKey.currentState!.save();
|
||||
widget.viewModel.currentOwner = widget.viewModel
|
||||
.addOwner(firstName!, lastName!, contact!);
|
||||
await widget.viewModel.addOwner(
|
||||
firstName!,
|
||||
lastName!,
|
||||
contact!,
|
||||
);
|
||||
setState(() {
|
||||
showNewOwner = false;
|
||||
});
|
||||
|
|
|
|||
Reference in a new issue