wip: cache license in build context.read

This commit is contained in:
J. A. Messias 2024-12-04 10:50:55 -03:00
parent 88f9b1f814
commit 243c89b636
10 changed files with 60 additions and 30 deletions

View File

@ -26,16 +26,24 @@ class _HomePageWidgetState extends State<HomePageWidget> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MultiBlocProvider( return MultiBlocProvider(
providers: [ providers: [
BlocProvider(
create: (context) => MenuBloc(
style: MenuView.list_grid,
item: EnumMenuItem.button,
menuOptions: MenuEntry.getEntriesByType(MenuEntryType.Home),
)..add(MenuEvent()),
),
BlocProvider<HomeBloc>( BlocProvider<HomeBloc>(
create: (context) => HomeBloc()..add(HomeEvent()), create: (context) => HomeBloc()..add(HomeEvent()),
), ),
BlocProvider( BlocProvider(
create: (context) => LocalProfileBloc()..add(LocalProfileEvent()), create: (context) => LocalProfileBloc()..add(LocalProfileEvent()),
), ),
], ],
child: Builder( child: Builder(
builder: (context) { builder: (context) {
// context.read<LocalProfileBloc>().updateProfile(context); context.read<LocalProfileBloc>().updateProfile(context);
return Scaffold( return Scaffold(
key: scaffoldKey, key: scaffoldKey,
backgroundColor: FlutterFlowTheme.of(context).primaryBackground, backgroundColor: FlutterFlowTheme.of(context).primaryBackground,

View File

@ -18,11 +18,11 @@ import 'package:hub/shared/utils/dialog_util.dart';
import 'package:hub/shared/utils/snackbar_util.dart'; import 'package:hub/shared/utils/snackbar_util.dart';
abstract class LocalsRemoteDataSource { abstract class LocalsRemoteDataSource {
Future<void> checkLocals(BuildContext context) async {} Future<void> checkLocals(BuildContext context);
Future<bool> processLocals(BuildContext context) async => false; Future<bool> processLocals(BuildContext context) ;
Future<bool> processData(BuildContext context) async => false; Future<bool> processData(BuildContext context);
Future<bool> selectLocal(BuildContext context, ApiCallResponse? response) async => false; Future<bool> selectLocal(BuildContext context, ApiCallResponse? response) ;
Future<void> detachLocal(BuildContext context) async {} Future<void> detachLocal(BuildContext context) ;
} }
class LocalsRemoteDataSourceImpl implements LocalsRemoteDataSource { class LocalsRemoteDataSourceImpl implements LocalsRemoteDataSource {
@ -165,7 +165,7 @@ class LocalsRemoteDataSourceImpl implements LocalsRemoteDataSource {
await DialogUtil.error(context, errorMsg).whenComplete(() => selectLocal(context, response)); await DialogUtil.error(context, errorMsg).whenComplete(() => selectLocal(context, response));
return false; return false;
} else { } else {
final bool isNewVersion = await LocalUtil.updateStorageUtil(response.jsonBody); await LocalUtil.updateStorageUtil(response.jsonBody);
return await LicenseRepositoryImpl().updateLicense(); return await LicenseRepositoryImpl().updateLicense();
} }
} catch (e, s) { } catch (e, s) {

View File

@ -2,10 +2,14 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hub/shared/components/molecules/locals/index.dart'; import 'package:hub/shared/components/molecules/locals/index.dart';
import 'package:hub/shared/components/molecules/modules/index.dart'; import 'package:hub/shared/components/molecules/modules/index.dart';
import 'package:rxdart/rxdart.dart';
class LocalsRepositoryImpl implements LocalsRepository { class LocalsRepositoryImpl implements LocalsRepository {
final LocalsRemoteDataSource remoteDataSource = LocalsRemoteDataSourceImpl(); final LocalsRemoteDataSource remoteDataSource = LocalsRemoteDataSourceImpl();
static final license = BehaviorSubject<LicenseStatus>();
static get stream => license.stream;
@override @override
Future<void> validateLocal(BuildContext context) async { Future<void> validateLocal(BuildContext context) async {
return await remoteDataSource.checkLocals(context); return await remoteDataSource.checkLocals(context);
@ -13,8 +17,10 @@ class LocalsRepositoryImpl implements LocalsRepository {
@override @override
Future<bool> update(BuildContext context) async { Future<bool> update(BuildContext context) async {
final bool response = await LicenseRepositoryImpl().updateLicense(); LocalsRepositoryImpl.license.add(false);
final bool response = await remoteDataSource.processLocals(context);
context.read<LocalProfileBloc>().add(LocalProfileEvent()); context.read<LocalProfileBloc>().add(LocalProfileEvent());
LocalsRepositoryImpl.license.add(response);
return response; return response;
} }

View File

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:hub/flutter_flow/flutter_flow_model.dart'; import 'package:hub/flutter_flow/flutter_flow_model.dart';
import 'package:hub/flutter_flow/nav/nav.dart'; import 'package:hub/flutter_flow/nav/nav.dart';
import 'package:hub/shared/components/molecules/locals/index.dart'; import 'package:hub/shared/components/molecules/locals/index.dart';
import 'package:hub/shared/components/molecules/menu/index.dart';
import 'package:hub/shared/helpers/storage/base_storage.dart'; import 'package:hub/shared/helpers/storage/base_storage.dart';
import 'package:hub/shared/helpers/storage/storage_helper.dart'; import 'package:hub/shared/helpers/storage/storage_helper.dart';
@ -36,10 +37,10 @@ class LocalProfileBloc extends Bloc<LocalProfileEvent, LocalProfileState> {
emit(state.copyWith(cliName: cliName, cliUUID: cliUUID)); emit(state.copyWith(cliName: cliName, cliUUID: cliUUID));
} }
// void updateProfile(BuildContext context) { void updateProfile(BuildContext context) {
// add(LocalProfileEvent()); context.read<MenuBloc>().add(MenuEvent());
// LocalsRepositoryImpl().update(context); add(LocalProfileEvent());
// } }
} }
class LocalProfileComponentModel extends FlutterFlowModel<LocalProfileComponentWidget> { class LocalProfileComponentModel extends FlutterFlowModel<LocalProfileComponentWidget> {

View File

@ -31,7 +31,7 @@ class _LocalProfileComponentWidgetState extends State<LocalProfileComponentWidge
return StreamBuilder<LicenseStatus>( return StreamBuilder<LicenseStatus>(
stream: LicenseRepositoryImpl.stream, stream: LocalsRepositoryImpl.stream,
builder: (context, snapshot) { builder: (context, snapshot) {
final textScaler = MediaQuery.textScalerOf(context); final textScaler = MediaQuery.textScalerOf(context);
final double baseFontSize = 14.0; final double baseFontSize = 14.0;

View File

@ -34,16 +34,18 @@ class MenuBloc extends Bloc<MenuEvent, MenuState> {
MenuBloc({required this.style, required this.item, required this.menuOptions}) : super(MenuState()) { MenuBloc({required this.style, required this.item, required this.menuOptions}) : super(MenuState()) {
on<MenuEvent>((event, emit) async { on<MenuEvent>(_onMenuEvent);
await LicenseRemoteDataSourceImpl().waitForSaveCompletion();
final entries = await MenuRepositoryImpl().generateMenuEntries(menuOptions, item);
emit(state.copyWith(menuEntries: entries));
});
_completer = LicenseRemoteDataSourceImpl().streamCompleterController.listen((_) { _completer = LicenseRemoteDataSourceImpl().streamCompleterController.listen((_) {
add(MenuEvent()); add(MenuEvent());
}); });
} }
Future<void> _onMenuEvent(MenuEvent event, Emitter<MenuState> emit) async {
await LicenseRemoteDataSourceImpl().waitForSaveCompletion();
final entries = await MenuRepositoryImpl().generateMenuEntries(menuOptions, item);
emit(state.copyWith(menuEntries: entries));
}
@override @override
Future<void> close() { Future<void> close() {

View File

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart'; import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/shared/components/molecules/locals/index.dart';
import 'package:hub/shared/components/molecules/modules/index.dart'; import 'package:hub/shared/components/molecules/modules/index.dart';
import 'package:hub/shared/components/molecules/menu/index.dart'; import 'package:hub/shared/components/molecules/menu/index.dart';
import 'package:hub/shared/utils/loading_util.dart'; import 'package:hub/shared/utils/loading_util.dart';
@ -22,7 +23,7 @@ class Menufactory extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return StreamBuilder<LicenseStatus>( return StreamBuilder<LicenseStatus>(
stream: LicenseRepositoryImpl.stream, stream: LocalsRepositoryImpl.stream,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());

View File

@ -4,6 +4,7 @@ import 'dart:async';
import 'dart:developer'; import 'dart:developer';
import 'package:hub/backend/api_requests/api_calls.dart'; import 'package:hub/backend/api_requests/api_calls.dart';
import 'package:hub/shared/components/molecules/locals/index.dart';
import 'package:hub/shared/components/molecules/modules/index.dart'; import 'package:hub/shared/components/molecules/modules/index.dart';
import 'package:hub/shared/helpers/database/database_helper.dart'; import 'package:hub/shared/helpers/database/database_helper.dart';
@ -31,13 +32,14 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
@override @override
Future<void> cleanLicense() async { Future<void> cleanLicense() async {
LicenseRepositoryImpl.license.add(false); LocalsRepositoryImpl.license.add(false);
} }
@override @override
Future<bool> setupLicense(Database database, bool isNewVersion) async { Future<bool> setupLicense(Database database, bool isNewVersion) async {
log('Setting up license...'); log('Setting up license...');
try {
final License license = await License.getLicense(isNewVersion); final License license = await License.getLicense(isNewVersion);
final List<String> inactiveModuleKey = license.modules final List<String> inactiveModuleKey = license.modules
@ -65,6 +67,10 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
} }
return true; return true;
} catch (e, s) {
log('Erro ao configurar licenças: $e', stackTrace: s);
return false;
}
} }
@override @override
@ -85,7 +91,8 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
} }
if (responseBody == []) { if (responseBody == []) {
return await setupLicense(DatabaseStorage.database, isNewVersion); // return await setupLicense(DatabaseStorage.database, isNewVersion);
return false;
} }
List<Future<void>> saveOperations = []; List<Future<void>> saveOperations = [];
@ -98,12 +105,13 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
return true; return true;
} catch (e, s) { } catch (e, s) {
log('Erro ao obter licenças: $e', stackTrace: s); log('Erro ao obter licenças: $e', stackTrace: s);
return await setupLicense(DatabaseStorage.database, isNewVersion); // return await setupLicense(DatabaseStorage.database, isNewVersion);
return false;
} }
} }
Future<void> waitForSaveCompletion() async { Future<void> waitForSaveCompletion() async {
await LicenseRepositoryImpl.stream; await LocalsRepositoryImpl.stream;
} }
static bool licenseContainsKey(final String key) { static bool licenseContainsKey(final String key) {

View File

@ -1,8 +1,9 @@
import 'dart:developer';
import 'package:hub/shared/components/molecules/modules/data/data_sources/license_local_data_source.dart'; import 'package:hub/shared/components/molecules/modules/data/data_sources/license_local_data_source.dart';
import 'package:hub/shared/components/molecules/modules/data/data_sources/license_remote_data_source.dart'; import 'package:hub/shared/components/molecules/modules/data/data_sources/license_remote_data_source.dart';
import 'package:hub/shared/components/molecules/modules/domain/respositories/license_repository.dart'; import 'package:hub/shared/components/molecules/modules/domain/respositories/license_repository.dart';
import 'package:hub/shared/helpers/database/database_helper.dart'; import 'package:hub/shared/helpers/database/database_helper.dart';
import 'package:rxdart/rxdart.dart';
import 'package:sqflite/sqflite.dart'; import 'package:sqflite/sqflite.dart';
typedef LicenseStatus = bool; typedef LicenseStatus = bool;
@ -14,31 +15,34 @@ class LicenseRepositoryImpl implements LicenseRepository {
LicenseRepositoryImpl(); LicenseRepositoryImpl();
static final license = BehaviorSubject<LicenseStatus>();
static get stream => license.stream;
@override
Future<bool> updateLicense() async { Future<bool> updateLicense() async {
LicenseRepositoryImpl.license.add(false);
bool result = false; bool result = false;
final bool isNewVersion = await localDataSource.isNewVersion(); final bool isNewVersion = await localDataSource.isNewVersion();
log('isNewVersion: $isNewVersion');
result = await remoteDataSource.setupLicense(database, isNewVersion); result = await remoteDataSource.setupLicense(database, isNewVersion);
if (isNewVersion) { if (isNewVersion) {
result = await remoteDataSource.fetchLicenses(isNewVersion); result = await remoteDataSource.fetchLicenses(isNewVersion);
} }
LicenseRepositoryImpl.license.add(result);
return result; return result;
} }
@override
Future<void> cleanLicense() async { Future<void> cleanLicense() async {
return await remoteDataSource.cleanLicense(); return await remoteDataSource.cleanLicense();
} }
@override
Future<String?> getLicense(String key) async { Future<String?> getLicense(String key) async {
return await localDataSource.get(key); return await localDataSource.get(key);
} }
@override
Future<void> setLicense<T>(String key, T value) async { Future<void> setLicense<T>(String key, T value) async {
return await localDataSource.set(key, value); return await localDataSource.set(key, value);
} }

View File

@ -32,7 +32,7 @@ class DatabaseStorage {
onUpgrade: _onUpgrade, onUpgrade: _onUpgrade,
onDowngrade: _onDowngrade, onDowngrade: _onDowngrade,
); );
await LicenseRepositoryImpl().updateLicense(); // await LicenseRepositoryImpl().updateLicense();
isInitialized = true; isInitialized = true;
} }