Skip to content

Commit

Permalink
uri encoder migrate to riverpod
Browse files Browse the repository at this point in the history
  • Loading branch information
laiiihz committed Mar 22, 2022
1 parent 1db7c7a commit deb11fb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
import 'package:alga/constants/import_helper.dart';
import 'package:alga/tools/encoders_decoders/uri_encoder_decoder/uri_provider.dart';
part './uri_provider.dart';

class UriEncoderDecoderView extends StatefulWidget {
class UriEncoderDecoderView extends StatelessWidget {
const UriEncoderDecoderView({Key? key}) : super(key: key);

@override
State<UriEncoderDecoderView> createState() => _UriEncoderDecoderViewState();
}

class _UriEncoderDecoderViewState extends State<UriEncoderDecoderView> {
final _provider = UriProvider();
update() {
setState(() {});
}

@override
void initState() {
super.initState();
_provider.addListener(update);
}

@override
void dispose() {
_provider.removeListener(update);
_provider.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return ToolView.scrollVertical(
Expand All @@ -38,57 +14,77 @@ class _UriEncoderDecoderViewState extends State<UriEncoderDecoderView> {
leading: const Icon(Icons.swap_horiz_sharp),
title: Text(S.of(context).conversion),
subtitle: Text(S.of(context).selectConversion),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
_provider.isEncode
? Text(S.of(context).encode)
: Text(S.of(context).decode),
Switch(
value: _provider.isEncode,
onChanged: (state) {
_provider.isEncode = state;
},
),
],
),
trailing: Consumer(builder: (context, ref, _) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
ref.watch(_isEncode)
? Text(S.of(context).encode)
: Text(S.of(context).decode),
Switch(
value: ref.watch(_isEncode),
onChanged: (state) {
ref.read(_isEncode.notifier).state = state;
},
),
],
);
}),
),
],
),
AppTitleWrapper(
title: S.of(context).input,
actions: [
IconButton(
icon: const Icon(Icons.paste),
onPressed: () => _provider.paste(),
),
IconButton(
icon: const Icon(Icons.clear),
onPressed: () => _provider.clear(),
),
RefReadonly(builder: (ref) {
return IconButton(
icon: const Icon(Icons.paste),
onPressed: () async {
ref.read(_input).text = await ClipboardUtil.paste();
ref.refresh(_result);
},
);
}),
RefReadonly(builder: (ref) {
return IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
ref.read(_input).clear();
ref.refresh(_result);
},
);
}),
],
child: TextField(
maxLines: 12,
minLines: 2,
controller: _provider.inputController,
onChanged: (_) {
_provider.convert();
},
),
child: Consumer(builder: (context, ref, _) {
return TextField(
maxLines: 12,
minLines: 2,
controller: ref.watch(_input),
onChanged: (_) {
ref.refresh(_result);
},
);
}),
),
AppTitleWrapper(
title: S.of(context).output,
actions: [
IconButton(
icon: const Icon(Icons.copy),
onPressed: () => _provider.copy(),
),
RefReadonly(builder: (ref) {
return IconButton(
icon: const Icon(Icons.copy),
onPressed: () {
ClipboardUtil.copy(ref.read(_result));
},
);
}),
],
child: AppTextBox(
maxLines: 12,
minLines: 2,
data: _provider.uriResult,
),
child: Consumer(builder: (context, ref, _) {
return AppTextBox(
maxLines: 12,
minLines: 2,
data: ref.watch(_result),
);
}),
),
],
);
Expand Down
55 changes: 13 additions & 42 deletions lib/tools/encoders_decoders/uri_encoder_decoder/uri_provider.dart
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@
import 'package:alga/utils/clipboard_util.dart';
import 'package:flutter/material.dart';
part of './uri_encoder_decoder.dart';

class UriProvider extends ChangeNotifier {
final inputController = TextEditingController();
String _uriResult = '';
String get uriResult => _uriResult;
set uriResult(String state) {
_uriResult = state;
notifyListeners();
}
final _input = StateProvider.autoDispose<TextEditingController>((ref) {
final controller = TextEditingController();
ref.onDispose(controller.dispose);
return controller;
});

bool _isEncode = true;
bool get isEncode => _isEncode;
set isEncode(bool state) {
_isEncode = state;
notifyListeners();
}
final _isEncode = StateProvider.autoDispose((ref) => true);

clear() {
inputController.clear();
uriResult = '';
}

paste() async {
inputController.text = await ClipboardUtil.paste();
convert();
}

copy() async {
await ClipboardUtil.copy(uriResult);
}

convert() {
uriResult = _isEncode
? Uri.encodeFull(inputController.text)
: Uri.decodeFull(inputController.text);
}

@override
void dispose() {
inputController.dispose();
super.dispose();
}
}
final _result = StateProvider.autoDispose<String>((ref) {
final isEncode = ref.watch(_isEncode);
final text = ref.watch(_input).text;
if (text.isEmpty) return '';
return isEncode ? Uri.encodeFull(text) : Uri.decodeFull(text);
});

0 comments on commit deb11fb

Please sign in to comment.