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:
+83
-2
@@ -9,6 +9,7 @@ import 'providers/auth_provider.dart';
|
||||
import 'providers/capture_queue_provider.dart';
|
||||
import 'providers/notes_provider.dart';
|
||||
import 'providers/settings_provider.dart';
|
||||
import 'providers/update_provider.dart';
|
||||
import 'providers/tasks_provider.dart';
|
||||
import 'screens/auth/login_screen.dart';
|
||||
import 'screens/chat/chat_screen.dart';
|
||||
@@ -127,12 +128,29 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
);
|
||||
});
|
||||
|
||||
class _Shell extends ConsumerWidget {
|
||||
class _Shell extends ConsumerStatefulWidget {
|
||||
final Widget child;
|
||||
const _Shell({required this.child});
|
||||
|
||||
@override
|
||||
ConsumerState<_Shell> createState() => _ShellState();
|
||||
}
|
||||
|
||||
class _ShellState extends ConsumerState<_Shell> {
|
||||
static const _tabs = [Routes.notes, Routes.tasks, Routes.conversations];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Silent update check on first app load.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final repoUrl = ref.read(forgejoRepoUrlProvider);
|
||||
if (repoUrl != null && repoUrl.isNotEmpty) {
|
||||
ref.read(updateProvider.notifier).check(repoUrl);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
int _tabIndex(String location) {
|
||||
for (var i = 0; i < _tabs.length; i++) {
|
||||
if (location.startsWith(_tabs[i])) return i;
|
||||
@@ -140,10 +158,73 @@ class _Shell extends ConsumerWidget {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _showUpdateDialog(UpdateState update) {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) => Consumer(
|
||||
builder: (context, ref, _) {
|
||||
final state = ref.watch(updateProvider);
|
||||
final isDownloading = state.status == UpdateStatus.downloading;
|
||||
return AlertDialog(
|
||||
title: const Text('Update available'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Version ${state.latestVersion} is ready to install.'),
|
||||
if (state.currentVersion != null)
|
||||
Text(
|
||||
'Installed: v${state.currentVersion}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
if (isDownloading) ...[
|
||||
const SizedBox(height: 16),
|
||||
LinearProgressIndicator(
|
||||
value: state.downloadProgress > 0
|
||||
? state.downloadProgress
|
||||
: null,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Downloading… '
|
||||
'${(state.downloadProgress * 100).toStringAsFixed(0)}%',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text('Later'),
|
||||
),
|
||||
if (!isDownloading)
|
||||
FilledButton(
|
||||
onPressed: () => ref
|
||||
.read(updateProvider.notifier)
|
||||
.downloadAndInstall(),
|
||||
child: const Text('Download & Install'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
Widget build(BuildContext context) {
|
||||
// Show update dialog once when a new version is detected.
|
||||
ref.listen(updateProvider, (prev, next) {
|
||||
if (next.status == UpdateStatus.available &&
|
||||
prev?.status != UpdateStatus.available) {
|
||||
WidgetsBinding.instance
|
||||
.addPostFrameCallback((_) => _showUpdateDialog(next));
|
||||
}
|
||||
});
|
||||
final location = GoRouterState.of(context).matchedLocation;
|
||||
final index = _tabIndex(location);
|
||||
final child = widget.child;
|
||||
// Use NavigationRail whenever the screen is wide enough — covers both
|
||||
// phone landscape and tablets in either orientation (600 dp breakpoint).
|
||||
final isWide = MediaQuery.of(context).size.width >= 600;
|
||||
|
||||
@@ -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);
|
||||
@@ -5,6 +5,7 @@ import 'package:go_router/go_router.dart';
|
||||
import '../../core/constants.dart';
|
||||
import '../../providers/auth_provider.dart';
|
||||
import '../../providers/settings_provider.dart';
|
||||
import '../../providers/update_provider.dart';
|
||||
|
||||
class SettingsScreen extends ConsumerWidget {
|
||||
const SettingsScreen({super.key});
|
||||
@@ -13,6 +14,8 @@ class SettingsScreen extends ConsumerWidget {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final serverUrl = ref.watch(serverUrlProvider);
|
||||
final themeMode = ref.watch(themeModeProvider);
|
||||
final repoUrl = ref.watch(forgejoRepoUrlProvider);
|
||||
final update = ref.watch(updateProvider);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Settings')),
|
||||
@@ -52,6 +55,59 @@ class SettingsScreen extends ConsumerWidget {
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
|
||||
// ── Updates ──────────────────────────────────────────────────────
|
||||
ListTile(
|
||||
leading: const Icon(Icons.update),
|
||||
title: const Text('Update repository'),
|
||||
subtitle: Text(repoUrl?.isNotEmpty == true
|
||||
? repoUrl!
|
||||
: 'Not configured — tap to set'),
|
||||
onTap: () => _editRepoUrl(context, ref, repoUrl),
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.info_outline),
|
||||
title: const Text('App version'),
|
||||
subtitle: _versionSubtitle(update),
|
||||
trailing: update.status == UpdateStatus.checking
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: TextButton(
|
||||
onPressed: () {
|
||||
final url = ref.read(forgejoRepoUrlProvider);
|
||||
if (url == null || url.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(
|
||||
'Set a repository URL first.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
ref.read(updateProvider.notifier).check(url);
|
||||
},
|
||||
child: const Text('Check'),
|
||||
),
|
||||
),
|
||||
if (update.status == UpdateStatus.available ||
|
||||
update.status == UpdateStatus.downloading)
|
||||
_UpdateTile(update: update),
|
||||
if (update.status == UpdateStatus.error)
|
||||
ListTile(
|
||||
leading:
|
||||
const Icon(Icons.error_outline, color: Colors.red),
|
||||
title: const Text('Update check failed'),
|
||||
subtitle: Text(
|
||||
update.errorMessage ?? 'Unknown error',
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
|
||||
const Divider(),
|
||||
ListTile(
|
||||
title: const Text('Sign Out'),
|
||||
leading: const Icon(Icons.logout),
|
||||
@@ -64,4 +120,96 @@ class SettingsScreen extends ConsumerWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _versionSubtitle(UpdateState update) {
|
||||
final current = update.currentVersion;
|
||||
final latest = update.latestVersion;
|
||||
|
||||
if (current == null && latest == null) {
|
||||
return const Text('Tap "Check" to look for updates');
|
||||
}
|
||||
if (update.status == UpdateStatus.upToDate) {
|
||||
return Text('v$current — up to date');
|
||||
}
|
||||
if (update.status == UpdateStatus.available ||
|
||||
update.status == UpdateStatus.downloading) {
|
||||
return Text('v$current installed');
|
||||
}
|
||||
return Text(current != null ? 'v$current' : '');
|
||||
}
|
||||
|
||||
Future<void> _editRepoUrl(
|
||||
BuildContext context, WidgetRef ref, String? current) async {
|
||||
String draft = current ?? '';
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text('Update repository URL'),
|
||||
content: TextFormField(
|
||||
initialValue: draft,
|
||||
onChanged: (v) => draft = v,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'https://git.example.com/user/fabled_app',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
autocorrect: false,
|
||||
keyboardType: TextInputType.url,
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
ref
|
||||
.read(forgejoRepoUrlProvider.notifier)
|
||||
.setUrl(draft.trim());
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
child: const Text('Save'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _UpdateTile extends ConsumerWidget {
|
||||
final UpdateState update;
|
||||
const _UpdateTile({required this.update});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final isDownloading = update.status == UpdateStatus.downloading;
|
||||
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.system_update, color: Colors.green),
|
||||
title: Text('v${update.latestVersion} available'),
|
||||
subtitle: isDownloading
|
||||
? Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(height: 4),
|
||||
LinearProgressIndicator(
|
||||
value: update.downloadProgress > 0
|
||||
? update.downloadProgress
|
||||
: null,
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'${(update.downloadProgress * 100).toStringAsFixed(0)}%'),
|
||||
],
|
||||
)
|
||||
: const Text('Tap to download and install'),
|
||||
trailing: isDownloading
|
||||
? null
|
||||
: FilledButton(
|
||||
onPressed: () =>
|
||||
ref.read(updateProvider.notifier).downloadAndInstall(),
|
||||
child: const Text('Install'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user