151 lines
3.6 KiB
Dart
151 lines
3.6 KiB
Dart
import 'dart:async';
|
|
|
|
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({required OwnerRepository ownerRepository})
|
|
: _ownerRepository = ownerRepository {
|
|
load = Command0(_load)..execute();
|
|
}
|
|
|
|
final OwnerRepository _ownerRepository;
|
|
late final StreamSubscription sub;
|
|
|
|
/*
|
|
* ====================
|
|
* =====[ OWNERS ]=====
|
|
* ====================
|
|
*/
|
|
|
|
Owner? _currentOwner;
|
|
Owner? get currentOwner => _currentOwner;
|
|
set currentOwner(Owner? owner) {
|
|
_currentOwner = owner;
|
|
notifyListeners();
|
|
}
|
|
|
|
List<Owner> _owners = [];
|
|
|
|
List<Owner>? get owners => _owners;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* ===================
|
|
* =====[ PRICE ]=====
|
|
* ===================
|
|
*/
|
|
|
|
bool _askPrice = true;
|
|
bool get askPrice => _askPrice;
|
|
set askPrice(bool newValue) {
|
|
_askPrice = newValue;
|
|
notifyListeners();
|
|
}
|
|
|
|
/*
|
|
* =================================
|
|
* =====[ BOOKS AND INSTANCES ]=====
|
|
* =================================
|
|
*/
|
|
|
|
/// Sends an api request with a [bacorde], then gets the [Book] that was
|
|
/// either created or retrieved. Sens the [Book] back wrapped in a [Result].
|
|
Future<Result<Book>> scanBook(BarcodeCapture barcode) async {
|
|
return Result.ok(
|
|
Book(
|
|
author: "Patrick K. Dewdney",
|
|
ean: barcode.barcodes.first.rawValue!,
|
|
id: 56,
|
|
priceNew: "50 EUR",
|
|
title: "Les chiens et la charrue",
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 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
|
|
..sort(
|
|
(a, b) => "${a.firstName} ${a.lastName}".compareTo(
|
|
"${b.firstName} ${b.lastName}",
|
|
),
|
|
);
|
|
isLoaded = true;
|
|
case Error():
|
|
debugPrint("Oupsie daysie, ${result.error}");
|
|
}
|
|
notifyListeners();
|
|
sub = _ownerRepository.liveOwners.listen((Owner owner) {
|
|
debugPrint("\n\n\n\n[5] Updated UI : $owner\n\n\n\n");
|
|
_owners.add(owner);
|
|
_owners.sort(
|
|
(a, b) => "${a.firstName} ${a.lastName}".compareTo(
|
|
"${b.firstName} ${b.lastName}",
|
|
),
|
|
);
|
|
notifyListeners();
|
|
});
|
|
return result;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
sub.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|