Skip to content

Commit

Permalink
New update manager
Browse files Browse the repository at this point in the history
  • Loading branch information
atomic-junky committed Jun 7, 2024
1 parent 78a507a commit 4ceff6e
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 16 deletions.
13 changes: 12 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />*
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<application
android:label="Catsuka"
android:name="${applicationName}"
Expand Down Expand Up @@ -31,5 +33,14 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<provider
android:name="sk.fourq.otaupdate.OtaUpdateFileProvider"
android:authorities="${applicationId}.ota_update_provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
</manifest>
4 changes: 4 additions & 0 deletions android/app/src/main/res/xml/filepaths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal_apk_storage" path="ota_update/"/>
</paths>
76 changes: 76 additions & 0 deletions lib/components/update_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:flutter/material.dart';
import 'package:ota_update/ota_update.dart';

import '../utils/update_manager.dart';

OverlayEntry updateDialog() {
late OverlayEntry entry;
entry = OverlayEntry(
builder: (BuildContext context) {
return UpdateDialog(entry: entry);
}
);

return entry;
}


class UpdateDialog extends StatefulWidget {
final OverlayEntry entry;

UpdateDialog({required this.entry});

@override
State<UpdateDialog> createState() => _UpdateDialog(entry: entry);
}


class _UpdateDialog extends State<UpdateDialog> with TickerProviderStateMixin {
OverlayEntry? entry;
late AlertDialog dialog;
String title = 'Mise à jour disponible';
String content = 'Une nouvelle version de l\'application est disponible. Voulez-vous la télécharger ?';

_UpdateDialog({required this.entry});

@override
Widget build(BuildContext context) {
dialog = AlertDialog(
title: Text(title),
content: Text(content),
actions: <Widget>[
TextButton(
child: const Text('Non'),
onPressed: () {
entry?.remove();
},
),
TextButton(
child: const Text('Oui'),
onPressed: () {
var stream = installUpdate();
stream.then((stream) => {
stream.listen(
(OtaEvent event) {
title = "Downloading...";
content = "${event.value}%";
setState(() {});
if (event.status != OtaStatus.DOWNLOADING) {
entry?.remove();
}
}
)
});
},
),
],
);

return dialog;
}

@override
void initState() {
super.initState();
}
}
39 changes: 26 additions & 13 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import 'components/app_bar.dart';
import 'components/nav_bar.dart';
import 'components/short.dart';
import 'components/news.dart';
import 'components/update_dialog.dart';
import 'screens/video.dart';
import 'screens/news.dart';
import 'screens/breve.dart';
import 'utils/update_manager.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -105,31 +107,42 @@ class Home extends StatefulWidget {

class _Home extends State<Home> {
late Future<List<Widget>> futureNews;
late Future<bool> update;

@override
void initState() {
super.initState();
futureNews = getNews();
update = checkForUpdate();
}

@override
Widget build(BuildContext context) {
return FutureBuilder<List<Widget>>(
future: futureNews,
return FutureBuilder(
future: update,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
return RefreshIndicator(
onRefresh: () async {
List<Widget> freshNews = await getNews(reload: true);
setState(() {
futureNews = Future.value(freshNews);
});
update.then((result) {
if (result) Overlay.of(context).insert(updateDialog());
});

return FutureBuilder<List<Widget>>(
future: futureNews,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
return RefreshIndicator(
onRefresh: () async {
List<Widget> freshNews = await getNews(reload: true);
setState(() {
futureNews = Future.value(freshNews);
});
},
edgeOffset: 100,
color: const Color(0xffe04a25),
backgroundColor: const Color(0xFF122E39),
child: _listView(context, snapshot),
);
},
edgeOffset: 100,
color: const Color(0xffe04a25),
backgroundColor: const Color(0xFF122E39),
child: _listView(context, snapshot),
);
},
}
);
}

Expand Down
39 changes: 39 additions & 0 deletions lib/utils/update_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'dart:async';
import 'dart:convert';

import 'package:ota_update/ota_update.dart';
import 'package:http/http.dart' as http;
import 'package:package_info_plus/package_info_plus.dart';

Future<String> fetchLatestVersion() async {
final response = await http.get(
Uri.parse('https://api.github.com/repos/atomic-junky/CatsukaApp/releases/latest'),
headers: {'Accept': 'application/vnd.github+json'},
);


if (response.statusCode == 200) {
final release = json.decode(response.body);
return release['tag_name'].replaceFirst("v","");
} else {
throw Exception('Failed to load release');
}
}

Future<bool> checkForUpdate() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();

String latestVersion = await fetchLatestVersion();
String currentVersion = packageInfo.version;

return latestVersion != currentVersion;
}

Future<Stream<OtaEvent>> installUpdate() async {
String latestVersion = await fetchLatestVersion();

return OtaUpdate()
.execute(
'https://github.com/atomic-junky/CatsukaApp/releases/download/v$latestVersion/app-arm64-v8a-release.apk',
);
}
10 changes: 9 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,16 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.0"
ota_update:
dependency: "direct main"
description:
name: ota_update
sha256: "8c47531b655f8fcf9961dc7758a933a9ccbcbe1c77a7ec52e5c5cfc2ec367ba7"
url: "https://pub.dev"
source: hosted
version: "6.0.0"
package_info_plus:
dependency: transitive
dependency: "direct main"
description:
name: package_info_plus
sha256: b93d8b4d624b4ea19b0a5a208b2d6eff06004bc3ce74c06040b120eeadd00ce0
Expand Down
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'github' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.1.0
version: 1.2.0

environment:
sdk: '>=3.0.6 <4.0.0'
Expand Down Expand Up @@ -50,6 +50,8 @@ dependencies:
flutter_cache_manager: ^3.3.1
jiffy: ^6.2.1
file: ^7.0.0
ota_update: ^6.0.0
package_info_plus: ^8.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 4ceff6e

Please sign in to comment.