Skip to content

Commit 2fde850

Browse files
committed
Documentation majorly
1 parent 4ee9ee4 commit 2fde850

File tree

13 files changed

+161
-30
lines changed

13 files changed

+161
-30
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ migrate_working_dir/
3030
.flutter-plugins-dependencies
3131
build/
3232

33-
fake_storage/
33+
**/fake_storage/

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
---
99

10+
## [0.1.1] - Documentation update
11+
12+
### Added
13+
- Documented methods
14+
- `.pubignore` to certain parts
15+
16+
---
17+
1018
## [0.1.0] - Initial Release
1119

1220
### Added

example/example.dart

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:mayr_storage/mayr_storage.dart';
3+
4+
/// [main.dart]
5+
void main() async {
6+
WidgetsFlutterBinding.ensureInitialized();
7+
8+
// Initialise MayrStorage
9+
await MayrStorage.init();
10+
11+
runApp(const MyApp());
12+
}
13+
14+
/// [storage.dart]
15+
class Storage {
16+
// Normal SharedPreferences storage
17+
static final normalToken = 'NORMAL_TOKEN'.storage<String>();
18+
19+
// Secure storage using encrypted shared preferences
20+
static final secureToken = 'SECURE_TOKEN'.secureStorage<String>();
21+
22+
// GetBox Storage
23+
static final boxToken = 'BOX_TOKEN'.boxStorage<String>();
24+
}
25+
26+
/// [my_app.dart]
27+
class MyApp extends StatelessWidget {
28+
const MyApp({super.key});
29+
30+
@override
31+
Widget build(BuildContext context) {
32+
return MaterialApp(
33+
home: Scaffold(
34+
appBar: AppBar(title: const Text('Mayr Storage Example')),
35+
body: const StorageDemo(),
36+
),
37+
);
38+
}
39+
}
40+
41+
/// [storage_demo.dart]
42+
class StorageDemo extends StatefulWidget {
43+
const StorageDemo({super.key});
44+
45+
@override
46+
State<StorageDemo> createState() => _StorageDemoState();
47+
}
48+
49+
class _StorageDemoState extends State<StorageDemo> {
50+
String? normalValue;
51+
String? secureValue;
52+
String? boxValue;
53+
54+
@override
55+
void initState() {
56+
super.initState();
57+
loadValues();
58+
}
59+
60+
Future<void> loadValues() async {
61+
normalValue = await Storage.normalToken.read();
62+
secureValue = await Storage.secureToken.read();
63+
boxValue = Storage.boxToken.read();
64+
65+
setState(() {});
66+
}
67+
68+
Future<void> writeValues() async {
69+
await Storage.normalToken.write('NormalTokenValue');
70+
await Storage.secureToken.write('SecureTokenValue');
71+
Storage.boxToken.write('BoxTokenValue');
72+
73+
await loadValues();
74+
}
75+
76+
Future<void> clearValues() async {
77+
await Storage.normalToken.delete();
78+
await Storage.secureToken.delete();
79+
Storage.boxToken.delete();
80+
81+
await loadValues();
82+
}
83+
84+
@override
85+
Widget build(BuildContext context) {
86+
return Padding(
87+
padding: const EdgeInsets.all(16.0),
88+
child: Column(
89+
mainAxisAlignment: MainAxisAlignment.center,
90+
children: [
91+
Text('Normal Token: $normalValue'),
92+
Text('Secure Token: $secureValue'),
93+
Text('Box Token: $boxValue'),
94+
const SizedBox(height: 20),
95+
ElevatedButton(
96+
onPressed: writeValues,
97+
child: const Text('Write Values'),
98+
),
99+
ElevatedButton(
100+
onPressed: clearValues,
101+
child: const Text('Clear Values'),
102+
),
103+
],
104+
),
105+
);
106+
}
107+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
part of '../../mayr_storage.dart';
22

33
abstract interface class PreferencesStorage<ValueT> {
4+
/// Delete stored record
45
Future<void> delete() async => write(null);
56

7+
/// Read stored record
68
Future<ValueT?> read();
79

10+
/// Write record to storage
811
Future<void> write(ValueT? value);
912
}

lib/src/drivers/encrypt_shared_preferences_storage.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ part of './../mayr_storage.dart';
22

33
class EncryptSharedPreferencesStorage<ValueT>
44
extends PreferencesStorage<ValueT> {
5-
final String preferenceKey;
5+
final String _preferenceKey;
66

7-
EncryptSharedPreferencesStorage(this.preferenceKey);
7+
EncryptSharedPreferencesStorage(this._preferenceKey);
88

99
@override
1010
Future<ValueT?> read() async {
1111
EncryptedSharedPreferences securePref =
1212
EncryptedSharedPreferences.getInstance();
1313

1414
if (ValueT == bool) {
15-
return securePref.getBool(preferenceKey) as ValueT?;
15+
return securePref.getBool(_preferenceKey) as ValueT?;
1616
} else if (ValueT == String) {
17-
return securePref.getString(preferenceKey) as ValueT?;
17+
return securePref.getString(_preferenceKey) as ValueT?;
1818
} else if (ValueT == int) {
19-
return securePref.getInt(preferenceKey) as ValueT?;
19+
return securePref.getInt(_preferenceKey) as ValueT?;
2020
} else if (ValueT == double) {
21-
return securePref.getDouble(preferenceKey) as ValueT?;
21+
return securePref.getDouble(_preferenceKey) as ValueT?;
2222
} else {
2323
throw Exception("Unsupported type");
2424
}
@@ -32,13 +32,13 @@ class EncryptSharedPreferencesStorage<ValueT>
3232
if (value == null) {
3333
securePref.clear();
3434
} else if (ValueT == bool) {
35-
securePref.setBool(preferenceKey, value as bool);
35+
securePref.setBool(_preferenceKey, value as bool);
3636
} else if (ValueT == String) {
37-
securePref.setString(preferenceKey, value as String);
37+
securePref.setString(_preferenceKey, value as String);
3838
} else if (ValueT == int) {
39-
securePref.setInt(preferenceKey, value as int);
39+
securePref.setInt(_preferenceKey, value as int);
4040
} else if (ValueT == double) {
41-
securePref.setDouble(preferenceKey, value as double);
41+
securePref.setDouble(_preferenceKey, value as double);
4242
} else {
4343
throw Exception("Unsupported type");
4444
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
part of '../mayr_storage.dart';
22

33
class GetBoxStorage<ValueT> {
4-
final String boxName;
5-
6-
GetBoxStorage(this.boxName);
4+
final String _boxName;
75

86
late final GetStorage _box = GetStorage();
97

10-
void write(ValueT? value) => _box.write(boxName, value);
11-
12-
ValueT? read() => _box.read(boxName);
8+
GetBoxStorage(this._boxName);
139

14-
void delete() => _box.remove(boxName);
10+
/// Delete stored record
11+
void delete() => _box.remove(_boxName);
1512

13+
/// Listen for updates
1614
void listen(void Function(ValueT newValue) handler) =>
17-
_box.listenKey(boxName, (newValue) => handler(newValue));
15+
_box.listenKey(_boxName, (newValue) => handler(newValue));
16+
17+
/// Read value from storage
18+
ValueT? read() => _box.read(_boxName);
19+
20+
/// Write value to storage
21+
void write(ValueT? value) => _box.write(_boxName, value);
1822
}

lib/src/drivers/shared_preferences_storage.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
part of '../mayr_storage.dart';
22

33
class SharedPreferencesStorage<ValueT> extends PreferencesStorage<ValueT> {
4-
SharedPreferencesStorage(this.preferenceKey);
4+
final String _preferenceKey;
55

6-
final String preferenceKey;
6+
SharedPreferencesStorage(this._preferenceKey);
77

88
@override
99
Future<ValueT?> read() async {
1010
SharedPreferences sharedPref = await SharedPreferences.getInstance();
1111

1212
if (ValueT == bool) {
13-
return sharedPref.getBool(preferenceKey) as ValueT?;
13+
return sharedPref.getBool(_preferenceKey) as ValueT?;
1414
} else if (ValueT == String) {
15-
return sharedPref.getString(preferenceKey) as ValueT?;
15+
return sharedPref.getString(_preferenceKey) as ValueT?;
1616
} else if (ValueT == int) {
17-
return sharedPref.getInt(preferenceKey) as ValueT?;
17+
return sharedPref.getInt(_preferenceKey) as ValueT?;
1818
} else if (ValueT == double) {
19-
return sharedPref.getDouble(preferenceKey) as ValueT?;
19+
return sharedPref.getDouble(_preferenceKey) as ValueT?;
2020
} else {
2121
throw Exception("Unsupported type");
2222
}
@@ -29,13 +29,13 @@ class SharedPreferencesStorage<ValueT> extends PreferencesStorage<ValueT> {
2929
if (value == null) {
3030
sharedPref.clear();
3131
} else if (ValueT == bool) {
32-
sharedPref.setBool(preferenceKey, value as bool);
32+
sharedPref.setBool(_preferenceKey, value as bool);
3333
} else if (ValueT == String) {
34-
sharedPref.setString(preferenceKey, value as String);
34+
sharedPref.setString(_preferenceKey, value as String);
3535
} else if (ValueT == int) {
36-
sharedPref.setInt(preferenceKey, value as int);
36+
sharedPref.setInt(_preferenceKey, value as int);
3737
} else if (ValueT == double) {
38-
sharedPref.setDouble(preferenceKey, value as double);
38+
sharedPref.setDouble(_preferenceKey, value as double);
3939
} else {
4040
throw Exception("Unsupported type");
4141
}

lib/src/extension.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
part of './mayr_storage.dart';
22

33
extension KeyStorageExtensions on String {
4+
/// Get Box Storage using GetStorage internally
45
GetBoxStorage<ValueT> boxStorage<ValueT>() => GetBoxStorage<ValueT>(this);
56

7+
/// Get Secure Storage using Encrypted Shared Preferences internally
68
EncryptSharedPreferencesStorage<ValueT> secureStorage<ValueT>() =>
79
EncryptSharedPreferencesStorage<ValueT>(this);
810

11+
/// Get Shared Preferences Storage
912
SharedPreferencesStorage<ValueT> storage<ValueT>() =>
1013
SharedPreferencesStorage<ValueT>(this);
1114
}

lib/src/mayr_storage.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ part './drivers/shared_preferences_storage.dart';
99
part './extension.dart';
1010

1111
class MayrStorage {
12+
/// Init MayrStorage
1213
static Future init() async {
1314
await GetStorage.init();
1415

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: mayr_storage
22
description: >-
33
Simple and unified storage solution for Flutter apps.
44
Easily manage SharedPreferences, EncryptedSharedPreferences, and GetStorage with a clean, consistent API.
5-
version: 0.1.0
5+
version: 0.1.1
66
homepage: https://github.com/YoungMayor/mayr_flutter_storage
77
repository: https://github.com/YoungMayor/mayr_flutter_storage
88
issue_tracker: https://github.com/YoungMayor/mayr_flutter_storage/issues

0 commit comments

Comments
 (0)