Skip to content

Commit

Permalink
fixed web hive
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovNick committed Nov 12, 2024
1 parent 7ae9a97 commit 4c5d82a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,45 @@ abstract class UsersLocalDataSource {
}

class UsersLocalDataSourceImpl implements UsersLocalDataSource {
late Box box;
Box? _box;

Future<Box> get box async {
if (_box == null || !_box!.isOpen) {
_box = await Hive.openBox('users');
}
return _box!;
}

@override
Future<void> initializeBox() async {
box = await Hive.openBox('users');
if (_box != null && _box!.isOpen) {
await _box!.close();
}
_box = await Hive.openBox('users');
}

@override
Future<void> addUser(String username) async {
final openedBox = await box;
List<String> users = await getUsers();
if (!users.contains(username)) {
users.add(username);
await box.put('users', users);
await openedBox.put('users', users);
}
}

@override
Future<void> removeUser(String username) async {
final openedBox = await box;
List<String> users = await getUsers();
users.remove(username);
await box.put('users', users);
await openedBox.put('users', users);
}

@override
Future<List<String>> getUsers() async {
final u = box.get('users', defaultValue: <String>[]) as List<String>;
final openedBox = await box;
final u = openedBox.get('users', defaultValue: <String>[]) as List<String>;
return u;
}
}
102 changes: 97 additions & 5 deletions flutter_front/lib/shared/data/datasources/local/hive_datasource.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:clean_architecture/core/error/exception.dart';
import 'package:clean_architecture/features/home/domain/entities/fetch_topics.dart';
import 'package:clean_architecture/shared/data/models/user_model.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

import '../../../../features/charts/data/models/chart_config_model.dart';
import '../../../../features/charts/domain/entities/chart_config.dart';
import '../../../../features/charts/domain/usecases/save_charts_layout_use_case.dart';

