added cache for categories

This commit is contained in:
Patrick Alvin Alcala 2025-03-20 13:10:39 +08:00
parent 753c730588
commit 1aa7410e2e
14 changed files with 195 additions and 63 deletions

View file

@ -0,0 +1,17 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_event.dart';
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_state.dart';
class CategoryListBloc extends Bloc<CategoryListCacheEvent, CategoryListCacheState> {
CategoryListBloc() : super(CategoryListCacheState([])) {
on<CategoryListCacheSet>((event, emit) {
emit(CategoryListCacheState(event.value));
});
on<CategoryListCacheGet>((event, emit) {
emit(state);
});
on<CategoryListCacheCheck>((event, emit) {
emit(state);
});
}
}

View file

@ -0,0 +1,10 @@
abstract class CategoryListCacheEvent {}
class CategoryListCacheSet extends CategoryListCacheEvent {
final List value;
CategoryListCacheSet(this.value);
}
class CategoryListCacheGet extends CategoryListCacheEvent {}
class CategoryListCacheCheck extends CategoryListCacheEvent {}

View file

@ -0,0 +1,5 @@
class CategoryListCacheState {
final List value;
CategoryListCacheState(this.value);
}

View file

@ -0,0 +1,14 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_event.dart';
Future<List> cacheGetCategoryList(BuildContext context) async {
try {
final categoryListCache = context.read<CategoryListBloc>();
categoryListCache.add(CategoryListCacheGet());
return categoryListCache.state.value;
} catch (e) {
return [];
}
}

View file

@ -0,0 +1,14 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_event.dart';
Future<bool> cacheSetCategoryList(BuildContext context, List value) async {
try {
final categoryListCache = context.read<CategoryListBloc>();
categoryListCache.add(CategoryListCacheSet(value));
return true;
} catch (e) {
return false;
}
}