-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to check for updates, implement the menu bar
- Loading branch information
Showing
10 changed files
with
332 additions
and
25 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
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,131 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:package_info_plus/package_info_plus.dart'; | ||
|
||
import 'strings.dart'; | ||
import 'update.dart'; | ||
import 'widgets.dart'; | ||
|
||
enum MenuSelection { | ||
about, | ||
updates, | ||
showMessage, | ||
} | ||
|
||
class MacMenuBar extends StatefulWidget { | ||
const MacMenuBar({super.key, required this.child}); | ||
|
||
final Widget child; | ||
|
||
@override | ||
State<MacMenuBar> createState() => _MacMenuBarState(); | ||
} | ||
|
||
class _MacMenuBarState extends State<MacMenuBar> { | ||
void _checkForUpdates() async { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar( | ||
content: Text('Checking for updates...'), | ||
), | ||
); | ||
|
||
// Check for updates | ||
Update? update; | ||
String? error; | ||
try { | ||
update = await checkForUpdate(); | ||
} catch (e) { | ||
error = e.toString(); | ||
} | ||
if (!mounted) return; | ||
ScaffoldMessenger.of(context).removeCurrentSnackBar(); | ||
if (update != null) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
duration: const Duration(seconds: 20), | ||
showCloseIcon: true, | ||
content: Text('Update available: ${update.version}'), | ||
action: SnackBarAction( | ||
label: 'Download', | ||
onPressed: () { | ||
update!.open(); | ||
}, | ||
), | ||
), | ||
); | ||
} else if (error != null) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
showCloseIcon: true, | ||
content: Text('Error: $error'), | ||
), | ||
); | ||
} else { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar( | ||
showCloseIcon: true, | ||
content: Text('No updates available'), | ||
), | ||
); | ||
} | ||
} | ||
|
||
void _handleMenuSelection(MenuSelection value) async { | ||
final packageInfo = await PackageInfo.fromPlatform(); | ||
if (!mounted) return; | ||
switch (value) { | ||
case MenuSelection.about: | ||
showAboutDialog( | ||
context: context, | ||
applicationName: packageInfo.appName, | ||
applicationVersion: | ||
"${packageInfo.version}+${packageInfo.buildNumber}", | ||
children: const [ | ||
TextLink( | ||
url: Strings.repoURL, | ||
text: 'View on GitHub', | ||
) | ||
], | ||
); | ||
case MenuSelection.showMessage: | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar( | ||
content: Text('Hello from the menu!'), | ||
), | ||
); | ||
case MenuSelection.updates: | ||
_checkForUpdates(); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return PlatformMenuBar( | ||
menus: [ | ||
PlatformMenu( | ||
label: 'Alic', | ||
menus: [ | ||
PlatformMenuItemGroup( | ||
members: [ | ||
PlatformMenuItem( | ||
label: 'About', | ||
onSelected: () { | ||
_handleMenuSelection(MenuSelection.about); | ||
}, | ||
), | ||
PlatformMenuItem( | ||
label: 'Check for Updates...', | ||
onSelected: () { | ||
_handleMenuSelection(MenuSelection.updates); | ||
}, | ||
), | ||
], | ||
), | ||
const PlatformProvidedMenuItem( | ||
type: PlatformProvidedMenuItemType.quit), | ||
], | ||
), | ||
], | ||
child: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Strings { | ||
static const downloadURL = | ||
'https://github.com/blopker/alic/releases/latest/download/Alic.Image.Compressor.dmg'; | ||
static const githubAPI = | ||
'https://api.github.com/repos/blopker/alic/tags?per_page=10'; | ||
static const repoURL = 'https://github.com/blopker/alic/'; | ||
} |
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,66 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:package_info_plus/package_info_plus.dart'; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
||
import 'strings.dart'; | ||
|
||
@immutable | ||
class Update { | ||
final String version; | ||
final int buildNumber; | ||
final String url; | ||
|
||
const Update( | ||
{required this.version, required this.url, required this.buildNumber}); | ||
|
||
void open() { | ||
launchUrl(Uri.parse(url)); | ||
} | ||
} | ||
|
||
Future<Update> getLatestBuildNumber() async { | ||
final uri = Uri.parse(Strings.githubAPI); | ||
final response = | ||
await http.get(uri, headers: {"Accept": "application/vnd.github+json"}); | ||
|
||
if (response.statusCode == 200) { | ||
final List decodedData = jsonDecode(response.body); | ||
final tags = decodedData.map((e) => e['name'].toString()).toList(); | ||
final latestBuild = tags.firstWhere((element) => element.contains('+')); | ||
final buildNumber = int.parse(latestBuild.split('+').last); | ||
return Update( | ||
version: latestBuild, | ||
buildNumber: buildNumber, | ||
url: Strings.downloadURL, | ||
); | ||
} else { | ||
throw Exception('Failed to fetch tags: ${response.statusCode}'); | ||
} | ||
} | ||
|
||
Future<Update?> checkForUpdate({bool force = false}) async { | ||
// 1. Get your current build number | ||
PackageInfo packageInfo = await PackageInfo.fromPlatform(); | ||
int currentBuildNumber = int.parse(packageInfo.buildNumber); | ||
|
||
// 2. Get the latest build number | ||
final update = await getLatestBuildNumber(); | ||
int latestBuildNumber = update.buildNumber; | ||
|
||
// 3. Compare | ||
if (latestBuildNumber > currentBuildNumber || force) { | ||
debugPrint('Update available: ${update.version}'); | ||
return update; | ||
} else { | ||
debugPrint('You have the latest version'); | ||
return null; | ||
} | ||
} | ||
|
||
void main() async { | ||
final buildNumber = await getLatestBuildNumber(); | ||
print('Latest Build Number: $buildNumber'); | ||
} |
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,25 @@ | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
||
class TextLink extends StatelessWidget { | ||
const TextLink({super.key, required this.url, required this.text}); | ||
|
||
final String url; | ||
final String text; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return RichText( | ||
text: TextSpan( | ||
text: text, | ||
style: const TextStyle( | ||
color: Colors.blue, | ||
), | ||
recognizer: TapGestureRecognizer() | ||
..onTap = () { | ||
launchUrl(Uri.parse(url)); | ||
}, | ||
)); | ||
} | ||
} |
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
Oops, something went wrong.