feat: save scheme name logic.

pull/5/head
DebuggerX 3 years ago
parent f024c39e1f
commit aa367cf3ba

@ -2,7 +2,7 @@ import 'package:dde_gesture_manager/models/scheme.dart';
export 'local_schemes_web.dart' if (dart.library.io) 'local_schemes_linux.dart'; export 'local_schemes_web.dart' if (dart.library.io) 'local_schemes_linux.dart';
abstract class LocalSchemeEntry { abstract class LocalSchemeEntry implements Comparable {
Scheme scheme; Scheme scheme;
DateTime lastModifyTime; DateTime lastModifyTime;
String path; String path;

@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:dde_gesture_manager/builder/provider_annotation.dart'; import 'package:dde_gesture_manager/builder/provider_annotation.dart';
@ -13,7 +14,7 @@ export 'local_schemes.dart';
@ProviderModel() @ProviderModel()
class LocalSchemes implements LocalSchemesInterface<LocalSchemeEntryLinux> { class LocalSchemes implements LocalSchemesInterface<LocalSchemeEntryLinux> {
LocalSchemes() { LocalSchemes() {
schemeEntries.then((value) => schemes = [LocalSchemeEntryLinux.systemDefault(), ...value]); schemeEntries.then((value) => schemes = [LocalSchemeEntryLinux.systemDefault(), ...value..sort()]);
} }
@override @override
@ -68,7 +69,13 @@ class LocalSchemeEntryLinux implements LocalSchemeEntry {
@override @override
save() { save() {
// TODO: implement save var file = File(path);
throw UnimplementedError(); file.writeAsStringSync(json.encode(scheme));
}
@override
int compareTo(other) {
assert(other is LocalSchemeEntry);
return lastModifyTime.isAfter(other.lastModifyTime) ? -1 : 1;
} }
} }

@ -12,7 +12,7 @@ export 'local_schemes.dart';
@ProviderModel() @ProviderModel()
class LocalSchemes implements LocalSchemesInterface<LocalSchemeEntryWeb> { class LocalSchemes implements LocalSchemesInterface<LocalSchemeEntryWeb> {
LocalSchemes() { LocalSchemes() {
schemeEntries.then((value) => schemes = [LocalSchemeEntryWeb.systemDefault(), ...value]); schemeEntries.then((value) => schemes = [LocalSchemeEntryWeb.systemDefault(), ...value..sort()]);
} }
@override @override
@ -72,4 +72,10 @@ class LocalSchemeEntryWeb implements LocalSchemeEntry {
// TODO: implement save // TODO: implement save
throw UnimplementedError(); throw UnimplementedError();
} }
@override
int compareTo(other) {
assert(other is LocalSchemeEntry);
return lastModifyTime.isAfter(other.lastModifyTime) ? -1 : 1;
}
} }

@ -128,7 +128,7 @@ class Scheme {
Scheme.parse(scheme) { Scheme.parse(scheme) {
if (scheme is String) scheme = json.decode(scheme); if (scheme is String) scheme = json.decode(scheme);
assert(scheme is Map); assert(scheme is Map);
id = scheme['id']; id = scheme['id'] ?? Uuid().v1();
name = scheme['name']; name = scheme['name'];
description = scheme['desc']; description = scheme['desc'];
gestures = (scheme['gestures'] as List? ?? []).map<GestureProp>((ele) => GestureProp.parse(ele)).toList()..sort(); gestures = (scheme['gestures'] as List? ?? []).map<GestureProp>((ele) => GestureProp.parse(ele)).toList()..sort();
@ -154,6 +154,13 @@ class Scheme {
}); });
return schemeTree; return schemeTree;
} }
Map toJson() => {
'id': id,
'name': name,
'desc': description,
'gestures': gestures,
};
} }
enum Gesture { enum Gesture {
@ -221,6 +228,16 @@ class GestureProp implements Comparable {
return 'GestureProp{gesture: $gesture, direction: $direction, fingers: $fingers, type: $type, command: $command, remark: $remark}'; return 'GestureProp{gesture: $gesture, direction: $direction, fingers: $fingers, type: $type, command: $command, remark: $remark}';
} }
Map toJson() => {
'id': id,
'gesture': H.getGestureName(gesture),
'direction': H.getGestureDirectionName(direction),
'fingers': fingers,
'type': H.getGestureTypeName(type),
'command': command,
'remark': remark,
};
GestureProp.empty() : this.id = Uuid.NAMESPACE_NIL; GestureProp.empty() : this.id = Uuid.NAMESPACE_NIL;
GestureProp.parse(props) { GestureProp.parse(props) {

@ -2,6 +2,7 @@ import 'package:adaptive_scrollbar/adaptive_scrollbar.dart';
import 'package:dde_gesture_manager/constants/constants.dart'; import 'package:dde_gesture_manager/constants/constants.dart';
import 'package:dde_gesture_manager/extensions.dart'; import 'package:dde_gesture_manager/extensions.dart';
import 'package:dde_gesture_manager/models/content_layout.provider.dart'; import 'package:dde_gesture_manager/models/content_layout.provider.dart';
import 'package:dde_gesture_manager/models/local_schemes_provider.dart';
import 'package:dde_gesture_manager/models/scheme.dart'; import 'package:dde_gesture_manager/models/scheme.dart';
import 'package:dde_gesture_manager/models/scheme.provider.dart'; import 'package:dde_gesture_manager/models/scheme.provider.dart';
import 'package:dde_gesture_manager/models/settings.provider.dart'; import 'package:dde_gesture_manager/models/settings.provider.dart';
@ -256,9 +257,25 @@ class GestureEditor extends StatelessWidget {
), ),
Expanded( Expanded(
child: DTextField( child: DTextField(
initText: schemeProvider.name,
onComplete: (val) { onComplete: (val) {
val = val.trim();
schemeProvider.setProps(name: val); schemeProvider.setProps(name: val);
/// todo: change name to local list. var localSchemesProvider = context.read<LocalSchemesProvider>();
if (!localSchemesProvider.schemes!.every((element) => element.scheme.name != val)) {
/// show error info;
'duplicate name'.sout();
return;
}
;
var localSchemeEntry = localSchemesProvider.schemes!
.firstWhere((ele) => ele.scheme.id == schemeProvider.id);
localSchemeEntry.scheme.name = val;
localSchemeEntry.save();
localSchemesProvider.schemeEntries.then((value) {
localSchemesProvider
.setProps(schemes: [localSchemesProvider.schemes!.first, ...value..sort()]);
});
}, },
), ),
), ),

@ -10,6 +10,7 @@ import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/painting.dart'; import 'package:flutter/painting.dart';
import 'package:uuid/uuid.dart';
class LocalManager extends StatefulWidget { class LocalManager extends StatefulWidget {
const LocalManager({ const LocalManager({
@ -22,22 +23,22 @@ class LocalManager extends StatefulWidget {
class _LocalManagerState extends State<LocalManager> { class _LocalManagerState extends State<LocalManager> {
late ScrollController _scrollController; late ScrollController _scrollController;
int? _hoveringIndex; String? _hoveringItem;
late int _selectedIndex; late String _selectedItem;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
/// todo: load from sp /// todo: load from sp
_selectedIndex = 0; _selectedItem = Uuid.NAMESPACE_NIL;
_scrollController = ScrollController(); _scrollController = ScrollController();
} }
Color _getItemBackgroundColor(int index) { Color _getItemBackgroundColor(int index, String itemId) {
Color _color = index % 2 == 0 ? context.t.scaffoldBackgroundColor : context.t.backgroundColor; Color _color = index % 2 == 0 ? context.t.scaffoldBackgroundColor : context.t.backgroundColor;
if (index == _hoveringIndex) _color = context.t.scaffoldBackgroundColor; if (itemId == _hoveringItem) _color = context.t.scaffoldBackgroundColor;
if (index == _selectedIndex) _color = context.read<SettingsProvider>().currentActiveColor; if (itemId == _selectedItem) _color = context.read<SettingsProvider>().currentActiveColor;
return _color; return _color;
} }
@ -110,7 +111,7 @@ class _LocalManagerState extends State<LocalManager> {
onTap: () { onTap: () {
context.read<SchemeProvider>().copyFrom(localSchemes[index].scheme); context.read<SchemeProvider>().copyFrom(localSchemes[index].scheme);
setState(() { setState(() {
_selectedIndex = index; _selectedItem = localSchemes[index].scheme.id!;
}); });
context.read<GesturePropProvider>().copyFrom(GestureProp.empty()); context.read<GesturePropProvider>().copyFrom(GestureProp.empty());
}, },
@ -118,16 +119,17 @@ class _LocalManagerState extends State<LocalManager> {
cursor: SystemMouseCursors.click, cursor: SystemMouseCursors.click,
onEnter: (_) { onEnter: (_) {
setState(() { setState(() {
_hoveringIndex = index; _hoveringItem = localSchemes[index].scheme.id!;
}); });
}, },
child: Container( child: Container(
color: _getItemBackgroundColor(index), color: _getItemBackgroundColor(index, localSchemes[index].scheme.id!),
child: Padding( child: Padding(
padding: const EdgeInsets.only(left: 6, right: 12.0), padding: const EdgeInsets.only(left: 6, right: 12.0),
child: DefaultTextStyle( child: DefaultTextStyle(
style: context.t.textTheme.bodyText2!.copyWith( style: context.t.textTheme.bodyText2!.copyWith(
color: _selectedIndex == index ? Colors.white : null, color:
_selectedItem == localSchemes[index].scheme.id! ? Colors.white : null,
), ),
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
@ -152,9 +154,9 @@ class _LocalManagerState extends State<LocalManager> {
mainAxisAlignment: MainAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.end,
children: [ children: [
DButton.add(enabled: true), DButton.add(enabled: true),
DButton.delete(enabled: _selectedIndex > 0), DButton.delete(enabled: _selectedItem != Uuid.NAMESPACE_NIL),
DButton.duplicate(enabled: _selectedIndex > 0), DButton.duplicate(enabled: _selectedItem != Uuid.NAMESPACE_NIL),
DButton.apply(enabled: _selectedIndex > 0), DButton.apply(enabled: _selectedItem != Uuid.NAMESPACE_NIL),
] ]
.map((e) => Padding( .map((e) => Padding(
padding: const EdgeInsets.only(right: 4), padding: const EdgeInsets.only(right: 4),

@ -1,8 +1,8 @@
import 'package:dde_gesture_manager/constants/constants.dart'; import 'package:dde_gesture_manager/constants/constants.dart';
import 'package:dde_gesture_manager/extensions.dart';
import 'package:dde_gesture_manager/models/settings.provider.dart'; import 'package:dde_gesture_manager/models/settings.provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:dde_gesture_manager/extensions.dart';
class DTextField extends StatefulWidget { class DTextField extends StatefulWidget {
final String? initText; final String? initText;
@ -44,6 +44,14 @@ class _DTextFieldState extends State<DTextField> {
} }
@override @override
void didUpdateWidget(covariant DTextField oldWidget) {
if (oldWidget.initText != widget.initText) {
_controller.text = widget.initText ?? '';
}
super.didUpdateWidget(oldWidget);
}
@override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Focus( return Focus(
child: Builder(builder: (context) { child: Builder(builder: (context) {

Loading…
Cancel
Save