Skip to content

Commit

Permalink
Add local storage for snippets and fix serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Nov 30, 2022
1 parent a1fc29c commit a0754f8
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 45 deletions.
30 changes: 3 additions & 27 deletions lib/src/home/models/char_model.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dart:convert';

import 'package:freezed_annotation/freezed_annotation.dart';

part 'char_model.freezed.dart';
part 'char_model.g.dart';

@freezed
class CharModel with _$CharModel {
Expand All @@ -11,30 +10,7 @@ class CharModel with _$CharModel {
required String value,
required int length,
}) = _CharModel;
}

extension CharModelSerialisable on CharModel {
static List<CharModel> fromJsonString(String json) {
final List list = const JsonDecoder().convert(json);
return list
.map(
(e) => CharModel(
name: e['name'],
value: e['value'],
length: e['length'],
),
)
.toList();
}

static String toJsonString(List<CharModel> list) {
final json = list.map(
(e) => {
'name': e.name,
'value': e.value,
'length': e.length,
},
);
return const JsonEncoder().convert(json);
}
factory CharModel.fromJson(Map<String, dynamic> json) =>
_$CharModelFromJson(json);
}
21 changes: 20 additions & 1 deletion lib/src/home/models/char_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');

CharModel _$CharModelFromJson(Map<String, dynamic> json) {
return _CharModel.fromJson(json);
}

/// @nodoc
mixin _$CharModel {
String get name => throw _privateConstructorUsedError;
String get value => throw _privateConstructorUsedError;
int get length => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$CharModelCopyWith<CharModel> get copyWith =>
throw _privateConstructorUsedError;
Expand Down Expand Up @@ -106,11 +111,14 @@ class __$$_CharModelCopyWithImpl<$Res> extends _$CharModelCopyWithImpl<$Res>
}

/// @nodoc
@JsonSerializable()
class _$_CharModel implements _CharModel {
const _$_CharModel(
{required this.name, required this.value, required this.length});

factory _$_CharModel.fromJson(Map<String, dynamic> json) =>
_$$_CharModelFromJson(json);

@override
final String name;
@override
Expand All @@ -133,6 +141,7 @@ class _$_CharModel implements _CharModel {
const DeepCollectionEquality().equals(other.length, length));
}

@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(
runtimeType,
Expand All @@ -144,6 +153,13 @@ class _$_CharModel implements _CharModel {
@override
_$$_CharModelCopyWith<_$_CharModel> get copyWith =>
__$$_CharModelCopyWithImpl<_$_CharModel>(this, _$identity);

@override
Map<String, dynamic> toJson() {
return _$$_CharModelToJson(
this,
);
}
}

abstract class _CharModel implements CharModel {
Expand All @@ -152,6 +168,9 @@ abstract class _CharModel implements CharModel {
required final String value,
required final int length}) = _$_CharModel;

factory _CharModel.fromJson(Map<String, dynamic> json) =
_$_CharModel.fromJson;

@override
String get name;
@override
Expand Down
20 changes: 20 additions & 0 deletions lib/src/home/models/char_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/src/home/providers/editor_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ class _EditorContentModel extends StateNotifier<String> {
}

void addContent(String content, TextPosition pos) {
state = state.substring(0, pos.offset) +
final offset = pos.offset == -1 ? 0 : pos.offset;
state = state.substring(0, offset) +
content +
state.substring(pos.offset, state.length);
state.substring(offset, state.length);
}

void replaceAll(String old, String new_) {
Expand Down
30 changes: 17 additions & 13 deletions lib/src/home/services/snippet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import 'package:excode/src/home/models/char_model.dart';

class SnippetService {
static Future<void> store({required List<CharModel> data}) async {
await box.put('snippetList', CharModelSerialisable.toJsonString(data));
await box.put('snippetList', data.map((e) => e.toJson()).toList());
}

static List<CharModel> fetch() {
return CharModelSerialisable.fromJsonString(
box.get('snippetList') ??
'''[
{"name": "( )", "value": "()", "length": 1},
{"name": "{ }", "value": "{}", "length": 1},
{"name": "[ ]", "value": "[]", "length": 1},
{"name": "\\"", "value": "\\"\\"", "length": 1},
{"name": "'", "value": "''", "length": 1},
{"name": "`", "value": "``", "length": 1},
{"name": "< >", "value": "<>", "length": 1}
]''',
);
final List<dynamic>? dynamicList = box.get('snippetList');

final storedCharList = dynamicList
?.map((e) => CharModel.fromJson(Map<String, dynamic>.from(e)))
.toList();

return storedCharList ??
[
{'name': '( )', 'value': '()', 'length': 1},
{'name': '{ }', 'value': '{}', 'length': 1},
{'name': '[ ]', 'value': '[]', 'length': 1},
{'name': '\\', 'value': '\\', 'length': 1},
{'name': "'", 'value': "''", 'length': 1},
{'name': '`', 'value': '``', 'length': 1},
{'name': '< >', 'value': '<>', 'length': 1},
].map((e) => CharModel.fromJson(e)).toList();
}
}
3 changes: 2 additions & 1 deletion lib/src/home/widgets/code_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ class CodeFieldWidgetState extends State<CodeFieldWidget> {
}

void _updateLineNumber() {
final offset = widget.controller.selection.base.offset;
setState(() {
_selectedLine = '\n'
.allMatches(widget.controller.text.substring(
0,
widget.controller.selection.base.offset,
offset,
))
.length +
1;
Expand Down
16 changes: 15 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,14 @@ packages:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.6.0"
version: "4.7.0"
json_serializable:
dependency: "direct dev"
description:
name: json_serializable
url: "https://pub.dartlang.org"
source: hosted
version: "6.5.4"
jwt_decode:
dependency: transitive
description:
Expand Down Expand Up @@ -880,6 +887,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.2"
source_helper:
dependency: transitive
description:
name: source_helper
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
source_span:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dev_dependencies:

flutter_lints: ^1.0.0
freezed: ^2.0.3+1
json_serializable: ^6.5.4

flutter:
uses-material-design: true
Expand Down

0 comments on commit a0754f8

Please sign in to comment.