Skip to content

Commit aea26db

Browse files
committed
✨ update documentation and examples to reflect changes in item holder and container usage
1 parent 57d454b commit aea26db

File tree

15 files changed

+271
-249
lines changed

15 files changed

+271
-249
lines changed

packages/hyper_storage/README.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ Then, run `flutter pub get` or `dart pub get`.
6969

7070
## Usage
7171

72-
Initialize the storage with a backend of your choice.
72+
Initialize the storage with the backend you want to use.
7373

7474
```dart
7575
import 'package:hyper_storage/hyper_storage.dart';
76+
import 'package:hyper_storage_shared_preferences/shared_preferences_backend.dart';
7677
7778
void main() async {
78-
// Initialize the storage with InMemoryBackend
79+
// Initialize the storage with SharedPreferences
7980
final storage = await HyperStorage.init(backend: SharedPreferencesBackend());
8081
8182
// Set a value
@@ -116,26 +117,24 @@ More backends are available in separate packages:
116117

117118
## Initialization
118119

119-
You can initialize `hyper_storage` with a specific backend. If no backend is provided, it defaults to `InMemoryBackend`.
120+
You can initialize `hyper_storage` with any storage backend that implements the `StorageBackend` interface. Always pass
121+
the backend you want to use to `HyperStorage.init`.
120122

121-
### Default (In-Memory)
123+
### In-memory (for tests or ephemeral data)
122124

123125
```dart
124126
import 'package:hyper_storage/hyper_storage.dart';
125127
126128
final storage = await HyperStorage.init(backend: InMemoryBackend());
127129
```
128130

129-
### Using Other Backends
131+
### Using other backends
130132

131-
To use other backends, you need to add the respective package to your `pubspec.yaml` and then provide the backend instance to the `init` method.
132-
133-
For example, to use the Hive backend:
133+
Add the desired backend package to your dependencies and provide the backend instance to `init`:
134134

135135
```dart
136-
// Make sure to add hyper_storage_hive to your dependencies
137-
import 'package:hyper_storage_hive/hyper_storage_hive.dart';
138136
import 'package:hyper_storage/hyper_storage.dart';
137+
import 'package:hyper_storage_hive/hyper_storage_hive.dart';
139138
140139
final storage = await HyperStorage.init(backend: HiveBackend());
141140
```
@@ -213,7 +212,7 @@ final List<Map<String, dynamic>>? items = await storage.getJsonList('items');
213212
You can use named containers to organize your data.
214213

215214
```dart
216-
final container = await HyperStorage.container('account');
215+
final container = await storage.container('account');
217216
await container.setString('username', 'john_doe');
218217
final String? username = await container.getString('username');
219218
```
@@ -224,30 +223,29 @@ You can store and retrieve custom objects by providing `toJson` and `fromJson` f
224223

225224
```dart
226225
class User {
226+
final String id;
227227
final String name;
228-
final int age;
229228
230-
User({required this.name, required this.age});
229+
User({required this.id, required this.name});
231230
232-
factory User.fromJson(Map<String, dynamic> json) {
233-
return User(name: json['name'], age: json['age']);
234-
}
231+
factory User.fromJson(Map<String, dynamic> json) => User(
232+
id: json['id'] as String,
233+
name: json['name'] as String,
234+
);
235235
236-
Map<String, dynamic> toJson() {
237-
return {'name': name, 'age': age};
238-
}
236+
Map<String, dynamic> toJson() => {'id': id, 'name': name};
239237
}
240238
241-
final storage = await HyperStorage.objectContainer<User>(
239+
final users = await storage.jsonSerializableContainer<User>(
242240
'users',
243-
toJson: (user) => user.toJson(),
244241
fromJson: User.fromJson,
242+
toJson: (user) => user.toJson(),
243+
idGetter: (user) => user.id,
245244
);
246245
247-
await storage.set('user1', User(name: 'John', age: 23));
248-
final User? user = await storage.get('user1');
249-
final List<User> users = await storage.getValues();
250-
final Map<String, User> allUsers = await storage.getAll();
246+
await users.add(User(id: 'user1', name: 'John'));
247+
final User? user = await users.get('user1');
248+
final List<User> allUsers = await users.getValues();
251249
```
252250

253251
## Checking for Keys
@@ -285,4 +283,4 @@ final storage = await HyperStorage.initMocked(initialData: {
285283

286284
## Contributing
287285

288-
Contributions are welcome! Please feel free to open an issue or submit a pull request.
286+
Contributions are welcome! Please feel free to open an issue or submit a pull request.

packages/hyper_storage/docs/backends.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ live activities, native runners, etc.
3636

3737
```dart
3838
import 'package:hyper_storage/hyper_storage.dart';
39-
import 'package:hyper_storage_shared_preferences/hyper_storage_shared_preferences.dart';
39+
import 'package:hyper_storage_shared_preferences/shared_preferences_backend.dart';
4040
4141
void main() async {
4242
final storage = await HyperStorage.init(backend: SharedPreferencesBackend());
@@ -108,7 +108,7 @@ void main() async {
108108
);
109109
110110
final storage = await HyperStorage.init(
111-
backend: SecureStorageBackend(secureStorage: secureStorage),
111+
backend: SecureStorageBackend(storage: secureStorage),
112112
);
113113
}
114114
```
@@ -143,7 +143,7 @@ You can also use multiple backends in your application by creating separate `Hyp
143143

144144
```dart
145145
import 'package:hyper_storage/hyper_storage.dart';
146-
import 'package:hyper_storage_shared_preferences/hyper_storage_shared_preferences.dart';
146+
import 'package:hyper_storage_shared_preferences/shared_preferences_backend.dart';
147147
import 'package:hyper_storage_hive/hyper_storage_hive.dart';
148148
149149
void main() async {
@@ -156,4 +156,4 @@ void main() async {
156156
// Use hiveStorage for app data
157157
await hiveStorage.setString('user_data', 'some complex data');
158158
}
159-
```
159+
```

packages/hyper_storage/docs/containers.md

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ returned.
2828

2929
```dart
3030
import 'package:hyper_storage/hyper_storage.dart';
31+
import 'package:hyper_storage_shared_preferences/shared_preferences_backend.dart';
3132
3233
void main() async {
3334
final storage = await HyperStorage.init(backend: SharedPreferencesBackend());
@@ -61,11 +62,15 @@ Containers also support storing and retrieving JSON serializable objects. You ca
6162
store complex data structures in a structured way. For example, storing a list of Todo items with `Todo` class.
6263

6364
```dart
64-
final todoContainer = await storage.jsonContainer<Todo>('todos');
65+
final todos = await storage.jsonSerializableContainer<Todo>(
66+
'todos',
67+
fromJson: Todo.fromJson,
68+
toJson: (todo) => todo.toJson(),
69+
);
6570
66-
await todoContainer.set('task1', Todo(id: 'task1', title: 'Buy groceries', isCompleted: false));
67-
final todo = await todoContainer.get('task1');
68-
await todoContainer.remove('task1');
71+
await todos.set('task1', Todo(id: 'task1', title: 'Buy groceries', isCompleted: false));
72+
final todo = await todos.get('task1');
73+
await todos.remove('task1');
6974
```
7075

7176
### Providing a custom ID
@@ -74,14 +79,16 @@ By default, serializable containers would generate a unique String ID for each o
7479
also provide a custom ID by providing a `idGetter` function when creating the container.
7580

7681
```dart
77-
final todoContainer = await storage.jsonContainer<Todo>(
82+
final todos = await storage.jsonSerializableContainer<Todo>(
7883
'todos',
84+
fromJson: Todo.fromJson,
85+
toJson: (todo) => todo.toJson(),
7986
idGetter: (todo) => todo.id,
8087
);
8188
82-
await todoContainer.add(Todo(id: 't1', title: 'Buy groceries', isCompleted: false));
83-
await todoContainer.get('t1');
84-
await todoContainer.remove('t1');
89+
await todos.add(Todo(id: 't1', title: 'Buy groceries', isCompleted: false));
90+
await todos.get('t1');
91+
await todos.remove('t1');
8592
```
8693

8794
## Custom Serializable Containers
@@ -92,34 +99,55 @@ and deserialization logic.
9299

93100
```dart
94101
class XmlSerializableContainer<E extends Object> extends SerializableStorageContainer<E> {
95-
XmlSerializableContainer({super.backend, super.name});
102+
XmlSerializableContainer({
103+
required super.backend,
104+
required super.name,
105+
super.delimiter,
106+
});
96107
97108
@override
98109
E deserialize(String value) {
99-
// Implement your XML deserialization logic here
100-
// For example, using an XML parsing library to convert XML string to object
101-
throw UnimplementedError();
110+
// Convert the stored XML string back into your object type
111+
return parseXml<E>(value);
102112
}
103113
104114
@override
105115
String serialize(E value) {
106-
// Implement your XML serialization logic here
107-
// For example, using an XML building library to convert object to XML string
108-
throw UnimplementedError();
116+
// Convert your object into an XML string for storage
117+
return toXml(value);
109118
}
110119
}
120+
121+
// Helper functions that convert between your XML representation and the Dart object.
122+
E parseXml<E>(String xml) => throw UnimplementedError();
123+
String toXml(Object value) => throw UnimplementedError();
124+
125+
// Register the custom container with Hyper Storage
126+
final posts = await storage.objectContainer<Post, XmlSerializableContainer<Post>>(
127+
'posts',
128+
factory: () => XmlSerializableContainer<Post>(
129+
backend: storage.backend,
130+
name: 'posts',
131+
),
132+
);
133+
134+
// storage.backend exposes the underlying backend instance that Hyper Storage uses internally.
111135
```
112136

113137
## Container Delimiter
114138

115139
The way containers are implemented is by prefixing the keys with the container name followed by a delimiter.
116-
By default, the delimiter is a dot (`___`). For example, if you have a container named `settings` and you store a key
140+
By default, the delimiter is a triple underscore (`___`). For example, if you have a container named `settings` and you store a key
117141
`theme` with value `dark`, the actual key stored in the backend would be `settings___theme`.
118142

119143
This is done to isolate the keys into a namespace, preventing key collisions between different containers. You can
120-
change the delimiter by providing a custom delimiter when initializing Hyper Storage.
144+
change the delimiter for serializable containers by providing the `delimiter` argument when creating them.
121145

122146
```dart
123-
// Create a storage instance with a custom delimiter.
124-
final storage = await storage.container('todos', delimiter: '--');
125-
```
147+
final todos = await storage.jsonSerializableContainer<Todo>(
148+
'todos',
149+
fromJson: Todo.fromJson,
150+
toJson: (todo) => todo.toJson(),
151+
delimiter: '--',
152+
);
153+
```

packages/hyper_storage/docs/enums.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,18 @@ final brightness = await container.getEnum<Brightness>('brightness', Brightness.
5959
You can also use the generic `get` method to retrieve enums:
6060

6161
```dart
62-
final role = await storage.get<Role>('role', Role.values);
62+
final role = await storage.get<Role>('role', enumValues: Role.values);
6363
```
6464

6565
## Using ItemHolder with Enums
6666

6767
You can also use `ItemHolder` to store and retrieve enums. Here's an example:
6868

6969
```dart
70-
final roleHolder = storage.itemHolder<Role>('role', Role.values);
70+
final roleHolder = storage.itemHolder<Role>(
71+
'role',
72+
enumValues: Role.values,
73+
);
7174
7275
// Set the enum value
7376
await roleHolder.set(Role.user);
@@ -81,7 +84,7 @@ final role = await roleHolder.get();
8184
You can stream changes to enum values using the `stream` method. Here's an example:
8285

8386
```dart
84-
final roleStream = storage.stream<Role>('role', Role.values);
87+
final roleStream = storage.stream<Role>('role', enumValues: Role.values);
8588
8689
roleStream.listen((role) {
8790
print('Role changed to: $role');
@@ -93,10 +96,12 @@ With containers:
9396
```dart
9497
final container = await storage.container('settings');
9598
96-
final brightnessStream = container.stream<Brightness>('brightness', Brightness.values);
99+
final brightnessStream = container.stream<Brightness>(
100+
'brightness',
101+
enumValues: Brightness.values,
102+
);
97103
98104
brightnessStream.listen((brightness) {
99105
print('Brightness changed to: $brightness');
100106
});
101107
```
102-

0 commit comments

Comments
 (0)