abstract class HiveLocalDataSource {
Future<void> initializeUserBox(String username);
Future<void> saveUsername(String username);
Expand Down Expand Up @@ -36,51 +37,95 @@ abstract class HiveLocalDataSource {

Future<List<String>?> getMultipleChosenEquipment();
Future<void> saveMultipleChosenEquipment(List<String>? equipment);

Future<ChartConfiguration?> getChartsConfiguration();
Future<void> saveChartsConfiguration(Map<String, dynamic> config);

Future<ChartsLayoutData?> getChartsLayout();
Future<void> saveChartsLayout(Map<String, dynamic> data);
}

class HiveLocalDataSourceImpl implements HiveLocalDataSource {
late Box userBox;
static const String SESSION_BOX_NAME = 'session';
static const String CURRENT_USER_KEY = 'current_username';

Box? _userBox;
Box? _sessionBox;

Future<Box> get sessionBox async {
if (_sessionBox == null || !_sessionBox!.isOpen) {
_sessionBox = await Hive.openBox(SESSION_BOX_NAME);
}
return _sessionBox!;
}

Future<Box> get getUserBox async {
if (_userBox == null || !_userBox!.isOpen) {
final session = await sessionBox;
final username = session.get(CURRENT_USER_KEY) as String?;
if (username == null) {
throw Exception('Username not set. Call initializeUserBox first');
}
_userBox = await Hive.openBox('user_$username');
}
return _userBox!;
}

@override
Future<void> initializeUserBox(String username) async {
userBox = await Hive.openBox('user_$username');
if (_userBox != null && _userBox!.isOpen) {
await _userBox!.close();
}

final session = await sessionBox;
await session.put(CURRENT_USER_KEY, username);

_userBox = await Hive.openBox('user_$username');
}


@override
Future<void> saveUsername(String username) async {
final userBox = await getUserBox;
await userBox.put("username", {"value": username});
}

@override
Future<void> saveToken(String token) async {
final userBox = await getUserBox;
await userBox.put("token", {"value": token});
}

@override
Future<void> saveRefreshToken(String token) async {
final userBox = await getUserBox;
await userBox.put("refreshToken", {"value": token});
}

@override
Future<void> saveLogo(String logo) async {
final userBox = await getUserBox;
await userBox.put("logo", <String, dynamic>{"value": logo});
}

@override
Future<String?> getLogo() async {
final userBox = await getUserBox;
final logo = userBox.get("logo") as Map?;
return logo?["value"] as String?;
}

@override
Future<String?> getUsername() async {
final userBox = await getUserBox;
final username = userBox.get("username");
return username?["value"] as String?;
}

@override
Future<String?> getToken() async {
try {
final userBox = await getUserBox;
final token = userBox.get("token") as Map?;
return token?["value"] as String?;
} catch (e){
Expand All @@ -90,18 +135,21 @@ class HiveLocalDataSourceImpl implements HiveLocalDataSource {

@override
Future<String?> getRefreshToken() async {
final userBox = await getUserBox;
final token = userBox.get("refreshToken") as Map?;
return token?["value"] as String?;
}


@override
Future<void> saveMiniChartsTopics(FetchMiniChartsTopics topics) async {
final userBox = await getUserBox;
await userBox.put("miniChartsTopics", topics.toMap());
}

@override
Future<FetchMiniChartsTopics?> getMiniChartsTopics() async {
final userBox = await getUserBox;
final topics = userBox.get("miniChartsTopics") as Map?;
if(topics == null) return null;
return FetchMiniChartsTopics.fromMap(topics);
Expand All @@ -111,11 +159,13 @@ class HiveLocalDataSourceImpl implements HiveLocalDataSource {

@override
Future<void> saveTheme(String theme) async {
final userBox = await getUserBox;
await userBox.put("theme", {"value": theme});
}

@override
Future<String?> getTheme() async {
final userBox = await getUserBox;
final theme = userBox.get("theme") as Map?;
return theme?["value"] as String?;
}
Expand All @@ -124,6 +174,7 @@ class HiveLocalDataSourceImpl implements HiveLocalDataSource {

@override
Future<void> savePeriod(DateTimeRange period) async {
final userBox = await getUserBox;
await userBox.put("period", {
"start": period.start.toIso8601String(),
"end": period.end.toIso8601String(),
Expand All @@ -132,6 +183,7 @@ class HiveLocalDataSourceImpl implements HiveLocalDataSource {

@override
Future<DateTimeRange> getPeriod() async {
final userBox = await getUserBox;
final periodMap = userBox.get("period") as Map?;
if (periodMap != null) {
final start = DateTime.parse(periodMap["start"] as String);
Expand All @@ -150,32 +202,37 @@ class HiveLocalDataSourceImpl implements HiveLocalDataSource {

@override
Future<List<String>> getCollapsedEquipment() async {
final userBox = await getUserBox;
final collapsed = userBox.get("collapsedEquipment") as List?;
return collapsed?.cast<String>() ?? [];
}

@override
Future<void> saveCollapsedEquipment(List<String> collapsedKeys) async {
final userBox = await getUserBox;
await userBox.put("collapsedEquipment", collapsedKeys);
}

// Chosen Equipment

@override
Future<String?> getChosenEquipment() async {
final userBox = await getUserBox;
final equipment = userBox.get("chosenEquipment") as Map?;
return equipment?["value"] as String?;
}

@override
Future<void> saveChosenEquipment(String? equipment) async {
final userBox = await getUserBox;
await userBox.put("chosenEquipment", {"value": equipment});
}

// Multiple Chosen Equipment

@override
Future<List<String>?> getMultipleChosenEquipment() async {
final userBox = await getUserBox;
final equipment = userBox.get("multipleChosenEquipment") as Map?;
if (equipment == null) return null;

Expand All @@ -185,20 +242,55 @@ class HiveLocalDataSourceImpl implements HiveLocalDataSource {

@override
Future<void> saveMultipleChosenEquipment(List<String>? equipment) async {
final userBox = await getUserBox;
await userBox.put("multipleChosenEquipment", {"value": equipment});
}

// Chosen Equipment

@override
Future<double?> getExcessPercent() async {
final userBox = await getUserBox;
final percent = userBox.get("excessPercent") as Map?;
return percent?["value"] as double?;
}

@override
Future<void> saveExcessPercent(double percent) async {
final userBox = await getUserBox;
await userBox.put("excessPercent", {"value": percent});
}

// Equipment config

@override
Future<ChartConfiguration?> getChartsConfiguration() async {
final userBox = await getUserBox;
final config = userBox.get("chartsConfiguration") as Map?;
if(config == null) return null;
return ChartConfigurationModel.fromMap(config).toEntity();
}

@override
Future<void> saveChartsConfiguration(Map<String, dynamic> config) async {
final userBox = await getUserBox;
await userBox.put("chartsConfiguration", config);
}

// Charts Layout

@override
Future<ChartsLayoutData?> getChartsLayout() async {
final userBox = await getUserBox;
final layout = userBox.get("chartsLayout") as Map?;
if(layout == null) return null;
return ChartsLayoutData.fromMap(layout);
}

@override
Future<void> saveChartsLayout(Map<String, dynamic> data) async {
final userBox = await getUserBox;
await userBox.put("chartsLayout", data);
}

}

0 comments on commit 4c5d82a

Please sign in to comment.