Skip to content

Commit

Permalink
chore: Add test cmds to example app
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Jan 28, 2024
1 parent a7d636e commit 943d946
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 33 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ on:
branches: [main]

jobs:
build-linux:
runs-on: ubuntu-latest
build-android:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
with:
flutter-version: "3.16.8"
channel: "stable"
- run: |
sudo apt-get update
sudo apt-get install -y clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev
sudo apt-get install -y keybinder-3.0
- uses: bluefireteam/melos-action@v2
- working-directory: ./packages/protocol_handler/example
run: |
melos bs
flutter build linux --release
flutter build apk --release
build-macos:
runs-on: macos-latest
Expand Down
5 changes: 5 additions & 0 deletions packages/protocol_handler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.0

* feat: Convert to federated plugin
* fix: Fix iOS platform handler issue #7

## 0.1.6

* fix: macos 3.16 plugin register issue #13
Expand Down
34 changes: 19 additions & 15 deletions packages/protocol_handler/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:protocol_handler/protocol_handler.dart';
import 'package:protocol_handler_example/pages/home.dart';
import 'package:uni_platform/uni_platform.dart';
import 'package:window_manager/window_manager.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

if (!kIsWeb && (Platform.isLinux || Platform.isMacOS || Platform.isWindows)) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
await windowManager.setSize(const Size(600, 400));
await windowManager.center();
await windowManager.show();
});

await protocolHandler.register('myprotocol');
}

await UniPlatform.call<Future<void>>(
desktop: () async {
await windowManager.ensureInitialized();
const windowOptions = WindowOptions(
size: Size(1024, 768),
center: true,
);
windowManager.waitUntilReadyToShow(
windowOptions,
() {
windowManager.show();
},
);
// Register a custom protocol
await protocolHandler.register('myprotocol');
},
otherwise: () => Future.value(),
);
runApp(const MyApp());
}

Expand Down
87 changes: 78 additions & 9 deletions packages/protocol_handler/example/lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:preference_list/preference_list.dart';
import 'package:protocol_handler/protocol_handler.dart';
import 'package:uni_platform/uni_platform.dart';
import 'package:window_manager/window_manager.dart';

class HomePage extends StatefulWidget {
Expand All @@ -13,6 +16,7 @@ class HomePage extends StatefulWidget {

class _HomePageState extends State<HomePage> with ProtocolListener {
bool _isAlwaysOnTop = false;
List<String> _testCmds = [];

String? _initialUrl = '';
final List<String> _receivedUrlList = [];
Expand All @@ -32,24 +36,52 @@ class _HomePageState extends State<HomePage> with ProtocolListener {
}

Future<void> _init() async {
_testCmds = _getTestCmds() ?? [];
if (kDebugMode) {
print('Test commands:');
for (String testCmd in _testCmds) {
print(testCmd);
}
}
_initialUrl = await protocolHandler.getInitialUrl();
setState(() {});
}

Widget _buildBody(BuildContext context) {
return PreferenceList(
children: <Widget>[
UniPlatform.select<Widget>(
desktop: PreferenceListSection(
children: [
PreferenceListSwitchItem(
title: const Text('setAlwaysOnTop'),
value: _isAlwaysOnTop,
onChanged: (newValue) async {
_isAlwaysOnTop = newValue;
await windowManager.setAlwaysOnTop(_isAlwaysOnTop);
setState(() {});
},
),
],
),
otherwise: Container(),
),
PreferenceListSection(
title: const Text('Test cmds'),
children: [
PreferenceListSwitchItem(
title: const Text('setAlwaysOnTop'),
value: _isAlwaysOnTop,
onChanged: (newValue) async {
_isAlwaysOnTop = newValue;
await windowManager.setAlwaysOnTop(_isAlwaysOnTop);
setState(() {});
},
),
if (_testCmds.isNotEmpty)
for (String testCmd in _testCmds)
PreferenceListItem(
padding: const EdgeInsets.all(12),
title: Text(testCmd),
accessoryView: Container(),
onTap: () async {
Clipboard.setData(
ClipboardData(text: testCmd),
);
BotToast.showText(text: 'Copied to clipboard');
},
),
],
),
PreferenceListSection(
Expand Down Expand Up @@ -101,3 +133,40 @@ class _HomePageState extends State<HomePage> with ProtocolListener {
});
}
}

/// Returns a list of test commands to be displayed in the UI.
List<String>? _getTestCmds() {
late final String cmd;
var cmdSuffix = '';

const plainPath = 'path/subpath';
const args = 'path/portion/?uid=123&token=abc';
const emojiArgs =
'?arr%5b%5d=123&arr%5b%5d=abc&addr=1%20Nowhere%20Rd&addr=Rand%20City%F0%9F%98%82';

if (UniPlatform.isWeb) {
return [plainPath, args, emojiArgs];
}

if (UniPlatform.isIOS) {
cmd = '/usr/bin/xcrun simctl openurl booted';
} else if (UniPlatform.isAndroid) {
cmd = '\$ANDROID_HOME/platform-tools/adb shell \'am start'
' -a android.intent.action.VIEW'
' -c android.intent.category.BROWSABLE -d';
cmdSuffix = "'";
} else if (UniPlatform.isMacOS) {
cmd = 'open';
} else if (UniPlatform.isWindows) {
cmd = 'start';
} else {
return null;
}

return [
'$cmd "myprotocol://host/$plainPath"$cmdSuffix',
'$cmd "myprotocol://example.com/$args"$cmdSuffix',
'$cmd "myprotocol://example.com/$emojiArgs"$cmdSuffix',
'$cmd "myprotocol://@@malformed.invalid.url/path?"$cmdSuffix',
];
}
6 changes: 6 additions & 0 deletions packages/protocol_handler/example/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ PODS:
- FlutterMacOS (1.0.0)
- protocol_handler_macos (0.0.1):
- FlutterMacOS
- screen_retriever (0.0.1):
- FlutterMacOS
- window_manager (0.2.0):
- FlutterMacOS

DEPENDENCIES:
- FlutterMacOS (from `Flutter/ephemeral`)
- protocol_handler_macos (from `Flutter/ephemeral/.symlinks/plugins/protocol_handler_macos/macos`)
- screen_retriever (from `Flutter/ephemeral/.symlinks/plugins/screen_retriever/macos`)
- window_manager (from `Flutter/ephemeral/.symlinks/plugins/window_manager/macos`)

EXTERNAL SOURCES:
FlutterMacOS:
:path: Flutter/ephemeral
protocol_handler_macos:
:path: Flutter/ephemeral/.symlinks/plugins/protocol_handler_macos/macos
screen_retriever:
:path: Flutter/ephemeral/.symlinks/plugins/screen_retriever/macos
window_manager:
:path: Flutter/ephemeral/.symlinks/plugins/window_manager/macos

SPEC CHECKSUMS:
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
protocol_handler_macos: d10a6c01d6373389ffd2278013ab4c47ed6d6daa
screen_retriever: 59634572a57080243dd1bf715e55b6c54f241a38
window_manager: 3a1844359a6295ab1e47659b1a777e36773cd6e8

PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367
Expand Down
5 changes: 3 additions & 2 deletions packages/protocol_handler/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ environment:
sdk: ">=3.1.0 <4.0.0"

dependencies:
bot_toast: ^4.0.1
bot_toast: ^4.1.3
flutter:
sdk: flutter
preference_list: ^0.0.1
protocol_handler:
path: ../
window_manager: ^0.2.0
uni_platform: ^0.1.1
window_manager: ^0.3.7

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 943d946

Please sign in to comment.