Add self-hosted update system via Forgejo releases
The app now checks for new APK releases on a Forgejo instance and can download and install them without leaving the app. Settings: - New "Update repository URL" field (e.g. https://git.example.com/user/fabled_app) - "App version" tile with a manual "Check" button and live status display - Download progress bar and "Install" button appear when update is found Startup: - Shell runs a silent background check on first load when a repo URL is set - A dialog appears automatically if a newer release is found Provider (update_provider.dart): - UpdateNotifier: idle → checking → available → downloading → available - Hits Forgejo /api/v1/repos/{owner}/{repo}/releases/latest - Parses semver tag, finds .apk asset, downloads with progress via Dio - Opens the APK with open_file to trigger the system package installer Android: - REQUEST_INSTALL_PACKAGES permission - FileProvider configured for open_file (shares APK URI with installer) - res/xml/file_paths.xml covers external-files and cache directories Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
const _kServerUrl = 'server_url';
|
||||
const _kThemeMode = 'theme_mode';
|
||||
const _kForgejoRepoUrl = 'forgejo_repo_url';
|
||||
|
||||
final sharedPreferencesProvider = Provider<SharedPreferences>((ref) {
|
||||
throw UnimplementedError('Override in ProviderScope');
|
||||
@@ -42,6 +43,23 @@ class ThemeModeNotifier extends StateNotifier<ThemeMode> {
|
||||
}
|
||||
}
|
||||
|
||||
final forgejoRepoUrlProvider =
|
||||
StateNotifierProvider<ForgejoRepoUrlNotifier, String?>((ref) {
|
||||
return ForgejoRepoUrlNotifier(ref.watch(sharedPreferencesProvider));
|
||||
});
|
||||
|
||||
class ForgejoRepoUrlNotifier extends StateNotifier<String?> {
|
||||
final SharedPreferences _prefs;
|
||||
ForgejoRepoUrlNotifier(this._prefs)
|
||||
: super(_prefs.getString(_kForgejoRepoUrl));
|
||||
|
||||
Future<void> setUrl(String url) async {
|
||||
final clean = url.endsWith('/') ? url.substring(0, url.length - 1) : url;
|
||||
await _prefs.setString(_kForgejoRepoUrl, clean);
|
||||
state = clean;
|
||||
}
|
||||
}
|
||||
|
||||
final serverUrlProvider = StateNotifierProvider<ServerUrlNotifier, String?>((ref) {
|
||||
final prefs = ref.watch(sharedPreferencesProvider);
|
||||
return ServerUrlNotifier(prefs);
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:open_file/open_file.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
enum UpdateStatus { idle, checking, available, downloading, upToDate, error }
|
||||
|
||||
class UpdateState {
|
||||
final UpdateStatus status;
|
||||
final String? currentVersion;
|
||||
final String? latestVersion;
|
||||
final String? downloadUrl;
|
||||
final double downloadProgress;
|
||||
final String? errorMessage;
|
||||
|
||||
const UpdateState({
|
||||
this.status = UpdateStatus.idle,
|
||||
this.currentVersion,
|
||||
this.latestVersion,
|
||||
this.downloadUrl,
|
||||
this.downloadProgress = 0.0,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
UpdateState copyWith({
|
||||
UpdateStatus? status,
|
||||
String? currentVersion,
|
||||
String? latestVersion,
|
||||
String? downloadUrl,
|
||||
double? downloadProgress,
|
||||
String? errorMessage,
|
||||
}) =>
|
||||
UpdateState(
|
||||
status: status ?? this.status,
|
||||
currentVersion: currentVersion ?? this.currentVersion,
|
||||
latestVersion: latestVersion ?? this.latestVersion,
|
||||
downloadUrl: downloadUrl ?? this.downloadUrl,
|
||||
downloadProgress: downloadProgress ?? this.downloadProgress,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
class UpdateNotifier extends Notifier<UpdateState> {
|
||||
@override
|
||||
UpdateState build() => const UpdateState();
|
||||
|
||||
/// [repoUrl] is the Forgejo repo page URL, e.g.
|
||||
/// "https://git.example.com/user/fabled_app"
|
||||
Future<void> check(String repoUrl) async {
|
||||
state = state.copyWith(status: UpdateStatus.checking);
|
||||
try {
|
||||
final packageInfo = await PackageInfo.fromPlatform();
|
||||
final currentVersion = packageInfo.version;
|
||||
|
||||
// Parse repo URL → Forgejo API endpoint
|
||||
final uri = Uri.parse(repoUrl);
|
||||
final parts =
|
||||
uri.pathSegments.where((s) => s.isNotEmpty).toList();
|
||||
if (parts.length < 2) throw 'Invalid repository URL (need /owner/repo)';
|
||||
final apiUrl =
|
||||
'${uri.scheme}://${uri.authority}/api/v1/repos/${parts[0]}/${parts[1]}/releases/latest';
|
||||
|
||||
final response = await Dio().get(apiUrl);
|
||||
final tagName = (response.data['tag_name'] as String? ?? '').trim();
|
||||
final latestVersion =
|
||||
tagName.startsWith('v') ? tagName.substring(1) : tagName;
|
||||
|
||||
if (_isNewer(latestVersion, currentVersion)) {
|
||||
final assets =
|
||||
(response.data['assets'] as List<dynamic>? ?? [])
|
||||
.cast<Map<String, dynamic>>();
|
||||
final apk = assets.firstWhere(
|
||||
(a) => (a['name'] as String? ?? '').endsWith('.apk'),
|
||||
orElse: () => {},
|
||||
);
|
||||
if (apk.isNotEmpty) {
|
||||
state = state.copyWith(
|
||||
status: UpdateStatus.available,
|
||||
currentVersion: currentVersion,
|
||||
latestVersion: latestVersion,
|
||||
downloadUrl: apk['browser_download_url'] as String?,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
state = state.copyWith(
|
||||
status: UpdateStatus.upToDate,
|
||||
currentVersion: currentVersion,
|
||||
latestVersion: latestVersion,
|
||||
);
|
||||
} catch (e) {
|
||||
state = state.copyWith(
|
||||
status: UpdateStatus.error,
|
||||
errorMessage: e.toString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> downloadAndInstall() async {
|
||||
if (state.downloadUrl == null) return;
|
||||
state = state.copyWith(status: UpdateStatus.downloading, downloadProgress: 0);
|
||||
|
||||
try {
|
||||
final dir = await getExternalStorageDirectory() ??
|
||||
await getTemporaryDirectory();
|
||||
final path = '${dir.path}/fabled_update.apk';
|
||||
|
||||
await Dio().download(
|
||||
state.downloadUrl!,
|
||||
path,
|
||||
onReceiveProgress: (received, total) {
|
||||
if (total > 0) {
|
||||
state = state.copyWith(downloadProgress: received / total);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
await OpenFile.open(
|
||||
path,
|
||||
type: 'application/vnd.android.package-archive',
|
||||
);
|
||||
|
||||
// Return to available so the user can retry install if they dismissed it.
|
||||
state = state.copyWith(
|
||||
status: UpdateStatus.available,
|
||||
downloadProgress: 1.0,
|
||||
);
|
||||
} catch (e) {
|
||||
state = state.copyWith(
|
||||
status: UpdateStatus.error,
|
||||
errorMessage: e.toString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void dismiss() => state = const UpdateState();
|
||||
|
||||
bool _isNewer(String latest, String current) {
|
||||
try {
|
||||
final l = latest.split('.').map(int.parse).toList();
|
||||
final c = current.split('.').map(int.parse).toList();
|
||||
for (var i = 0; i < l.length && i < c.length; i++) {
|
||||
if (l[i] > c[i]) return true;
|
||||
if (l[i] < c[i]) return false;
|
||||
}
|
||||
return l.length > c.length;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final updateProvider =
|
||||
NotifierProvider<UpdateNotifier, UpdateState>(UpdateNotifier.new);
|
||||
Reference in New Issue
Block a user