feat: honestly forgot
This commit is contained in:
parent
48bcf0b1f8
commit
da953ba651
19 changed files with 1097 additions and 244 deletions
46
<
Normal file
46
<
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
enum State { pending, ongoing, ended }
|
||||||
|
|
||||||
|
class Bal {
|
||||||
|
Bal({
|
||||||
|
required this.id,
|
||||||
|
required this.name,
|
||||||
|
required this.state,
|
||||||
|
required this.startTime,
|
||||||
|
required this.endTime,
|
||||||
|
});
|
||||||
|
|
||||||
|
int id;
|
||||||
|
String name;
|
||||||
|
State state;
|
||||||
|
DateTime startTime;
|
||||||
|
DateTime endTime;
|
||||||
|
|
||||||
|
factory Bal.fromJSON(Map<String, dynamic> json) => Bal(
|
||||||
|
id: json["id"],
|
||||||
|
name: json["name"],
|
||||||
|
state: switch (json["state"]) {
|
||||||
|
"Pending" => State.pending,
|
||||||
|
"Ongoing" => State.ongoing,
|
||||||
|
_ => State.ended,
|
||||||
|
},
|
||||||
|
startTime: DateTime.fromMillisecondsSinceEpoch(
|
||||||
|
json["start_timestamp"] * 1000,
|
||||||
|
isUtc: true,
|
||||||
|
),
|
||||||
|
endTime: DateTime.fromMillisecondsSinceEpoch(
|
||||||
|
json["end_timestamp"] * 1000,
|
||||||
|
isUtc: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
int compareTo(Bal other) {
|
||||||
|
if (ended == other.ended) {
|
||||||
|
return 0;
|
||||||
|
} else if (ended) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import "package:provider/provider.dart";
|
import "package:provider/provider.dart";
|
||||||
import "package:provider/single_child_widget.dart";
|
import "package:provider/single_child_widget.dart";
|
||||||
import "package:seshat/data/repositories/auth_repository.dart";
|
import "package:seshat/data/repositories/auth_repository.dart";
|
||||||
|
import "package:seshat/data/repositories/bal_repository.dart";
|
||||||
import "package:seshat/data/repositories/book_instance_repository.dart";
|
import "package:seshat/data/repositories/book_instance_repository.dart";
|
||||||
import "package:seshat/data/repositories/book_repository.dart";
|
import "package:seshat/data/repositories/book_repository.dart";
|
||||||
|
|
||||||
|
|
@ -25,5 +26,6 @@ List<SingleChildWidget> get providers {
|
||||||
Provider(
|
Provider(
|
||||||
create: (context) => BookInstanceRepository(apiClient: context.read()),
|
create: (context) => BookInstanceRepository(apiClient: context.read()),
|
||||||
),
|
),
|
||||||
|
Provider(create: (context) => BalRepository(apiClient: context.read())),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
51
lib/data/repositories/bal_repository.dart
Normal file
51
lib/data/repositories/bal_repository.dart
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
import 'package:seshat/data/services/api_client.dart';
|
||||||
|
import 'package:seshat/domain/models/bal.dart';
|
||||||
|
import 'package:seshat/utils/result.dart';
|
||||||
|
|
||||||
|
class BalRepository {
|
||||||
|
BalRepository({required ApiClient apiClient}) : _apiClient = apiClient;
|
||||||
|
|
||||||
|
final ApiClient _apiClient;
|
||||||
|
List<Bal>? _bals;
|
||||||
|
|
||||||
|
Future<Result<List<Bal>>> getBals() async {
|
||||||
|
if (_bals != null) {
|
||||||
|
return Result.ok(_bals!);
|
||||||
|
}
|
||||||
|
final result = await _apiClient.getBals();
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
_bals = result.value;
|
||||||
|
return Result.ok(result.value);
|
||||||
|
case Error():
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<Bal>> balById(int id) async {
|
||||||
|
if (_bals == null) {
|
||||||
|
await getBals();
|
||||||
|
}
|
||||||
|
Bal? bal = _bals!.where((bal) => bal.id == id).firstOrNull;
|
||||||
|
if (bal != null) {
|
||||||
|
return Result.ok(bal);
|
||||||
|
}
|
||||||
|
final result = await _apiClient.getBalById(id);
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
return Result.ok(result.value);
|
||||||
|
case Error():
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<void>> addBal(String name) async {
|
||||||
|
final result = await _apiClient.addBal(name);
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
return result;
|
||||||
|
case Error():
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart';
|
||||||
import 'package:seshat/config/constants.dart';
|
import 'package:seshat/config/constants.dart';
|
||||||
|
|
@ -35,6 +36,106 @@ class ApiClient {
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =================
|
||||||
|
* =====[ BAL ]=====
|
||||||
|
* =================
|
||||||
|
*/
|
||||||
|
|
||||||
|
Future<Result<Bal>> getBalById(int id) async {
|
||||||
|
final client = Client();
|
||||||
|
try {
|
||||||
|
final headers = await _getHeaders();
|
||||||
|
final response = await client.get(
|
||||||
|
Uri.parse("https://$apiBasePath/bal/${id.toString()}"),
|
||||||
|
headers: headers,
|
||||||
|
);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final json = jsonDecode(response.body);
|
||||||
|
return Result.ok(Bal.fromJSON(json));
|
||||||
|
} else if (response.statusCode == 403) {
|
||||||
|
return Result.error(Exception("You don't own the specified bal"));
|
||||||
|
} else {
|
||||||
|
return Result.error(
|
||||||
|
Exception("No bal wirth this id exists the database"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return Result.error(Exception(e));
|
||||||
|
} finally {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<Bal>> addBal(String name) async {
|
||||||
|
final client = Client();
|
||||||
|
try {
|
||||||
|
final headers = await _getHeaders({"Content-Type": "application/json"});
|
||||||
|
final body = {"name": name};
|
||||||
|
final response = await client.post(
|
||||||
|
Uri.parse("https://$apiBasePath/bal"),
|
||||||
|
headers: headers,
|
||||||
|
body: jsonEncode(body),
|
||||||
|
);
|
||||||
|
if (response.statusCode == 201) {
|
||||||
|
final json = jsonDecode(response.body);
|
||||||
|
return Result.ok(Bal.fromJSON(json));
|
||||||
|
} else {
|
||||||
|
return Result.error(Exception("Something went wrong"));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint("\n\n\n\n${e.toString()}\n\n\n\n");
|
||||||
|
return Result.error(Exception(e));
|
||||||
|
} finally {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<List<Bal>>> getBals() async {
|
||||||
|
final client = Client();
|
||||||
|
try {
|
||||||
|
final headers = await _getHeaders();
|
||||||
|
final response = await client.get(
|
||||||
|
Uri.parse("https://$apiBasePath/bals"),
|
||||||
|
headers: headers,
|
||||||
|
);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final json = jsonDecode(response.body) as List<dynamic>;
|
||||||
|
debugPrint("\n\n\n\nRECEIVED $json\n\n\n\n");
|
||||||
|
return Result.ok(json.map((element) => Bal.fromJSON(element)).toList());
|
||||||
|
} else {
|
||||||
|
return Result.error(Exception("Something wrong happened"));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return Result.error(Exception(e));
|
||||||
|
} finally {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<Bal?>> getCurrentBal() async {
|
||||||
|
final client = Client();
|
||||||
|
try {
|
||||||
|
final headers = await _getHeaders();
|
||||||
|
final response = await client.get(
|
||||||
|
Uri.parse("https://$apiBasePath/bal/current"),
|
||||||
|
headers: headers,
|
||||||
|
);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final json = jsonDecode(response.body);
|
||||||
|
return Result.ok(Bal.fromJSON(json));
|
||||||
|
} else if (response.statusCode == 404) {
|
||||||
|
return Result.ok(null);
|
||||||
|
} else {
|
||||||
|
return Result.error(Exception("Something went wrong"));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return Result.error(Exception(e));
|
||||||
|
} finally {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ===================
|
* ===================
|
||||||
* =====[ BOOKS ]=====
|
* =====[ BOOKS ]=====
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,44 @@
|
||||||
|
enum BalState { pending, ongoing, ended }
|
||||||
|
|
||||||
class Bal {
|
class Bal {
|
||||||
Bal({required this.id});
|
Bal({
|
||||||
|
required this.id,
|
||||||
|
required this.name,
|
||||||
|
required this.state,
|
||||||
|
required this.startTime,
|
||||||
|
required this.endTime,
|
||||||
|
});
|
||||||
|
|
||||||
int id;
|
int id;
|
||||||
|
String name;
|
||||||
|
BalState state;
|
||||||
|
DateTime startTime;
|
||||||
|
DateTime endTime;
|
||||||
|
|
||||||
|
factory Bal.fromJSON(Map<String, dynamic> json) => Bal(
|
||||||
|
id: json["id"],
|
||||||
|
name: json["name"],
|
||||||
|
state: switch (json["state"]) {
|
||||||
|
"Pending" => BalState.pending,
|
||||||
|
"Ongoing" => BalState.ongoing,
|
||||||
|
_ => BalState.ended,
|
||||||
|
},
|
||||||
|
startTime: DateTime.fromMillisecondsSinceEpoch(
|
||||||
|
json["start_timestamp"] * 1000,
|
||||||
|
isUtc: true,
|
||||||
|
),
|
||||||
|
endTime: DateTime.fromMillisecondsSinceEpoch(
|
||||||
|
json["end_timestamp"] * 1000,
|
||||||
|
isUtc: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
int compareTo(Bal other) {
|
||||||
|
if (state.index == other.state.index) {
|
||||||
|
return 0;
|
||||||
|
} else if (state.index > other.state.index) {
|
||||||
|
return state.index;
|
||||||
|
}
|
||||||
|
return -state.index;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,10 @@ import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
|
||||||
import 'package:seshat/ui/add_page/widgets/add_page.dart';
|
import 'package:seshat/ui/add_page/widgets/add_page.dart';
|
||||||
import 'package:seshat/ui/auth/viewmodel/login_view_model.dart';
|
import 'package:seshat/ui/auth/viewmodel/login_view_model.dart';
|
||||||
import 'package:seshat/ui/auth/widgets/login_page.dart';
|
import 'package:seshat/ui/auth/widgets/login_page.dart';
|
||||||
import 'package:seshat/ui/home_page/home_page.dart';
|
import 'package:seshat/ui/bal_page/view_model/bal_view_model.dart';
|
||||||
|
import 'package:seshat/ui/bal_page/widget/bal_screen.dart';
|
||||||
|
import 'package:seshat/ui/home_page/view_model/home_view_model.dart';
|
||||||
|
import 'package:seshat/ui/home_page/widgets/home_page.dart';
|
||||||
import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart';
|
import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart';
|
||||||
import 'package:seshat/ui/sell_page/widgets/sell_page.dart';
|
import 'package:seshat/ui/sell_page/widgets/sell_page.dart';
|
||||||
|
|
||||||
|
|
@ -30,8 +33,22 @@ GoRouter router(AuthRepository authRepository) => GoRouter(
|
||||||
routes: [
|
routes: [
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: Routes.home,
|
path: Routes.home,
|
||||||
pageBuilder: (context, state) => NoTransitionPage(child: HomePage()),
|
pageBuilder: (context, state) {
|
||||||
|
final viewModel = HomeViewModel(balRepository: context.read());
|
||||||
|
return NoTransitionPage(child: HomePage(viewModel: viewModel));
|
||||||
|
},
|
||||||
routes: [
|
routes: [
|
||||||
|
GoRoute(
|
||||||
|
path: Routes.balPage,
|
||||||
|
name: "bal",
|
||||||
|
pageBuilder: (context, state) {
|
||||||
|
final viewModel = BalViewModel(
|
||||||
|
balRepository: context.read(),
|
||||||
|
id: int.tryParse(state.pathParameters["id"] ?? ""),
|
||||||
|
);
|
||||||
|
return NoTransitionPage(child: BalScreen(viewModel: viewModel));
|
||||||
|
},
|
||||||
|
),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: Routes.add,
|
path: Routes.add,
|
||||||
pageBuilder: (context, state) {
|
pageBuilder: (context, state) {
|
||||||
|
|
@ -39,6 +56,7 @@ GoRouter router(AuthRepository authRepository) => GoRouter(
|
||||||
ownerRepository: context.read(),
|
ownerRepository: context.read(),
|
||||||
bookRepository: context.read(),
|
bookRepository: context.read(),
|
||||||
bookInstanceRepository: context.read(),
|
bookInstanceRepository: context.read(),
|
||||||
|
balRepository: context.read(),
|
||||||
);
|
);
|
||||||
return NoTransitionPage(child: AddPage(viewModel: viewModel));
|
return NoTransitionPage(child: AddPage(viewModel: viewModel));
|
||||||
},
|
},
|
||||||
|
|
@ -51,7 +69,7 @@ GoRouter router(AuthRepository authRepository) => GoRouter(
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: Routes.sell,
|
path: Routes.sell,
|
||||||
pageBuilder: (context, state) {
|
pageBuilder: (context, state) {
|
||||||
final viewModel = SellViewModel();
|
final viewModel = SellViewModel(balRepository: context.read());
|
||||||
return NoTransitionPage(child: SellPage(viewModel: viewModel));
|
return NoTransitionPage(child: SellPage(viewModel: viewModel));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
abstract final class Routes {
|
abstract final class Routes {
|
||||||
// ==[ HOME ]==
|
// ==[ HOME ]==
|
||||||
static const home = '/';
|
static const home = '/';
|
||||||
|
static const balPage = '/bal/:id';
|
||||||
|
|
||||||
// ==[ ADD ]==
|
// ==[ ADD ]==
|
||||||
static const add = '/add';
|
static const add = '/add';
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import 'dart:async';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||||
|
import 'package:seshat/data/repositories/bal_repository.dart';
|
||||||
import 'package:seshat/data/repositories/book_instance_repository.dart';
|
import 'package:seshat/data/repositories/book_instance_repository.dart';
|
||||||
import 'package:seshat/data/repositories/book_repository.dart';
|
import 'package:seshat/data/repositories/book_repository.dart';
|
||||||
import 'package:seshat/data/repositories/owner_repository.dart';
|
import 'package:seshat/data/repositories/owner_repository.dart';
|
||||||
|
|
@ -18,16 +19,18 @@ class AddViewModel extends ChangeNotifier {
|
||||||
required OwnerRepository ownerRepository,
|
required OwnerRepository ownerRepository,
|
||||||
required BookRepository bookRepository,
|
required BookRepository bookRepository,
|
||||||
required BookInstanceRepository bookInstanceRepository,
|
required BookInstanceRepository bookInstanceRepository,
|
||||||
|
required BalRepository balRepository,
|
||||||
}) : _ownerRepository = ownerRepository,
|
}) : _ownerRepository = ownerRepository,
|
||||||
_bookRepository = bookRepository,
|
_bookRepository = bookRepository,
|
||||||
_bookInstanceRepository = bookInstanceRepository {
|
_bookInstanceRepository = bookInstanceRepository,
|
||||||
|
_balRepository = balRepository {
|
||||||
load = Command0(_load)..execute();
|
load = Command0(_load)..execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
final OwnerRepository _ownerRepository;
|
final OwnerRepository _ownerRepository;
|
||||||
final BookRepository _bookRepository;
|
final BookRepository _bookRepository;
|
||||||
final BookInstanceRepository _bookInstanceRepository;
|
final BookInstanceRepository _bookInstanceRepository;
|
||||||
late final StreamSubscription sub;
|
final BalRepository _balRepository;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ====================
|
* ====================
|
||||||
|
|
@ -73,6 +76,15 @@ class AddViewModel extends ChangeNotifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =================
|
||||||
|
* =====[ BAL ]=====
|
||||||
|
* =================
|
||||||
|
*/
|
||||||
|
|
||||||
|
Bal? _currentBal;
|
||||||
|
Bal? get currentBal => _currentBal;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ===================
|
* ===================
|
||||||
* =====[ PRICE ]=====
|
* =====[ PRICE ]=====
|
||||||
|
|
@ -109,11 +121,6 @@ class AddViewModel extends ChangeNotifier {
|
||||||
return await _bookInstanceRepository.sendBook(book, owner, bal, price);
|
return await _bookInstanceRepository.sendBook(book, owner, bal, price);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends an api request with
|
|
||||||
// Result<BookInstance> newBookInstance() {
|
|
||||||
|
|
||||||
// };
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* =================================
|
* =================================
|
||||||
* =====[ COMMAND AND LOADING ]=====
|
* =====[ COMMAND AND LOADING ]=====
|
||||||
|
|
@ -124,7 +131,32 @@ class AddViewModel extends ChangeNotifier {
|
||||||
bool isLoaded = false;
|
bool isLoaded = false;
|
||||||
|
|
||||||
Future<Result<void>> _load() async {
|
Future<Result<void>> _load() async {
|
||||||
return await _loadOwners();
|
final result1 = await _loadOwners();
|
||||||
|
switch (result1) {
|
||||||
|
case Error():
|
||||||
|
return result1;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
final result2 = await _loadBal();
|
||||||
|
isLoaded = true;
|
||||||
|
notifyListeners();
|
||||||
|
return result2;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<void>> _loadBal() async {
|
||||||
|
final result = await _balRepository.getBals();
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
_currentBal = result.value
|
||||||
|
.where((bal) => bal.state == BalState.ongoing)
|
||||||
|
.firstOrNull;
|
||||||
|
break;
|
||||||
|
case Error():
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<void>> _loadOwners() async {
|
Future<Result<void>> _loadOwners() async {
|
||||||
|
|
@ -137,18 +169,10 @@ class AddViewModel extends ChangeNotifier {
|
||||||
"${b.firstName} ${b.lastName}",
|
"${b.firstName} ${b.lastName}",
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
isLoaded = true;
|
|
||||||
case Error():
|
case Error():
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
notifyListeners();
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
sub.cancel();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_svg/svg.dart';
|
import 'package:flutter_svg/svg.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||||
import 'package:seshat/domain/models/book.dart';
|
import 'package:seshat/domain/models/book.dart';
|
||||||
|
import 'package:seshat/routing/routes.dart';
|
||||||
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
|
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
|
||||||
import 'package:seshat/ui/add_page/widgets/form_popup.dart';
|
import 'package:seshat/ui/add_page/widgets/form_popup.dart';
|
||||||
import 'package:seshat/ui/add_page/widgets/owner_popup.dart';
|
import 'package:seshat/ui/add_page/widgets/owner_popup.dart';
|
||||||
|
|
@ -36,133 +38,163 @@ class _AddPageState extends State<AddPage> {
|
||||||
listenable: widget.viewModel,
|
listenable: widget.viewModel,
|
||||||
builder: (context, child) => switch (widget.viewModel.isLoaded) {
|
builder: (context, child) => switch (widget.viewModel.isLoaded) {
|
||||||
false => Center(child: CircularProgressIndicator()),
|
false => Center(child: CircularProgressIndicator()),
|
||||||
true => Stack(
|
true => switch (widget.viewModel.currentBal) {
|
||||||
children: [
|
null => Center(
|
||||||
ColoredBox(color: Colors.black),
|
child: SizedBox(
|
||||||
MobileScanner(
|
width: 300,
|
||||||
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: [
|
|
||||||
SizedBox(height: 5),
|
|
||||||
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;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Center(child: SvgPicture.asset('assets/scan-overlay.svg')),
|
|
||||||
SafeArea(
|
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
Center(
|
Text(
|
||||||
child: TextButton(
|
"Aucune bal n'est active.",
|
||||||
style: ButtonStyle(
|
style: TextStyle(fontSize: 25),
|
||||||
backgroundColor: WidgetStatePropertyAll(
|
textAlign: TextAlign.center,
|
||||||
theme.cardColor,
|
),
|
||||||
),
|
SizedBox(height: 15),
|
||||||
),
|
Text(
|
||||||
onPressed: () => _formDialogBuilder(
|
"Vous devez créer puis activer une BAL pour pouvoir scanner des livres.",
|
||||||
context,
|
textAlign: TextAlign.center,
|
||||||
controller,
|
),
|
||||||
widget.viewModel,
|
SizedBox(height: 30),
|
||||||
),
|
ElevatedButton(
|
||||||
child: Text("Enregistrer manuellement"),
|
onPressed: () {
|
||||||
),
|
context.go(Routes.home);
|
||||||
|
},
|
||||||
|
child: Text("Gérer les BALs"),
|
||||||
),
|
),
|
||||||
SizedBox(height: 5),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
),
|
||||||
),
|
_ => 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: [
|
||||||
|
SizedBox(height: 5),
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Center(child: 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"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 5),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:seshat/domain/models/bal.dart';
|
|
||||||
import 'package:seshat/domain/models/book.dart';
|
import 'package:seshat/domain/models/book.dart';
|
||||||
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
|
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
|
||||||
import 'package:seshat/utils/result.dart';
|
import 'package:seshat/utils/result.dart';
|
||||||
|
|
@ -117,7 +116,7 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
||||||
var result = await widget.viewModel.sendBook(
|
var result = await widget.viewModel.sendBook(
|
||||||
widget.book,
|
widget.book,
|
||||||
widget.viewModel.currentOwner!,
|
widget.viewModel.currentOwner!,
|
||||||
Bal(id: 1),
|
widget.viewModel.currentBal!,
|
||||||
price,
|
price,
|
||||||
);
|
);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
|
|
|
||||||
56
lib/ui/bal_page/view_model/bal_view_model.dart
Normal file
56
lib/ui/bal_page/view_model/bal_view_model.dart
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:seshat/data/repositories/bal_repository.dart';
|
||||||
|
import 'package:seshat/domain/models/bal.dart';
|
||||||
|
import 'package:seshat/utils/command.dart';
|
||||||
|
import 'package:seshat/utils/result.dart';
|
||||||
|
|
||||||
|
class BalViewModel extends ChangeNotifier {
|
||||||
|
BalViewModel({required BalRepository balRepository, required this.id})
|
||||||
|
: _balRepository = balRepository {
|
||||||
|
load = Command0(_load)..execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
final BalRepository _balRepository;
|
||||||
|
|
||||||
|
Bal? _bal;
|
||||||
|
int? id;
|
||||||
|
Bal? get bal => _bal;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =================================
|
||||||
|
* =====[ COMMAND AND LOADING ]=====
|
||||||
|
* =================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
late final Command0 load;
|
||||||
|
bool isLoaded = false;
|
||||||
|
|
||||||
|
Future<Result<void>> _load() async {
|
||||||
|
final result1 = await _loadBal();
|
||||||
|
switch (result1) {
|
||||||
|
case Error():
|
||||||
|
return result1;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
isLoaded = true;
|
||||||
|
notifyListeners();
|
||||||
|
return result1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<void>> _loadBal() async {
|
||||||
|
if (id == null) {
|
||||||
|
return Result.error(Exception("No id given"));
|
||||||
|
}
|
||||||
|
final result = await _balRepository.balById(id!);
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
_bal = result.value;
|
||||||
|
break;
|
||||||
|
case Error():
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
40
lib/ui/bal_page/widget/bal_screen.dart
Normal file
40
lib/ui/bal_page/widget/bal_screen.dart
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:seshat/domain/models/bal.dart';
|
||||||
|
import 'package:seshat/ui/bal_page/view_model/bal_view_model.dart';
|
||||||
|
import 'package:seshat/ui/core/ui/navigation_bar.dart';
|
||||||
|
|
||||||
|
class BalScreen extends StatefulWidget {
|
||||||
|
const BalScreen({super.key, required this.viewModel});
|
||||||
|
|
||||||
|
final BalViewModel viewModel;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<BalScreen> createState() => _BalScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _BalScreenState extends State<BalScreen> {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||||
|
body: ListenableBuilder(
|
||||||
|
listenable: widget.viewModel,
|
||||||
|
builder: (context, child) {
|
||||||
|
return switch (widget.viewModel.isLoaded) {
|
||||||
|
false => Center(child: CircularProgressIndicator()),
|
||||||
|
true => switch (widget.viewModel.bal == null) {
|
||||||
|
true => Center(
|
||||||
|
child: Text("La BAL référencée n'est pas accessible"),
|
||||||
|
),
|
||||||
|
false => switch (widget.viewModel.bal!.state) {
|
||||||
|
BalState.pending => Center(child: Text("Pending")),
|
||||||
|
BalState.ongoing => Center(child: Text("Ongoing")),
|
||||||
|
BalState.ended => Center(child: Text("Ending")),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter/widgets.dart';
|
|
||||||
import 'package:seshat/ui/core/ui/navigation_bar.dart';
|
|
||||||
|
|
||||||
class HomePage extends StatelessWidget {
|
|
||||||
const HomePage({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Scaffold(
|
|
||||||
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
|
||||||
body: Center(child: Text("Home page.")),
|
|
||||||
);
|
|
||||||
// return Center(child: Text("Home page;"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
79
lib/ui/home_page/view_model/home_view_model.dart
Normal file
79
lib/ui/home_page/view_model/home_view_model.dart
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:seshat/data/repositories/bal_repository.dart';
|
||||||
|
import 'package:seshat/domain/models/bal.dart';
|
||||||
|
import 'package:seshat/utils/command.dart';
|
||||||
|
import 'package:seshat/utils/result.dart';
|
||||||
|
|
||||||
|
class HomeViewModel extends ChangeNotifier {
|
||||||
|
HomeViewModel({required BalRepository balRepository})
|
||||||
|
: _balRepository = balRepository {
|
||||||
|
load = Command0(_load)..execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
final BalRepository _balRepository;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =================
|
||||||
|
* =====[ BAL ]=====
|
||||||
|
* =================
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<Bal> _bals = [];
|
||||||
|
List<Bal> get bals => _bals;
|
||||||
|
|
||||||
|
Bal? _currentBal;
|
||||||
|
Bal? get currentBal => _currentBal;
|
||||||
|
|
||||||
|
Future<Result<void>> createBal(String name) async {
|
||||||
|
final result = await _balRepository.addBal(name);
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
final result2 = await _balRepository.getBals();
|
||||||
|
switch (result2) {
|
||||||
|
case Ok():
|
||||||
|
_bals = result2.value..sort((a, b) => a.compareTo(b));
|
||||||
|
break;
|
||||||
|
case Error():
|
||||||
|
debugPrint("\n\n\n\n${result2.error.toString()}\n\n\n\n");
|
||||||
|
return result2;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Error():
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
notifyListeners();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =================================
|
||||||
|
* =====[ COMMAND AND LOADING ]=====
|
||||||
|
* =================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
late final Command0 load;
|
||||||
|
bool isLoaded = false;
|
||||||
|
|
||||||
|
Future<Result<void>> _load() async {
|
||||||
|
final result2 = await _loadBal();
|
||||||
|
isLoaded = true;
|
||||||
|
notifyListeners();
|
||||||
|
return result2;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<void>> _loadBal() async {
|
||||||
|
final result = await _balRepository.getBals();
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
_bals = result.value..sort((a, b) => a.compareTo(b));
|
||||||
|
_currentBal = _bals
|
||||||
|
.where((bal) => bal.state == BalState.ongoing)
|
||||||
|
.firstOrNull;
|
||||||
|
break;
|
||||||
|
case Error():
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
71
lib/ui/home_page/widgets/create_confirmation_popup.dart
Normal file
71
lib/ui/home_page/widgets/create_confirmation_popup.dart
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:seshat/ui/home_page/view_model/home_view_model.dart';
|
||||||
|
|
||||||
|
class CreateConfirmationPopup extends StatefulWidget {
|
||||||
|
const CreateConfirmationPopup({super.key, required this.viewModel});
|
||||||
|
|
||||||
|
final HomeViewModel viewModel;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<CreateConfirmationPopup> createState() =>
|
||||||
|
_CreateConfirmationPopupState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CreateConfirmationPopupState extends State<CreateConfirmationPopup> {
|
||||||
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||||
|
String? name;
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text("Créer une BAL"),
|
||||||
|
content: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Form(
|
||||||
|
key: _formKey,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
TextFormField(
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: "Nom de la BAL",
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
validator: (value) {
|
||||||
|
if (value == null || value.isEmpty) {
|
||||||
|
return "Veuillez entrer un nom";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
onSaved: (newValue) {
|
||||||
|
name = newValue;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
child: Text("Annuler"),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
if (_formKey.currentState!.validate()) {
|
||||||
|
_formKey.currentState!.save();
|
||||||
|
await widget.viewModel.createBal(name!);
|
||||||
|
}
|
||||||
|
if (context.mounted) {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Text("Valider"),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
213
lib/ui/home_page/widgets/home_page.dart
Normal file
213
lib/ui/home_page/widgets/home_page.dart
Normal file
|
|
@ -0,0 +1,213 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:seshat/domain/models/bal.dart';
|
||||||
|
import 'package:seshat/ui/core/ui/navigation_bar.dart';
|
||||||
|
import 'package:seshat/ui/home_page/view_model/home_view_model.dart';
|
||||||
|
import 'package:seshat/ui/home_page/widgets/create_confirmation_popup.dart';
|
||||||
|
|
||||||
|
class HomePage extends StatefulWidget {
|
||||||
|
const HomePage({super.key, required this.viewModel});
|
||||||
|
|
||||||
|
final HomeViewModel viewModel;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<HomePage> createState() => _HomePageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _HomePageState extends State<HomePage> {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||||
|
body: ListenableBuilder(
|
||||||
|
listenable: widget.viewModel,
|
||||||
|
builder: (context, child) {
|
||||||
|
return switch (widget.viewModel.isLoaded) {
|
||||||
|
false => Center(child: CircularProgressIndicator()),
|
||||||
|
true => switch (widget.viewModel.currentBal == null) {
|
||||||
|
true => HomePageOnNoCurrent(widget: widget),
|
||||||
|
false => HomePageOnCurrent(widget: widget),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// return Center(child: Text("Home page;"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomePageOnNoCurrent extends StatelessWidget {
|
||||||
|
const HomePageOnNoCurrent({super.key, required this.widget});
|
||||||
|
|
||||||
|
final HomePage widget;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: (widget.viewModel.bals.isEmpty)
|
||||||
|
? Center(child: Text("Aucune BAL existante"))
|
||||||
|
: ListView(
|
||||||
|
children: [
|
||||||
|
for (Bal bal in widget.viewModel.bals.where(
|
||||||
|
(el) => el.id != widget.viewModel.currentBal?.id,
|
||||||
|
))
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 10.0),
|
||||||
|
child: Card(
|
||||||
|
child: ListTile(
|
||||||
|
leading: switch (bal.state) {
|
||||||
|
BalState.pending => Icon(Icons.event),
|
||||||
|
BalState.ongoing => Icon(Icons.event_available),
|
||||||
|
BalState.ended => Icon(Icons.lock),
|
||||||
|
},
|
||||||
|
title: Text(bal.name),
|
||||||
|
subtitle: switch (bal.state) {
|
||||||
|
BalState.pending => Text(
|
||||||
|
"À venir · Débute le ${bal.startTime.toString()}",
|
||||||
|
),
|
||||||
|
BalState.ongoing => Text("En cours"),
|
||||||
|
BalState.ended => Text("Terminée"),
|
||||||
|
},
|
||||||
|
trailing: switch (bal.state) {
|
||||||
|
BalState.pending => IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_moveToBal(context, bal.id);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.edit),
|
||||||
|
),
|
||||||
|
BalState.ongoing => IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_moveToBal(context, bal.id);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.arrow_forward),
|
||||||
|
),
|
||||||
|
BalState.ended => IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_moveToBal(context, bal.id);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.analytics),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 10),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return CreateConfirmationPopup(viewModel: widget.viewModel);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: Text("Débuter une BAL"),
|
||||||
|
),
|
||||||
|
SizedBox(height: 5),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomePageOnCurrent extends StatelessWidget {
|
||||||
|
const HomePageOnCurrent({super.key, required this.widget});
|
||||||
|
|
||||||
|
final HomePage widget;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: (widget.viewModel.bals.isEmpty)
|
||||||
|
? Center(child: Text("Aucune BAL existante"))
|
||||||
|
: ListView(
|
||||||
|
children: [
|
||||||
|
for (Bal bal in widget.viewModel.bals.where(
|
||||||
|
(el) => el.id != widget.viewModel.currentBal?.id,
|
||||||
|
))
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 10.0),
|
||||||
|
child: Card(
|
||||||
|
child: ListTile(
|
||||||
|
leading: switch (bal.state) {
|
||||||
|
BalState.pending => Icon(Icons.event),
|
||||||
|
BalState.ongoing => Icon(Icons.event_available),
|
||||||
|
BalState.ended => Icon(Icons.lock),
|
||||||
|
},
|
||||||
|
title: Text(bal.name),
|
||||||
|
subtitle: switch (bal.state) {
|
||||||
|
BalState.pending => Text(
|
||||||
|
"À venir · Débute le ${bal.startTime.toString()}",
|
||||||
|
),
|
||||||
|
BalState.ongoing => Text("En cours"),
|
||||||
|
BalState.ended => Text("Terminée"),
|
||||||
|
},
|
||||||
|
trailing: switch (bal.state) {
|
||||||
|
BalState.pending => IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_moveToBal(context, bal.id);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.edit),
|
||||||
|
),
|
||||||
|
BalState.ongoing => IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_moveToBal(context, bal.id);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.arrow_forward),
|
||||||
|
),
|
||||||
|
BalState.ended => IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_moveToBal(context, bal.id);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.analytics),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 10.0),
|
||||||
|
child: Card(
|
||||||
|
child: ListTile(
|
||||||
|
leading: Icon(Icons.event_available),
|
||||||
|
title: Text(widget.viewModel.currentBal!.name),
|
||||||
|
subtitle: Text("BAL en cours"),
|
||||||
|
trailing: IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_moveToBal(context, widget.viewModel.currentBal!.id);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.arrow_forward),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 10),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return CreateConfirmationPopup(viewModel: widget.viewModel);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: Text("Créer une BAL"),
|
||||||
|
),
|
||||||
|
SizedBox(height: 5),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _moveToBal(BuildContext context, int id) {
|
||||||
|
context.goNamed("bal", pathParameters: {"id": id.toString()});
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,18 @@
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||||
|
import 'package:seshat/data/repositories/bal_repository.dart';
|
||||||
|
import 'package:seshat/domain/models/bal.dart';
|
||||||
import 'package:seshat/domain/models/book_instance.dart';
|
import 'package:seshat/domain/models/book_instance.dart';
|
||||||
|
import 'package:seshat/utils/command.dart';
|
||||||
|
import 'package:seshat/utils/result.dart';
|
||||||
|
|
||||||
class SellViewModel extends ChangeNotifier {
|
class SellViewModel extends ChangeNotifier {
|
||||||
SellViewModel();
|
SellViewModel({required BalRepository balRepository})
|
||||||
|
: _balRepository = balRepository {
|
||||||
|
load = Command0(_load)..execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
final BalRepository _balRepository;
|
||||||
|
|
||||||
bool _showScan = false;
|
bool _showScan = false;
|
||||||
bool get showScan => _showScan;
|
bool get showScan => _showScan;
|
||||||
|
|
@ -12,6 +21,12 @@ class SellViewModel extends ChangeNotifier {
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ===============================
|
||||||
|
* =====[ BOOKS & INSTANCES ]=====
|
||||||
|
* ===============================
|
||||||
|
*/
|
||||||
|
|
||||||
final List<BookInstance> _scannedBooks = [];
|
final List<BookInstance> _scannedBooks = [];
|
||||||
get scannedBooks => _scannedBooks;
|
get scannedBooks => _scannedBooks;
|
||||||
void scanBook(BarcodeCapture barcode) {
|
void scanBook(BarcodeCapture barcode) {
|
||||||
|
|
@ -36,4 +51,44 @@ class SellViewModel extends ChangeNotifier {
|
||||||
_scannedBooks.removeWhere((book) => book.id == id);
|
_scannedBooks.removeWhere((book) => book.id == id);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =================
|
||||||
|
* =====[ BAL ]=====
|
||||||
|
* =================
|
||||||
|
*/
|
||||||
|
|
||||||
|
Bal? _currentBal;
|
||||||
|
get currentBal => _currentBal;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =================================
|
||||||
|
* =====[ COMMAND AND LOADING ]=====
|
||||||
|
* =================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
late final Command0 load;
|
||||||
|
bool isLoaded = false;
|
||||||
|
|
||||||
|
Future<Result<void>> _load() async {
|
||||||
|
final result2 = await _loadBal();
|
||||||
|
isLoaded = true;
|
||||||
|
notifyListeners();
|
||||||
|
return result2;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<void>> _loadBal() async {
|
||||||
|
final result = await _balRepository.getBals();
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
_currentBal = result.value
|
||||||
|
.where((bal) => bal.state == BalState.ongoing)
|
||||||
|
.firstOrNull;
|
||||||
|
break;
|
||||||
|
case Error():
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart';
|
||||||
import 'package:seshat/ui/sell_page/widgets/manual_scan_popup.dart';
|
import 'package:seshat/ui/sell_page/widgets/manual_scan_popup.dart';
|
||||||
|
|
||||||
class ScanScreen extends StatefulWidget {
|
class ScanScreen extends StatefulWidget {
|
||||||
ScanScreen({super.key, required this.viewModel});
|
const ScanScreen({super.key, required this.viewModel});
|
||||||
|
|
||||||
SellViewModel viewModel;
|
final SellViewModel viewModel;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<ScanScreen> createState() => _ScanScreenState();
|
State<ScanScreen> createState() => _ScanScreenState();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:seshat/domain/models/book_instance.dart';
|
import 'package:seshat/domain/models/book_instance.dart';
|
||||||
|
import 'package:seshat/routing/routes.dart';
|
||||||
import 'package:seshat/ui/core/ui/navigation_bar.dart';
|
import 'package:seshat/ui/core/ui/navigation_bar.dart';
|
||||||
import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart';
|
import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart';
|
||||||
import 'package:seshat/ui/sell_page/widgets/scan_screen.dart';
|
import 'package:seshat/ui/sell_page/widgets/scan_screen.dart';
|
||||||
|
|
@ -21,93 +23,133 @@ class _SellPageState extends State<SellPage> {
|
||||||
body: ListenableBuilder(
|
body: ListenableBuilder(
|
||||||
listenable: widget.viewModel,
|
listenable: widget.viewModel,
|
||||||
builder: (context, child) {
|
builder: (context, child) {
|
||||||
return Stack(
|
return switch (widget.viewModel.isLoaded) {
|
||||||
children: [
|
false => Center(child: CircularProgressIndicator()),
|
||||||
SafeArea(
|
true => switch (widget.viewModel.currentBal) {
|
||||||
child: Column(
|
null => Center(
|
||||||
mainAxisSize: MainAxisSize.min,
|
child: SizedBox(
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
width: 300,
|
||||||
children: [
|
child: Column(
|
||||||
SizedBox(height: 6),
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
Expanded(
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
child: ListView(
|
children: [
|
||||||
children: [
|
Text(
|
||||||
for (BookInstance bookInstance
|
"Aucune bal n'est active.",
|
||||||
in widget.viewModel.scannedBooks)
|
style: TextStyle(fontSize: 25),
|
||||||
Card(
|
textAlign: TextAlign.center,
|
||||||
child: ListTile(
|
|
||||||
leading: Text(
|
|
||||||
"${bookInstance.price.toString()}€",
|
|
||||||
style: TextStyle(fontSize: 30),
|
|
||||||
),
|
|
||||||
title: Text(
|
|
||||||
"Les chiens et la charrue · Patrick K. Dewdney ${bookInstance.id}",
|
|
||||||
),
|
|
||||||
subtitle: Text("Union Étudiante Auvergne"),
|
|
||||||
trailing: IconButton(
|
|
||||||
onPressed: () {
|
|
||||||
widget.viewModel.deleteBook(
|
|
||||||
bookInstance.id,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
icon: Icon(Icons.delete),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
SizedBox(height: 15),
|
||||||
SizedBox(height: 40),
|
Text(
|
||||||
Text("Somme minimum requise : 20€"),
|
"Vous devez créer puis activer une BAL pour pouvoir scanner des livres.",
|
||||||
SizedBox(
|
textAlign: TextAlign.center,
|
||||||
width: 400,
|
|
||||||
child: TextField(
|
|
||||||
decoration: InputDecoration(
|
|
||||||
labelText: "Argent reçu",
|
|
||||||
helperText:
|
|
||||||
"L'argent reçu sera réparti automatiquement",
|
|
||||||
suffixText: "€",
|
|
||||||
border: OutlineInputBorder(),
|
|
||||||
),
|
|
||||||
keyboardType: TextInputType.number,
|
|
||||||
),
|
),
|
||||||
),
|
SizedBox(height: 30),
|
||||||
SizedBox(height: 10),
|
ElevatedButton(
|
||||||
Row(
|
onPressed: () {
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
context.go(Routes.home);
|
||||||
children: [
|
},
|
||||||
IconButton(
|
child: Text("Gérer les BALs"),
|
||||||
onPressed: () {
|
),
|
||||||
widget.viewModel.sendSell();
|
],
|
||||||
},
|
),
|
||||||
icon: Icon(Icons.check),
|
|
||||||
style: ButtonStyle(
|
|
||||||
iconSize: WidgetStatePropertyAll(70),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
SizedBox(width: 70),
|
|
||||||
IconButton(
|
|
||||||
onPressed: () {
|
|
||||||
widget.viewModel.showScan = true;
|
|
||||||
},
|
|
||||||
icon: Icon(Icons.add),
|
|
||||||
style: ButtonStyle(
|
|
||||||
iconSize: WidgetStatePropertyAll(70),
|
|
||||||
elevation: WidgetStatePropertyAll(50),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
SizedBox(height: 5),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
_ => Stack(
|
||||||
|
children: [
|
||||||
|
SafeArea(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
SizedBox(height: 6),
|
||||||
|
Expanded(
|
||||||
|
child: ListView(
|
||||||
|
children: [
|
||||||
|
for (BookInstance bookInstance
|
||||||
|
in widget.viewModel.scannedBooks)
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(
|
||||||
|
horizontal: 15,
|
||||||
|
),
|
||||||
|
child: Card(
|
||||||
|
child: ListTile(
|
||||||
|
leading: Text(
|
||||||
|
"${bookInstance.price.toString()}€",
|
||||||
|
style: TextStyle(fontSize: 30),
|
||||||
|
),
|
||||||
|
title: Text(
|
||||||
|
"Les chiens et la charrue · Patrick K. Dewdney ${bookInstance.id}",
|
||||||
|
),
|
||||||
|
subtitle: Text(
|
||||||
|
"Union Étudiante Auvergne",
|
||||||
|
),
|
||||||
|
trailing: IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
widget.viewModel.deleteBook(
|
||||||
|
bookInstance.id,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.delete),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 40),
|
||||||
|
Text("Somme minimum requise : 20€"),
|
||||||
|
SizedBox(
|
||||||
|
width: 400,
|
||||||
|
child: TextField(
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: "Argent reçu",
|
||||||
|
helperText:
|
||||||
|
"L'argent reçu sera réparti automatiquement",
|
||||||
|
suffixText: "€",
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
keyboardType: TextInputType.number,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 10),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
widget.viewModel.sendSell();
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.check),
|
||||||
|
style: ButtonStyle(
|
||||||
|
iconSize: WidgetStatePropertyAll(70),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(width: 70),
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
widget.viewModel.showScan = true;
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.add),
|
||||||
|
style: ButtonStyle(
|
||||||
|
iconSize: WidgetStatePropertyAll(70),
|
||||||
|
elevation: WidgetStatePropertyAll(50),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
SizedBox(height: 5),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
(widget.viewModel.showScan)
|
(widget.viewModel.showScan)
|
||||||
? ScanScreen(viewModel: widget.viewModel)
|
? ScanScreen(viewModel: widget.viewModel)
|
||||||
: SizedBox(),
|
: SizedBox(),
|
||||||
],
|
],
|
||||||
);
|
),
|
||||||
|
},
|
||||||
|
};
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Reference in a new issue