wip: cache license in build context.read
This commit is contained in:
parent
88f9b1f814
commit
243c89b636
|
@ -26,16 +26,24 @@ class _HomePageWidgetState extends State<HomePageWidget> {
|
|||
Widget build(BuildContext context) {
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(
|
||||
create: (context) => MenuBloc(
|
||||
style: MenuView.list_grid,
|
||||
item: EnumMenuItem.button,
|
||||
menuOptions: MenuEntry.getEntriesByType(MenuEntryType.Home),
|
||||
)..add(MenuEvent()),
|
||||
),
|
||||
BlocProvider<HomeBloc>(
|
||||
create: (context) => HomeBloc()..add(HomeEvent()),
|
||||
),
|
||||
BlocProvider(
|
||||
create: (context) => LocalProfileBloc()..add(LocalProfileEvent()),
|
||||
),
|
||||
|
||||
],
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
// context.read<LocalProfileBloc>().updateProfile(context);
|
||||
context.read<LocalProfileBloc>().updateProfile(context);
|
||||
return Scaffold(
|
||||
key: scaffoldKey,
|
||||
backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
|
||||
|
|
|
@ -18,11 +18,11 @@ import 'package:hub/shared/utils/dialog_util.dart';
|
|||
import 'package:hub/shared/utils/snackbar_util.dart';
|
||||
|
||||
abstract class LocalsRemoteDataSource {
|
||||
Future<void> checkLocals(BuildContext context) async {}
|
||||
Future<bool> processLocals(BuildContext context) async => false;
|
||||
Future<bool> processData(BuildContext context) async => false;
|
||||
Future<bool> selectLocal(BuildContext context, ApiCallResponse? response) async => false;
|
||||
Future<void> detachLocal(BuildContext context) async {}
|
||||
Future<void> checkLocals(BuildContext context);
|
||||
Future<bool> processLocals(BuildContext context) ;
|
||||
Future<bool> processData(BuildContext context);
|
||||
Future<bool> selectLocal(BuildContext context, ApiCallResponse? response) ;
|
||||
Future<void> detachLocal(BuildContext context) ;
|
||||
}
|
||||
|
||||
class LocalsRemoteDataSourceImpl implements LocalsRemoteDataSource {
|
||||
|
@ -165,7 +165,7 @@ class LocalsRemoteDataSourceImpl implements LocalsRemoteDataSource {
|
|||
await DialogUtil.error(context, errorMsg).whenComplete(() => selectLocal(context, response));
|
||||
return false;
|
||||
} else {
|
||||
final bool isNewVersion = await LocalUtil.updateStorageUtil(response.jsonBody);
|
||||
await LocalUtil.updateStorageUtil(response.jsonBody);
|
||||
return await LicenseRepositoryImpl().updateLicense();
|
||||
}
|
||||
} catch (e, s) {
|
||||
|
|
|
@ -2,10 +2,14 @@ import 'package:flutter/material.dart';
|
|||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hub/shared/components/molecules/locals/index.dart';
|
||||
import 'package:hub/shared/components/molecules/modules/index.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
class LocalsRepositoryImpl implements LocalsRepository {
|
||||
final LocalsRemoteDataSource remoteDataSource = LocalsRemoteDataSourceImpl();
|
||||
|
||||
static final license = BehaviorSubject<LicenseStatus>();
|
||||
static get stream => license.stream;
|
||||
|
||||
@override
|
||||
Future<void> validateLocal(BuildContext context) async {
|
||||
return await remoteDataSource.checkLocals(context);
|
||||
|
@ -13,8 +17,10 @@ class LocalsRepositoryImpl implements LocalsRepository {
|
|||
|
||||
@override
|
||||
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());
|
||||
LocalsRepositoryImpl.license.add(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
|
|||
import 'package:hub/flutter_flow/flutter_flow_model.dart';
|
||||
import 'package:hub/flutter_flow/nav/nav.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/storage_helper.dart';
|
||||
|
||||
|
@ -36,10 +37,10 @@ class LocalProfileBloc extends Bloc<LocalProfileEvent, LocalProfileState> {
|
|||
emit(state.copyWith(cliName: cliName, cliUUID: cliUUID));
|
||||
}
|
||||
|
||||
// void updateProfile(BuildContext context) {
|
||||
// add(LocalProfileEvent());
|
||||
// LocalsRepositoryImpl().update(context);
|
||||
// }
|
||||
void updateProfile(BuildContext context) {
|
||||
context.read<MenuBloc>().add(MenuEvent());
|
||||
add(LocalProfileEvent());
|
||||
}
|
||||
}
|
||||
|
||||
class LocalProfileComponentModel extends FlutterFlowModel<LocalProfileComponentWidget> {
|
||||
|
|
|
@ -31,7 +31,7 @@ class _LocalProfileComponentWidgetState extends State<LocalProfileComponentWidge
|
|||
|
||||
|
||||
return StreamBuilder<LicenseStatus>(
|
||||
stream: LicenseRepositoryImpl.stream,
|
||||
stream: LocalsRepositoryImpl.stream,
|
||||
builder: (context, snapshot) {
|
||||
final textScaler = MediaQuery.textScalerOf(context);
|
||||
final double baseFontSize = 14.0;
|
||||
|
|
|
@ -34,17 +34,19 @@ class MenuBloc extends Bloc<MenuEvent, MenuState> {
|
|||
|
||||
|
||||
MenuBloc({required this.style, required this.item, required this.menuOptions}) : super(MenuState()) {
|
||||
on<MenuEvent>((event, emit) async {
|
||||
await LicenseRemoteDataSourceImpl().waitForSaveCompletion();
|
||||
final entries = await MenuRepositoryImpl().generateMenuEntries(menuOptions, item);
|
||||
emit(state.copyWith(menuEntries: entries));
|
||||
});
|
||||
on<MenuEvent>(_onMenuEvent);
|
||||
|
||||
_completer = LicenseRemoteDataSourceImpl().streamCompleterController.listen((_) {
|
||||
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
|
||||
Future<void> close() {
|
||||
_completer.cancel();
|
||||
|
|
|
@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
|
|||
import 'package:flutter_bloc/flutter_bloc.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/menu/index.dart';
|
||||
import 'package:hub/shared/utils/loading_util.dart';
|
||||
|
@ -22,7 +23,7 @@ class Menufactory extends StatelessWidget {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return StreamBuilder<LicenseStatus>(
|
||||
stream: LicenseRepositoryImpl.stream,
|
||||
stream: LocalsRepositoryImpl.stream,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
|
|
|
@ -4,6 +4,7 @@ import 'dart:async';
|
|||
import 'dart:developer';
|
||||
|
||||
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/helpers/database/database_helper.dart';
|
||||
|
||||
|
@ -31,13 +32,14 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
|
|||
|
||||
@override
|
||||
Future<void> cleanLicense() async {
|
||||
LicenseRepositoryImpl.license.add(false);
|
||||
LocalsRepositoryImpl.license.add(false);
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> setupLicense(Database database, bool isNewVersion) async {
|
||||
log('Setting up license...');
|
||||
try {
|
||||
final License license = await License.getLicense(isNewVersion);
|
||||
|
||||
final List<String> inactiveModuleKey = license.modules
|
||||
|
@ -65,6 +67,10 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
|
|||
}
|
||||
|
||||
return true;
|
||||
} catch (e, s) {
|
||||
log('Erro ao configurar licenças: $e', stackTrace: s);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -85,7 +91,8 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
|
|||
}
|
||||
|
||||
if (responseBody == []) {
|
||||
return await setupLicense(DatabaseStorage.database, isNewVersion);
|
||||
// return await setupLicense(DatabaseStorage.database, isNewVersion);
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Future<void>> saveOperations = [];
|
||||
|
@ -98,12 +105,13 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
|
|||
return true;
|
||||
} catch (e, 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 {
|
||||
await LicenseRepositoryImpl.stream;
|
||||
await LocalsRepositoryImpl.stream;
|
||||
}
|
||||
|
||||
static bool licenseContainsKey(final String key) {
|
||||
|
|
|
@ -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_remote_data_source.dart';
|
||||
import 'package:hub/shared/components/molecules/modules/domain/respositories/license_repository.dart';
|
||||
import 'package:hub/shared/helpers/database/database_helper.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
typedef LicenseStatus = bool;
|
||||
|
@ -14,31 +15,34 @@ class LicenseRepositoryImpl implements LicenseRepository {
|
|||
|
||||
LicenseRepositoryImpl();
|
||||
|
||||
static final license = BehaviorSubject<LicenseStatus>();
|
||||
static get stream => license.stream;
|
||||
|
||||
|
||||
@override
|
||||
Future<bool> updateLicense() async {
|
||||
LicenseRepositoryImpl.license.add(false);
|
||||
bool result = false;
|
||||
final bool isNewVersion = await localDataSource.isNewVersion();
|
||||
log('isNewVersion: $isNewVersion');
|
||||
result = await remoteDataSource.setupLicense(database, isNewVersion);
|
||||
if (isNewVersion) {
|
||||
result = await remoteDataSource.fetchLicenses(isNewVersion);
|
||||
}
|
||||
|
||||
LicenseRepositoryImpl.license.add(result);
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> cleanLicense() async {
|
||||
return await remoteDataSource.cleanLicense();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getLicense(String key) async {
|
||||
return await localDataSource.get(key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setLicense<T>(String key, T value) async {
|
||||
return await localDataSource.set(key, value);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ class DatabaseStorage {
|
|||
onUpgrade: _onUpgrade,
|
||||
onDowngrade: _onDowngrade,
|
||||
);
|
||||
await LicenseRepositoryImpl().updateLicense();
|
||||
// await LicenseRepositoryImpl().updateLicense();
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue