Skip to content

Commit

Permalink
chore: display if there is a new version available in about page
Browse files Browse the repository at this point in the history
this might be an entry for in-app-update
  • Loading branch information
MSOB7YY committed May 19, 2024
1 parent a1ec555 commit 2e6b377
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
65 changes: 64 additions & 1 deletion lib/ui/pages/about_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: depend_on_referenced_packages, implementation_imports

import 'dart:convert';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mailer/flutter_mailer.dart';
Expand All @@ -20,9 +22,26 @@ import 'package:namida/ui/widgets/custom_widgets.dart';
import 'package:namida/ui/widgets/settings/extra_settings.dart';
import 'package:namida/ui/widgets/settings_card.dart';

class AboutPage extends StatelessWidget {
String? _latestCheckedVersion;

class AboutPage extends StatefulWidget {
const AboutPage({super.key});

@override
State<AboutPage> createState() => _AboutPageState();
}

class _AboutPageState extends State<AboutPage> {
@override
void initState() {
super.initState();
if (_latestCheckedVersion == null && NamidaDeviceInfo.version != null) {
_checkNewVersion(NamidaDeviceInfo.version!).then((value) {
if (value != null) refreshState(() => _latestCheckedVersion = value);
});
}
}

String _getDateDifferenceText() {
final buildDate = NamidaDeviceInfo.buildDate;
if (buildDate == null) return '';
Expand All @@ -36,13 +55,36 @@ class AboutPage extends StatelessWidget {
return '';
}

Future<String?> _checkNewVersion(String current) async {
try {
final response = await http.get(Uri(scheme: 'https', host: 'api.github.com', path: '/repos/namidaco/namida/releases/latest'));
final resMap = jsonDecode(response.body) as Map;
String? latestRelease = resMap['name'] as String?;
if (latestRelease == null) return null;
if (latestRelease.startsWith('v')) latestRelease = latestRelease.substring(1);
if (current.startsWith('v')) current = current.substring(1);
if (latestRelease == current) return null;
return latestRelease;
} catch (_) {}
return null;
}

@override
Widget build(BuildContext context) {
final imageSize = context.width * 0.25;
final topPadding = imageSize / 2;
const textTopPadding = 28.0 * 2;
final version = NamidaDeviceInfo.version ?? '';
final buildDateDiff = _getDateDifferenceText();
String latestVersion = _latestCheckedVersion ?? '';
if (latestVersion != '') {
if (version.endsWith('beta') ^ latestVersion.endsWith('beta')) {
latestVersion = ''; // both should end with/without beta
} else {
if (!latestVersion.startsWith('v')) latestVersion = "v$latestVersion";
}
}

return BackgroundWrapper(
child: ListView(
padding: kBottomPaddingInsets,
Expand Down Expand Up @@ -303,6 +345,27 @@ class AboutPage extends StatelessWidget {
title: 'App Version',
subtitle: version,
link: AppSocial.GITHUB_RELEASES,
trailing: latestVersion == ''
? null
: NamidaInkWell(
borderRadius: 8.0,
bgColor: context.theme.cardColor,
padding: const EdgeInsets.symmetric(horizontal: 6.0, vertical: 3.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
latestVersion,
style: context.textTheme.displaySmall,
),
const SizedBox(width: 4.0),
const Icon(
Broken.arrow_up_1,
size: 14.0,
)
],
),
),
),
NamidaAboutListTile(
icon: Broken.clipboard_text,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 2.5.0-beta+240519182
version: 2.5.1-beta+240519184

environment:
sdk: ">=3.1.4 <4.0.0"
Expand Down

0 comments on commit 2e6b377

Please sign in to comment.