-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ffi value notifier, to be able to re-use session model across UI …
…components
- Loading branch information
Showing
8 changed files
with
191 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package app | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrMissingTab = errors.New("missing tab") | ||
AccountTab = Tab("account") | ||
DeveloperTab = Tab("developer") | ||
VPNTab = Tab("vpn") | ||
UnknownTab = Tab("") | ||
) | ||
|
||
// Identifies a specific tab in the desktop app | ||
type Tab string | ||
|
||
// Parse the given string into a Tab | ||
func ParseTab(s string) (Tab, error) { | ||
normalized := strings.ToLower(strings.TrimSpace(s)) | ||
if normalized == "" { | ||
// leave currency empty | ||
return UnknownTab, ErrMissingTab | ||
} | ||
return Tab(normalized), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export 'dart:convert'; | ||
export 'dart:ffi'; // For FFI | ||
|
||
export 'package:lantern/desktop/ffi.dart'; | ||
export 'package:ffi/ffi.dart'; | ||
export 'package:ffi/src/utf8.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import 'common.dart'; | ||
import 'common_desktop.dart'; | ||
|
||
|
||
class FfiValueNotifier<T> extends SubscribedNotifier<T?> { | ||
FfiValueNotifier( | ||
Pointer<Utf8> Function() ffiFunction, | ||
T? defaultValue, | ||
void Function() removeFromCache, { | ||
bool details = false, | ||
T Function(Uint8List serialized)? deserialize, | ||
}) : super(defaultValue, removeFromCache) { | ||
value = ffiFunction().toDartString() as T?; | ||
} | ||
} | ||
|
||
/// A ValueListenableBuilder that obtains its single value by subscribing to a | ||
/// path in the database. | ||
class FfiValueBuilder<T> extends ValueListenableBuilder<T?> { | ||
FfiValueBuilder( | ||
String path, | ||
ValueNotifier<T?> notifier, | ||
ValueWidgetBuilder<T> builder, | ||
) : super( | ||
valueListenable: notifier, | ||
builder: (BuildContext context, T? value, Widget? child) => | ||
value == null ? const SizedBox() : builder(context, value, child), | ||
); | ||
|
||
@override | ||
_FfiValueBuilderState createState() => | ||
_FfiValueBuilderState<T>(); | ||
} | ||
|
||
class _FfiValueBuilderState<T> | ||
extends State<ValueListenableBuilder<T?>> { | ||
T? value; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
value = widget.valueListenable.value; | ||
widget.valueListenable.addListener(_valueChanged); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(ValueListenableBuilder<T?> oldWidget) { | ||
if (oldWidget.valueListenable != widget.valueListenable) { | ||
oldWidget.valueListenable.removeListener(_valueChanged); | ||
value = widget.valueListenable.value; | ||
widget.valueListenable.addListener(_valueChanged); | ||
} | ||
super.didUpdateWidget(oldWidget); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
widget.valueListenable.removeListener(_valueChanged); | ||
super.dispose(); | ||
} | ||
|
||
void _valueChanged() { | ||
setState(() { | ||
value = widget.valueListenable.value; | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return widget.builder(context, value, widget.child); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters