Skip to content

Commit

Permalink
more strings localized
Browse files Browse the repository at this point in the history
  • Loading branch information
herzhenr committed Sep 23, 2023
1 parent 0f52936 commit 2a9e7b6
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 49 deletions.
69 changes: 56 additions & 13 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"placeholders": {
"ip": {
"type": "String",
"example": "192.168.0.1"
"example": "192.168.0.1",
"description": "The IP address which was provided by the user."
}
}
},
Expand All @@ -38,7 +39,8 @@
"placeholders": {
"mac": {
"type": "String",
"example": "12:AB:00:00:00:00"
"example": "12:AB:00:00:00:00",
"description": "The MAC address which was provided by the user."
}
}
},
Expand All @@ -47,22 +49,44 @@
"placeholders": {
"port": {
"type": "String",
"example": "9"
"example": "9",
"description": "The port number which was provided by the user."
}
}
},
"homeWolCardValid": "Provided device details are valid",
"homeWolCardInvalid": "There was an error trying to send a WOL Package",
"homeWolCardValid": "Provided device details are valid",
"homeWolCardSendWol": "Trying to send WOL Packages",
"homeWolCardSendWolSuccess": "Successfully send WOL packages to {ip}",
"@homeWolCardSendWolSuccess": {
"placeholders": {
"ip": {
"type": "String",
"example": "192.168.0.1",
"description": "The IP address of the device which was woken up."
}
}
},
"homeWolCardSendWolFail": "There was an error when trying to send WOL Packages to the host {ip}",
"@homeWolCardSendWolFail": {
"placeholders": {
"ip": {
"type": "String",
"example": "192.168.0.1",
"description": "The IP address of the device which could not be woken up."
}
}
},
"homeWolCardPingInfo": "Trying to ping device until it is online...",
"homeWolCardPing": "Sending ping #{count}",
"@homeWolCardPing": {
"placeholders": {
"count": {
"type": "String",
"example": "1"
}
}
},
"@homeWolCardPing": {
"placeholders": {
"count": {
"type": "int",
"example": "1"
}
}
},
"homeWolCardPingSuccess": "Device is online",
"homeWolCardPingFail": "Device is still offline",
"@DISCOVER": {},
Expand Down Expand Up @@ -145,5 +169,24 @@
"aboutInfoText": "A simple tool to wake devices in the local network remotely if they are turned off. This app tries to make this process easy for the user.",
"aboutOpenSourceTitle": "Open Source",
"aboutOpenSourceCodeButton": "Source Code",
"aboutOpenSourceLicenseButton": "Licenses"
"aboutOpenSourceLicenseButton": "Licenses",
"aboutVersionText": "Version: {version} ({buildNumber})",
"@aboutVersionText": {
"placeholders": {
"version": {
"type": "String",
"example": "1.0.0"
},
"buildNumber": {
"type": "String",
"example": "1"
}
}
},
"aboutWebPlatformError": "Web platform isn't supported",
"aboutFuchsiaPlatformError": "Fuchsia platform isn't supported",
"aboutLinuxPlatformError": "Linux platform isn't supported",
"aboutMacOSPlatformError": "macOS platform isn't supported",
"aboutWindowsPlatformError": "Windows platform isn't supported",
"aboutNoPlatformDetected": "Failed to get platform version"
}
35 changes: 18 additions & 17 deletions lib/screens/about/about.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@ class _AboutPageState extends State<AboutPage> {
@override
void initState() {
super.initState();
initPlatformState();
initWifiAddress();
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
// not in initSate because buildContext is needed
initPlatformState();
}

Future<void> initPlatformState() async {
var deviceData = <String, dynamic>{};

try {
if (kIsWeb) {
deviceData = <String, dynamic>{
'Error:': 'Web platform isn\'t supported'
'Error:': AppLocalizations.of(context)!.aboutWebPlatformError
};
} else {
switch (defaultTargetPlatform) {
Expand All @@ -60,29 +66,29 @@ class _AboutPageState extends State<AboutPage> {
break;
case TargetPlatform.fuchsia:
deviceData = <String, dynamic>{
'Error:': 'Fuchsia platform isn\'t supported'
'Error:': AppLocalizations.of(context)!.aboutFuchsiaPlatformError
};
break;
case TargetPlatform.linux:
deviceData = <String, dynamic>{
'Error:': 'Linux platform isn\'t supported'
'Error:': AppLocalizations.of(context)!.aboutLinuxPlatformError
};
break;
case TargetPlatform.macOS:
deviceData = <String, dynamic>{
'Error:': 'MacOS platform isn\'t supported'
'Error:': AppLocalizations.of(context)!.aboutMacOSPlatformError
};
break;
case TargetPlatform.windows:
deviceData = <String, dynamic>{
'Error:': 'Windows platform isn\'t supported'
'Error:': AppLocalizations.of(context)!.aboutWindowsPlatformError
};
break;
}
}
} on PlatformException {
deviceData = <String, dynamic>{
'Error:': 'Failed to get platform version.'
'Error:': AppLocalizations.of(context)!.aboutNoPlatformDetected
};
}

Expand Down Expand Up @@ -163,14 +169,10 @@ class _AboutPageState extends State<AboutPage> {
children: [
VersionText(text: widget.packageInfo.appName),
VersionText(text: widget.packageInfo.packageName),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const VersionText(text: "Version "),
VersionText(text: widget.packageInfo.version),
VersionText(text: " (${widget.packageInfo.buildNumber})"),
],
),
VersionText(
text: AppLocalizations.of(context)!.aboutVersionText(
widget.packageInfo.version,
widget.packageInfo.buildNumber)),
],
)
],
Expand All @@ -188,12 +190,11 @@ class _AboutPageState extends State<AboutPage> {
child: InkWell(
borderRadius: AppConstants.borderRadius,
child: ListTile(
// title: const Text("iPhone XR"),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: _deviceData.keys.map((String property) {
return Text('${_deviceData[property]}');
}).toList(), // Text("IP: $_wifiAddress \nMAC: 12:12:12:12:12")
}).toList(),
),
subtitle: Text(
"IP: $_wifiAddress",
Expand Down
3 changes: 2 additions & 1 deletion lib/screens/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ class _HomePageState extends State<HomePage> {
context: context,
builder: (context) {
return StreamBuilder<List<Message>>(
stream: sendWolAndGetMessages(device: device.toNetworkDevice()),
stream: sendWolAndGetMessages(
context: context, device: device.toNetworkDevice()),
builder: (BuildContext context,
AsyncSnapshot<List<Message>> snapshot) {
// set color, text and icon of dialog box according to the arrived messages
Expand Down
64 changes: 46 additions & 18 deletions lib/services/network.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:async';

import 'package:dart_ping/dart_ping.dart';
import 'package:flutter/material.dart';
import 'package:simple_wake_on_lan/constants.dart';
import 'dart:io';
import 'package:wake_on_lan/wake_on_lan.dart';
import 'data.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Stream<NetworkDevice> findDevicesInNetwork(
String networkPrefix,
Expand Down Expand Up @@ -61,37 +63,47 @@ Stream<NetworkDevice> findDevicesInNetwork(
}

/// sends the magic packet to the [device] that should receive a magic wol package in order to get woken up
Stream<Message> sendWolPackage({required NetworkDevice device}) async* {
Stream<Message> sendWolPackage(
{required BuildContext context, required NetworkDevice device}) async* {
// Validate correct formatting of ip and mac addresses
final ip = device.ipAddress;
final mac = device.macAddress;
final int? port = device.wolPort;
bool invalid = false;

if (!IPv4Address.validate(ip)) {
yield Message(text: "'$ip' is an invalid IPv4 address", type: MsgType.error);
yield Message(
text: AppLocalizations.of(context)!.homeWolCardIp(ip),
type: MsgType.error);
invalid = true;
}

if (!MACAddress.validate(mac)) {
yield Message(text: "'$mac' is an invalid MAC address", type: MsgType.error);
yield Message(
text: AppLocalizations.of(context)!.homeWolCardMac(mac),
type: MsgType.error);
invalid = true;
}

//validate port
if (port == null || port < 0 || port > 65535) {
yield Message(text: "'$port' is an invalid port", type: MsgType.error);
String portString = port == null ? "" : port.toString();
yield Message(
text: AppLocalizations.of(context)!.homeWolCardPort(portString),
type: MsgType.error);
invalid = true;
}

if (invalid) {
yield Message(text: "There was an error trying to send a WOL Package", type: MsgType.error);
yield Message(
text: AppLocalizations.of(context)!.homeWolCardInvalid,
type: MsgType.error);
return;
}

// if no error occurred: try to send wol package
yield Message(text: "Provided device details are valid");
yield Message(text: "Trying to send WOL Packages");
yield Message(text: AppLocalizations.of(context)!.homeWolCardValid);
yield Message(text: AppLocalizations.of(context)!.homeWolCardSendWol);

IPv4Address ipv4Address = IPv4Address(ip);
MACAddress macAddress = MACAddress(mac);
Expand All @@ -102,28 +114,43 @@ Stream<Message> sendWolPackage({required NetworkDevice device}) async* {
final broadcast = "$subnet.255";
IPv4Address ipv4Broadcast = IPv4Address(broadcast);

// get localisation string beforehand to avoid using BuildContexts across async gaps
String homeWolCardSendWolSuccess =
AppLocalizations.of(context)!.homeWolCardSendWolSuccess(ip);
String homeWolCardPingInfo =
AppLocalizations.of(context)!.homeWolCardPingInfo;
String homeWolCardPingSuccess =
AppLocalizations.of(context)!.homeWolCardPingSuccess;
String homeWolCardPingFail =
AppLocalizations.of(context)!.homeWolCardPingFail;

try {
WakeOnLAN wol = WakeOnLAN(ipv4Address, macAddress, port: port!);
await wol.wake(repeat: 5);
await wol.wake(repeat: 3);
await Future.delayed(const Duration(seconds: 1));
WakeOnLAN wolBroadcast = WakeOnLAN(ipv4Broadcast, macAddress, port: port);
await wolBroadcast.wake(repeat: 5);
yield Message(
text: "Successfully send WOL packages to $ip", type: MsgType.check);
await wolBroadcast.wake(repeat: 3);
yield Message(text: homeWolCardSendWolSuccess, type: MsgType.check);
} catch (e) {
yield Message(
text: "There was an error when trying to send WOL Packages to this host",
text: AppLocalizations.of(context)!.homeWolCardSendWolFail(ip),
type: MsgType.error);
}

// ping device until it is online
yield Message(text: "Trying to ping device until it is online...");
yield Message(text: homeWolCardPingInfo);
bool online = false;
int tries = 0;
const maxPings = 25;
while (!online && tries < maxPings) {
tries++;
yield Message(text: "Sending ping #$tries", type: MsgType.ping);

// BuildContext has to be used async here to get the current tries in the message
// ignore: use_build_context_synchronously
if (!context.mounted) return;
yield Message(
text: AppLocalizations.of(context)!.homeWolCardPing(tries),
type: MsgType.ping);

final ping = Ping(ip, count: 1, timeout: 5);

Expand All @@ -135,18 +162,19 @@ Stream<Message> sendWolPackage({required NetworkDevice device}) async* {
}
}
if (online) {
yield Message(text: "Device is online", type: MsgType.online);
yield Message(text: homeWolCardPingSuccess, type: MsgType.online);
} else {
yield Message(text: "Device is not online", type: MsgType.error);
yield Message(text: homeWolCardPingFail, type: MsgType.error);
}
}

/// returns a list of Messages by using the sendWolPackage function
/// accumulates the messages in a list and yields the list after each message
Stream<List<Message>> sendWolAndGetMessages(
{required NetworkDevice device}) async* {
{required BuildContext context, required NetworkDevice device}) async* {
List<Message> messages = [];
await for (Message message in sendWolPackage(device: device)) {
await for (Message message
in sendWolPackage(context: context, device: device)) {
// if last message is ping, replace it with the new one
if (messages.isNotEmpty &&
messages.last.type == MsgType.ping &&
Expand Down

0 comments on commit 2a9e7b6

Please sign in to comment.