mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 22:33:42 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -0,0 +1,377 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../api/gopeed_site_api.dart';
|
||||
import '../../../api/model/extension.dart';
|
||||
import '../../../api/model/install_extension.dart';
|
||||
import '../../../api/model/store_extension.dart';
|
||||
import '../../../api/model/switch_extension.dart';
|
||||
import '../../../api/model/update_extension_settings.dart';
|
||||
import '../../../core/capabilities/app_capabilities.dart';
|
||||
|
||||
final extensionsControllerProvider = AsyncNotifierProvider<ExtensionsController, ExtensionsState>(
|
||||
ExtensionsController.new,
|
||||
);
|
||||
|
||||
enum ExtensionListFilter { market, installed }
|
||||
|
||||
class ExtensionListItem {
|
||||
const ExtensionListItem({this.installed, this.store});
|
||||
|
||||
final Extension? installed;
|
||||
final StoreExtension? store;
|
||||
|
||||
bool get isInstalled => installed != null;
|
||||
String get id => installed?.identity ?? store!.id;
|
||||
String get title => installed?.title ?? store?.title ?? '';
|
||||
String get author => installed?.author ?? store?.author ?? '';
|
||||
String get description => installed?.description ?? store?.description ?? '';
|
||||
String get version => installed?.version ?? store?.version ?? '0.0.0';
|
||||
String? get homepage => installed?.homepage.isNotEmpty == true ? installed!.homepage : store?.homepage;
|
||||
String? get repoUrl => installed?.repository?.url.isNotEmpty == true ? installed!.repository!.url : store?.repoUrl;
|
||||
int get stars => store?.stars ?? 0;
|
||||
int get installCount => store?.installCount ?? 0;
|
||||
}
|
||||
|
||||
class ExtensionsState {
|
||||
const ExtensionsState({
|
||||
this.installedExtensions = const [],
|
||||
this.storeExtensions = const [],
|
||||
this.updateFlags = const {},
|
||||
this.storePagination,
|
||||
this.storeQuery = '',
|
||||
this.storeSort = StoreExtensionSort.stars,
|
||||
this.listFilter = ExtensionListFilter.market,
|
||||
this.loadingInstalled = false,
|
||||
this.loadingStore = false,
|
||||
this.loadingMoreStore = false,
|
||||
this.busyExtensionIds = const {},
|
||||
this.devMode = false,
|
||||
});
|
||||
|
||||
final List<Extension> installedExtensions;
|
||||
final List<StoreExtension> storeExtensions;
|
||||
final Map<String, String> updateFlags;
|
||||
final StorePagination? storePagination;
|
||||
final String storeQuery;
|
||||
final StoreExtensionSort storeSort;
|
||||
final ExtensionListFilter listFilter;
|
||||
final bool loadingInstalled;
|
||||
final bool loadingStore;
|
||||
final bool loadingMoreStore;
|
||||
final Set<String> busyExtensionIds;
|
||||
final bool devMode;
|
||||
|
||||
UnmodifiableMapView<String, Extension> get installedMap =>
|
||||
UnmodifiableMapView({for (final ext in installedExtensions) ext.identity: ext});
|
||||
|
||||
UnmodifiableMapView<String, StoreExtension> get storeMap =>
|
||||
UnmodifiableMapView({for (final ext in storeExtensions) ext.id: ext});
|
||||
|
||||
List<ExtensionListItem> get displayItems {
|
||||
if (listFilter == ExtensionListFilter.installed) {
|
||||
return installedExtensions
|
||||
.map((ext) => ExtensionListItem(installed: ext, store: storeMap[ext.identity]))
|
||||
.toList();
|
||||
}
|
||||
return storeExtensions.map((ext) => ExtensionListItem(installed: installedMap[ext.id], store: ext)).toList();
|
||||
}
|
||||
|
||||
ExtensionListItem? findItem(String id) {
|
||||
final installed = installedMap[id];
|
||||
final store = storeMap[id];
|
||||
if (installed == null && store == null) return null;
|
||||
return ExtensionListItem(installed: installed, store: store);
|
||||
}
|
||||
|
||||
ExtensionsState copyWith({
|
||||
List<Extension>? installedExtensions,
|
||||
List<StoreExtension>? storeExtensions,
|
||||
Map<String, String>? updateFlags,
|
||||
StorePagination? storePagination,
|
||||
bool clearStorePagination = false,
|
||||
String? storeQuery,
|
||||
StoreExtensionSort? storeSort,
|
||||
ExtensionListFilter? listFilter,
|
||||
bool? loadingInstalled,
|
||||
bool? loadingStore,
|
||||
bool? loadingMoreStore,
|
||||
Set<String>? busyExtensionIds,
|
||||
bool? devMode,
|
||||
}) {
|
||||
return ExtensionsState(
|
||||
installedExtensions: installedExtensions ?? this.installedExtensions,
|
||||
storeExtensions: storeExtensions ?? this.storeExtensions,
|
||||
updateFlags: updateFlags ?? this.updateFlags,
|
||||
storePagination: clearStorePagination ? null : storePagination ?? this.storePagination,
|
||||
storeQuery: storeQuery ?? this.storeQuery,
|
||||
storeSort: storeSort ?? this.storeSort,
|
||||
listFilter: listFilter ?? this.listFilter,
|
||||
loadingInstalled: loadingInstalled ?? this.loadingInstalled,
|
||||
loadingStore: loadingStore ?? this.loadingStore,
|
||||
loadingMoreStore: loadingMoreStore ?? this.loadingMoreStore,
|
||||
busyExtensionIds: busyExtensionIds ?? this.busyExtensionIds,
|
||||
devMode: devMode ?? this.devMode,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionsController extends AsyncNotifier<ExtensionsState> {
|
||||
static const manualInstallBusyKey = '__manual_install__';
|
||||
int _devModeCount = 0;
|
||||
|
||||
@override
|
||||
Future<ExtensionsState> build() async {
|
||||
var next = const ExtensionsState(loadingInstalled: true, loadingStore: true);
|
||||
state = AsyncValue.data(next);
|
||||
final installed = await _loadInstalled(refreshUpdates: true, current: next);
|
||||
next = state.value ?? installed;
|
||||
await _refreshStore(current: next);
|
||||
return state.value ?? next;
|
||||
}
|
||||
|
||||
Future<void> loadInitialData() async {
|
||||
state = const AsyncValue.loading();
|
||||
state = AsyncValue.data(await build());
|
||||
}
|
||||
|
||||
Future<void> loadInstalled({bool refreshUpdates = false}) async {
|
||||
await _loadInstalled(refreshUpdates: refreshUpdates, current: _current.copyWith(loadingInstalled: true));
|
||||
}
|
||||
|
||||
Future<void> refreshStore() async {
|
||||
await _refreshStore(current: _current.copyWith(loadingStore: true));
|
||||
}
|
||||
|
||||
Future<void> loadMoreStore() async {
|
||||
final current = _current;
|
||||
final pagination = current.storePagination;
|
||||
if (pagination == null || !pagination.hasNext || current.loadingMoreStore) return;
|
||||
state = AsyncValue.data(current.copyWith(loadingMoreStore: true));
|
||||
try {
|
||||
final page = await GopeedSiteApi.instance.getExtensions(
|
||||
page: pagination.page + 1,
|
||||
limit: pagination.limit,
|
||||
sort: current.storeSort,
|
||||
query: current.storeQuery,
|
||||
);
|
||||
state = AsyncValue.data(
|
||||
_current.copyWith(
|
||||
storeExtensions: [..._current.storeExtensions, ...page.data],
|
||||
storePagination: page.pagination,
|
||||
loadingMoreStore: false,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
state = AsyncValue.data(_current.copyWith(loadingMoreStore: false));
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> searchStore(String query) async {
|
||||
state = AsyncValue.data(_current.copyWith(storeQuery: query.trim()));
|
||||
await refreshStore();
|
||||
}
|
||||
|
||||
Future<void> changeSort(StoreExtensionSort sort) async {
|
||||
state = AsyncValue.data(_current.copyWith(storeSort: sort));
|
||||
await refreshStore();
|
||||
}
|
||||
|
||||
void changeFilter(ExtensionListFilter filter) {
|
||||
state = AsyncValue.data(_current.copyWith(listFilter: filter));
|
||||
}
|
||||
|
||||
bool canUpdateItem(ExtensionListItem item) {
|
||||
final current = _current;
|
||||
if (item.installed == null) return false;
|
||||
if (current.listFilter == ExtensionListFilter.market && item.store != null) {
|
||||
return _compareVersion(item.store!.version, item.installed!.version) > 0;
|
||||
}
|
||||
return current.updateFlags.containsKey(item.installed!.identity);
|
||||
}
|
||||
|
||||
Future<void> installFromStore(StoreExtension extension) async {
|
||||
await _runBusy(extension.id, () async {
|
||||
final installUrl = (extension.directory ?? '').trim().isEmpty
|
||||
? extension.repoUrl
|
||||
: '${extension.repoUrl}#${extension.directory!.trim()}';
|
||||
final installedId = await ref.read(gopeedServiceProvider).installExtension(InstallExtension(url: installUrl));
|
||||
await loadInstalled(refreshUpdates: false);
|
||||
unawaited(checkUpdate());
|
||||
_bumpStoreInstallCount(installedId.isNotEmpty ? installedId : extension.id);
|
||||
_reportInstallSafe(installedId.isNotEmpty ? installedId : extension.id);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> installFromUrl(String url, {bool devInstall = false}) async {
|
||||
await _runBusy(manualInstallBusyKey, () async {
|
||||
final installedId = await ref
|
||||
.read(gopeedServiceProvider)
|
||||
.installExtension(InstallExtension(devMode: devInstall, url: url));
|
||||
await loadInstalled(refreshUpdates: false);
|
||||
unawaited(checkUpdate());
|
||||
if (installedId.isNotEmpty) {
|
||||
_bumpStoreInstallCount(installedId);
|
||||
_reportInstallSafe(installedId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> toggleExtension(Extension extension, bool enabled) async {
|
||||
await _runBusy(extension.identity, () async {
|
||||
await ref.read(gopeedServiceProvider).switchExtension(extension.identity, SwitchExtension(status: enabled));
|
||||
await loadInstalled(refreshUpdates: false);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> removeExtension(Extension extension) async {
|
||||
await _runBusy(extension.identity, () async {
|
||||
await ref.read(gopeedServiceProvider).deleteExtension(extension.identity);
|
||||
await loadInstalled(refreshUpdates: false);
|
||||
final flags = Map<String, String>.of(_current.updateFlags)..remove(extension.identity);
|
||||
state = AsyncValue.data(_current.copyWith(updateFlags: flags));
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> upgradeExtension(Extension extension) async {
|
||||
await _runBusy(extension.identity, () async {
|
||||
await ref.read(gopeedServiceProvider).updateExtension(extension.identity);
|
||||
await loadInstalled(refreshUpdates: false);
|
||||
unawaited(checkUpdate());
|
||||
_bumpStoreInstallCount(extension.identity);
|
||||
_reportInstallSafe(extension.identity);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> saveExtensionSettings(Extension extension, Map<String, dynamic> settings) async {
|
||||
await _runBusy(extension.identity, () async {
|
||||
await ref
|
||||
.read(gopeedServiceProvider)
|
||||
.updateExtensionSettings(extension.identity, UpdateExtensionSettings(settings: settings));
|
||||
await loadInstalled(refreshUpdates: false);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> checkUpdate() async {
|
||||
final flags = <String, String>{};
|
||||
for (final ext in _current.installedExtensions) {
|
||||
try {
|
||||
final resp = await ref.read(gopeedServiceProvider).upgradeCheckExtension(ext.identity);
|
||||
if (resp.newVersion.isNotEmpty) {
|
||||
flags[ext.identity] = resp.newVersion;
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
state = AsyncValue.data(_current.copyWith(updateFlags: flags));
|
||||
}
|
||||
|
||||
void tryOpenDevMode() {
|
||||
if (_devModeCount == 0) {
|
||||
Future.delayed(const Duration(seconds: 2), () {
|
||||
if (_current.devMode) return;
|
||||
_devModeCount = 0;
|
||||
});
|
||||
}
|
||||
_devModeCount++;
|
||||
if (_devModeCount >= 5) {
|
||||
state = AsyncValue.data(_current.copyWith(devMode: true));
|
||||
}
|
||||
}
|
||||
|
||||
ExtensionsState get _current => state.value ?? const ExtensionsState();
|
||||
|
||||
Future<ExtensionsState> _loadInstalled({required bool refreshUpdates, required ExtensionsState current}) async {
|
||||
state = AsyncValue.data(current.copyWith(loadingInstalled: true));
|
||||
final installed = await ref.read(gopeedServiceProvider).getExtensions();
|
||||
state = AsyncValue.data(_current.copyWith(installedExtensions: installed, loadingInstalled: false));
|
||||
if (refreshUpdates) {
|
||||
await checkUpdate();
|
||||
}
|
||||
return _current;
|
||||
}
|
||||
|
||||
Future<void> _refreshStore({required ExtensionsState current}) async {
|
||||
state = AsyncValue.data(current.copyWith(loadingStore: true));
|
||||
final page = await GopeedSiteApi.instance.getExtensions(
|
||||
page: 1,
|
||||
limit: 20,
|
||||
sort: _current.storeSort,
|
||||
query: _current.storeQuery,
|
||||
);
|
||||
state = AsyncValue.data(
|
||||
_current.copyWith(storeExtensions: page.data, storePagination: page.pagination, loadingStore: false),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _runBusy(String id, Future<void> Function() action) async {
|
||||
if (_current.busyExtensionIds.contains(id)) return;
|
||||
state = AsyncValue.data(_current.copyWith(busyExtensionIds: {..._current.busyExtensionIds, id}));
|
||||
try {
|
||||
await action();
|
||||
} finally {
|
||||
final busy = Set<String>.of(_current.busyExtensionIds)..remove(id);
|
||||
state = AsyncValue.data(_current.copyWith(busyExtensionIds: busy));
|
||||
}
|
||||
}
|
||||
|
||||
void _bumpStoreInstallCount(String id) {
|
||||
final current = _current;
|
||||
final index = current.storeExtensions.indexWhere((e) => e.id == id);
|
||||
if (index < 0) return;
|
||||
final ext = current.storeExtensions[index];
|
||||
final updated = [...current.storeExtensions];
|
||||
updated[index] = StoreExtension(
|
||||
id: ext.id,
|
||||
repoFullName: ext.repoFullName,
|
||||
repoUrl: ext.repoUrl,
|
||||
directory: ext.directory,
|
||||
commitSha: ext.commitSha,
|
||||
name: ext.name,
|
||||
author: ext.author,
|
||||
title: ext.title,
|
||||
description: ext.description,
|
||||
icon: ext.icon,
|
||||
version: ext.version,
|
||||
homepage: ext.homepage,
|
||||
readme: ext.readme,
|
||||
installCount: ext.installCount + 1,
|
||||
stars: ext.stars,
|
||||
topics: ext.topics,
|
||||
createdAt: ext.createdAt,
|
||||
updatedAt: ext.updatedAt,
|
||||
);
|
||||
state = AsyncValue.data(current.copyWith(storeExtensions: updated));
|
||||
}
|
||||
|
||||
void _reportInstallSafe(String id) {
|
||||
unawaited(() async {
|
||||
try {
|
||||
await GopeedSiteApi.instance.reportExtensionInstall(id);
|
||||
} catch (_) {}
|
||||
}());
|
||||
}
|
||||
|
||||
static int _compareVersion(String a, String b) {
|
||||
final aNums = _toVersionNumbers(a);
|
||||
final bNums = _toVersionNumbers(b);
|
||||
final maxLen = aNums.length > bNums.length ? aNums.length : bNums.length;
|
||||
for (var i = 0; i < maxLen; i++) {
|
||||
final left = i < aNums.length ? aNums[i] : 0;
|
||||
final right = i < bNums.length ? bNums[i] : 0;
|
||||
if (left > right) return 1;
|
||||
if (left < right) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static List<int> _toVersionNumbers(String version) {
|
||||
return version
|
||||
.split(RegExp(r'[^0-9]+'))
|
||||
.where((part) => part.isNotEmpty)
|
||||
.map((part) => int.tryParse(part) ?? 0)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../api/model/install_extension.dart';
|
||||
|
||||
final pendingExtensionInstallProvider = NotifierProvider<PendingExtensionInstall, InstallExtension?>(
|
||||
PendingExtensionInstall.new,
|
||||
);
|
||||
|
||||
class PendingExtensionInstall extends Notifier<InstallExtension?> {
|
||||
@override
|
||||
InstallExtension? build() => null;
|
||||
|
||||
void set(InstallExtension? install) {
|
||||
state = install;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:shadcn_flutter/shadcn_flutter.dart' as shad;
|
||||
|
||||
import '../../../../l10n/l10n.dart';
|
||||
import '../../../../shared/theme/app_palette.dart';
|
||||
import '../../../../shared/widgets/detail/app_detail_surface.dart';
|
||||
import '../../application/extensions_controller.dart';
|
||||
import '../widgets/extension_detail_view.dart';
|
||||
|
||||
class ExtensionDetailsPage extends ConsumerWidget {
|
||||
const ExtensionDetailsPage({super.key, required this.extensionId, this.initialItem});
|
||||
|
||||
final String extensionId;
|
||||
final ExtensionListItem? initialItem;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final stateAsync = ref.watch(extensionsControllerProvider);
|
||||
final item = stateAsync.value?.findItem(extensionId) ?? initialItem;
|
||||
final palette = AppPalette.of(context);
|
||||
|
||||
return AppDetailPage(
|
||||
title: item?.title ?? context.l10n.extensions,
|
||||
onBack: () => context.canPop() ? context.pop() : context.go('/extensions'),
|
||||
child: item == null
|
||||
? Center(
|
||||
child: stateAsync.isLoading
|
||||
? const shad.CircularProgressIndicator()
|
||||
: Text(context.l10n.noExtensions, style: TextStyle(color: palette.textMuted, fontSize: 13)),
|
||||
)
|
||||
: ExtensionDetailView(key: ValueKey(item.id), item: item, mobile: true),
|
||||
);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,467 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart' show Icons;
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:shadcn_flutter/shadcn_flutter.dart' as shad;
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../../../api/model/store_extension.dart';
|
||||
import '../../../../l10n/l10n.dart';
|
||||
import '../../../../shared/theme/app_design_tokens.dart';
|
||||
import '../../../../shared/theme/app_palette.dart';
|
||||
import '../../../../shared/widgets/app_loading_button.dart';
|
||||
import '../../../../shared/widgets/app_toast.dart';
|
||||
import '../../../../shared/widgets/detail/app_detail_surface.dart';
|
||||
import '../../../../util/util.dart';
|
||||
import '../../application/extensions_controller.dart';
|
||||
import 'extension_icon.dart';
|
||||
|
||||
class ExtensionDetailDrawer extends StatelessWidget {
|
||||
const ExtensionDetailDrawer({super.key, required this.item, required this.onClose});
|
||||
|
||||
final ExtensionListItem? item;
|
||||
final VoidCallback onClose;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AppDetailDrawer(
|
||||
open: item != null,
|
||||
title: item?.title ?? '',
|
||||
onClose: onClose,
|
||||
drawerKey: const ValueKey('extension-details-drawer'),
|
||||
child: item == null ? const SizedBox.shrink() : ExtensionDetailView(item: item!),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionDetailView extends ConsumerWidget {
|
||||
const ExtensionDetailView({super.key, required this.item, this.mobile = false});
|
||||
|
||||
final ExtensionListItem item;
|
||||
final bool mobile;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final palette = AppPalette.of(context);
|
||||
final state = ref.watch(extensionsControllerProvider).value;
|
||||
final current = state?.findItem(item.id) ?? item;
|
||||
final installed = current.installed;
|
||||
final store = current.store;
|
||||
final busy = state?.busyExtensionIds.contains(current.id) ?? false;
|
||||
final canUpdate = ref.read(extensionsControllerProvider.notifier).canUpdateItem(current);
|
||||
final horizontalPadding = mobile ? AppDesignTokens.space16 : AppDesignTokens.space32;
|
||||
|
||||
return ListView(
|
||||
key: const ValueKey('extension-details-content'),
|
||||
padding: EdgeInsets.fromLTRB(horizontalPadding, mobile ? 24 : 28, horizontalPadding, mobile ? 40 : 48),
|
||||
children: [
|
||||
Row(
|
||||
key: const ValueKey('extension-details-hero'),
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ExtensionIcon(
|
||||
item: current,
|
||||
size: mobile ? 60 : 68,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
fallbackPadding: EdgeInsets.all((mobile ? 60 : 68) * 0.16),
|
||||
fallbackBackgroundColor: palette.surfaceSoft,
|
||||
),
|
||||
SizedBox(width: mobile ? 16 : 20),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
current.author,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
color: palette.textPrimary,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w700,
|
||||
letterSpacing: 0.15,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 7),
|
||||
Text(
|
||||
current.description,
|
||||
style: TextStyle(color: palette.textSecondary, fontSize: mobile ? 14 : 13.5, height: 1.5),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Wrap(
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
spacing: 9,
|
||||
runSpacing: 5,
|
||||
children: [
|
||||
Text(
|
||||
'v${current.version}',
|
||||
style: TextStyle(color: palette.textMuted, fontSize: 11.5, fontWeight: FontWeight.w600),
|
||||
),
|
||||
if (store != null) _ExtensionStats(store: store),
|
||||
if (installed != null && canUpdate)
|
||||
_ExtensionStatus(label: context.l10n.extensionCanUpdate, emphasized: true),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: mobile ? 24 : 28),
|
||||
Wrap(
|
||||
key: const ValueKey('extension-details-actions'),
|
||||
spacing: 4,
|
||||
runSpacing: 6,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
if (installed == null && store != null)
|
||||
AppLoadingButton(
|
||||
key: const ValueKey('extension-details-install'),
|
||||
loading: busy,
|
||||
variant: AppLoadingButtonVariant.primary,
|
||||
icon: const Icon(Icons.download_outlined, size: 16),
|
||||
onPressed: () =>
|
||||
_runAction(context, () => ref.read(extensionsControllerProvider.notifier).installFromStore(store)),
|
||||
child: Text(context.l10n.extensionInstall),
|
||||
),
|
||||
if (installed != null && canUpdate)
|
||||
AppLoadingButton(
|
||||
key: const ValueKey('extension-details-update'),
|
||||
loading: busy,
|
||||
variant: AppLoadingButtonVariant.primary,
|
||||
icon: const Icon(Icons.refresh, size: 16),
|
||||
onPressed: () => _runAction(
|
||||
context,
|
||||
() => ref.read(extensionsControllerProvider.notifier).upgradeExtension(installed),
|
||||
successMessage: context.l10n.extensionUpdateSuccess,
|
||||
),
|
||||
child: Text(context.l10n.newVersionUpdate),
|
||||
),
|
||||
if (installed != null && !canUpdate)
|
||||
AppLoadingButton(
|
||||
key: const ValueKey('extension-details-installed'),
|
||||
loading: false,
|
||||
variant: AppLoadingButtonVariant.primary,
|
||||
icon: const Icon(Icons.check_circle_outline, size: 16),
|
||||
onPressed: null,
|
||||
child: Text(context.l10n.extensionInstalled),
|
||||
),
|
||||
if ((current.homepage ?? '').isNotEmpty)
|
||||
shad.SecondaryButton(
|
||||
key: const ValueKey('extension-details-homepage'),
|
||||
onPressed: () => unawaited(_openUrl(current.homepage!)),
|
||||
child: _LinkButtonContent(icon: Icons.home_outlined, label: context.l10n.homepage),
|
||||
),
|
||||
if ((current.repoUrl ?? '').isNotEmpty)
|
||||
shad.SecondaryButton(
|
||||
key: const ValueKey('extension-details-github'),
|
||||
onPressed: () => unawaited(_openUrl(current.repoUrl!)),
|
||||
child: const _LinkButtonContent(icon: Icons.code, label: 'GitHub'),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (store != null) ...[SizedBox(height: mobile ? 24 : 28), _ExtensionReadme(item: current, mobile: mobile)],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _runAction(BuildContext context, Future<void> Function() action, {String? successMessage}) async {
|
||||
try {
|
||||
await action();
|
||||
if (context.mounted && successMessage != null) {
|
||||
showAppToast(context, successMessage, type: AppToastType.success);
|
||||
}
|
||||
} catch (error) {
|
||||
if (context.mounted) showAppToast(context, error.toString(), type: AppToastType.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _ExtensionReadme extends StatefulWidget {
|
||||
const _ExtensionReadme({required this.item, required this.mobile});
|
||||
|
||||
final ExtensionListItem item;
|
||||
final bool mobile;
|
||||
|
||||
@override
|
||||
State<_ExtensionReadme> createState() => _ExtensionReadmeState();
|
||||
}
|
||||
|
||||
class _ExtensionReadmeState extends State<_ExtensionReadme> {
|
||||
late Future<_ReadmeInfo> _readme;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_readme = _loadReadme(widget.item);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant _ExtensionReadme oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
final previous = oldWidget.item.installed;
|
||||
final current = widget.item.installed;
|
||||
if (previous?.identity != current?.identity ||
|
||||
previous?.devMode != current?.devMode ||
|
||||
previous?.devPath != current?.devPath ||
|
||||
oldWidget.item.store?.readme != widget.item.store?.readme) {
|
||||
_readme = _loadReadme(widget.item);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
return FutureBuilder<_ReadmeInfo>(
|
||||
future: _readme,
|
||||
builder: (context, snapshot) {
|
||||
final info = snapshot.data;
|
||||
if (info == null) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
if (info.content.trim().isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return Padding(
|
||||
key: const ValueKey('extension-details-readme'),
|
||||
padding: EdgeInsets.only(top: widget.mobile ? 32 : 36),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'README',
|
||||
style: TextStyle(
|
||||
color: palette.textMuted,
|
||||
fontSize: 10.5,
|
||||
fontWeight: FontWeight.w800,
|
||||
letterSpacing: 1.2,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
MarkdownBody(
|
||||
data: info.content,
|
||||
selectable: true,
|
||||
styleSheet: _markdownStyle(palette, mobile: widget.mobile),
|
||||
onTapLink: (_, href, _) {
|
||||
final resolved = _resolveReadmeUrl(widget.item, href, info: info, forImage: false);
|
||||
if (resolved != null) unawaited(_openUrl(resolved));
|
||||
},
|
||||
imageBuilder: (uri, title, alt) {
|
||||
final resolved = _resolveReadmeUrl(widget.item, uri.toString(), info: info, forImage: true);
|
||||
if (resolved == null) return const SizedBox.shrink();
|
||||
final image = resolved.startsWith('file:')
|
||||
? Image.file(
|
||||
File(Uri.parse(resolved).toFilePath()),
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (_, _, _) => const SizedBox.shrink(),
|
||||
)
|
||||
: Image.network(
|
||||
resolved,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (_, _, _) => const SizedBox.shrink(),
|
||||
);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: ClipRRect(borderRadius: BorderRadius.circular(6), child: image),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ExtensionStatus extends StatelessWidget {
|
||||
const _ExtensionStatus({required this.label, required this.emphasized});
|
||||
|
||||
final String label;
|
||||
final bool emphasized;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
final dotColor = emphasized ? palette.brand : palette.success;
|
||||
final textColor = emphasized ? palette.textPrimary : palette.success;
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 5,
|
||||
height: 5,
|
||||
decoration: BoxDecoration(color: dotColor, shape: BoxShape.circle),
|
||||
),
|
||||
const SizedBox(width: 5),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(color: textColor, fontSize: 11.5, fontWeight: FontWeight.w600),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LinkButtonContent extends StatelessWidget {
|
||||
const _LinkButtonContent({required this.icon, required this.label});
|
||||
|
||||
final IconData icon;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 15, color: palette.textMuted),
|
||||
const SizedBox(width: 7),
|
||||
Text(label),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ExtensionStats extends StatelessWidget {
|
||||
const _ExtensionStats({required this.store});
|
||||
|
||||
final StoreExtension store;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
key: const ValueKey('extension-details-stats'),
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_MetadataItem(icon: Icons.star_rounded, value: store.stars.toString()),
|
||||
const SizedBox(width: 12),
|
||||
_MetadataItem(icon: Icons.download_outlined, value: store.installCount.toString()),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MetadataItem extends StatelessWidget {
|
||||
const _MetadataItem({required this.icon, required this.value});
|
||||
|
||||
final IconData icon;
|
||||
final String value;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 14, color: palette.textMuted),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(color: palette.textSecondary, fontSize: 12, fontWeight: FontWeight.w600),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
MarkdownStyleSheet _markdownStyle(AppPalette palette, {required bool mobile}) {
|
||||
final paragraph = TextStyle(color: palette.textSecondary, fontSize: mobile ? 14 : 13.5, height: 1.62);
|
||||
final heading = TextStyle(color: palette.textPrimary, fontWeight: FontWeight.w800, height: 1.25);
|
||||
return MarkdownStyleSheet(
|
||||
a: paragraph.copyWith(color: palette.brand, decoration: TextDecoration.underline),
|
||||
p: paragraph,
|
||||
code: paragraph.copyWith(fontFamily: 'monospace', fontSize: 12, backgroundColor: palette.surfaceSoft),
|
||||
h1: heading.copyWith(fontSize: mobile ? 21 : 20),
|
||||
h2: heading.copyWith(fontSize: mobile ? 18 : 17),
|
||||
h3: heading.copyWith(fontSize: mobile ? 16 : 15),
|
||||
h4: heading.copyWith(fontSize: 14),
|
||||
h5: heading.copyWith(fontSize: 13),
|
||||
h6: heading.copyWith(fontSize: 12),
|
||||
em: paragraph.copyWith(fontStyle: FontStyle.italic),
|
||||
strong: paragraph.copyWith(fontWeight: FontWeight.w800, color: palette.textPrimary),
|
||||
blockSpacing: 12,
|
||||
listIndent: 22,
|
||||
listBullet: paragraph,
|
||||
blockquote: paragraph,
|
||||
blockquotePadding: const EdgeInsets.fromLTRB(12, 8, 10, 8),
|
||||
blockquoteDecoration: BoxDecoration(
|
||||
color: palette.surfaceSoft,
|
||||
border: Border(left: BorderSide(color: palette.brand, width: 3)),
|
||||
),
|
||||
codeblockPadding: const EdgeInsets.all(12),
|
||||
codeblockDecoration: BoxDecoration(
|
||||
color: palette.surfaceSoft,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: palette.border),
|
||||
),
|
||||
horizontalRuleDecoration: BoxDecoration(
|
||||
border: Border(top: BorderSide(color: palette.border)),
|
||||
),
|
||||
tableHead: paragraph.copyWith(color: palette.textPrimary, fontWeight: FontWeight.w800),
|
||||
tableBody: paragraph,
|
||||
tableBorder: TableBorder.all(color: palette.border),
|
||||
tableCellsPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 7),
|
||||
);
|
||||
}
|
||||
|
||||
Future<_ReadmeInfo> _loadReadme(ExtensionListItem item) async {
|
||||
final installed = item.installed;
|
||||
final remote = item.store?.readme ?? '';
|
||||
if (installed == null || Util.isWeb()) return _ReadmeInfo(content: remote);
|
||||
|
||||
try {
|
||||
final rootDirectory = installed.devMode
|
||||
? installed.devPath
|
||||
: path.join(Util.getStorageDir(), 'extensions', installed.identity);
|
||||
for (final name in const ['README.md', 'readme.md', 'README.MD']) {
|
||||
final filePath = path.join(rootDirectory, name);
|
||||
final file = File(filePath);
|
||||
if (file.existsSync()) {
|
||||
return _ReadmeInfo(content: file.readAsStringSync(), localPath: filePath);
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
// Fall back to the store copy when local storage is unavailable or unreadable.
|
||||
}
|
||||
return _ReadmeInfo(content: remote);
|
||||
}
|
||||
|
||||
String? _resolveReadmeUrl(ExtensionListItem item, String? raw, {required _ReadmeInfo info, required bool forImage}) {
|
||||
final store = item.store;
|
||||
final value = raw?.trim() ?? '';
|
||||
if (value.isEmpty) return null;
|
||||
final parsed = Uri.tryParse(value);
|
||||
if (parsed != null && parsed.hasScheme) return parsed.toString();
|
||||
|
||||
if (forImage && info.localPath != null) {
|
||||
final clean = value.split('#').first;
|
||||
return Uri.file(path.normalize(path.join(path.dirname(info.localPath!), clean))).toString();
|
||||
}
|
||||
if (store == null) return null;
|
||||
|
||||
final ref = store.commitSha?.isNotEmpty == true ? store.commitSha! : 'HEAD';
|
||||
final directory = (store.directory ?? '').split('/').where((segment) => segment.isNotEmpty).join('/');
|
||||
final suffix = directory.isEmpty ? '' : '$directory/';
|
||||
final base = forImage
|
||||
? Uri.https('raw.githubusercontent.com', '/${store.repoFullName}/$ref/$suffix')
|
||||
: Uri.https('github.com', '/${store.repoFullName}/blob/$ref/$suffix');
|
||||
return base.resolve(value).toString();
|
||||
}
|
||||
|
||||
class _ReadmeInfo {
|
||||
const _ReadmeInfo({required this.content, this.localPath});
|
||||
|
||||
final String content;
|
||||
final String? localPath;
|
||||
}
|
||||
|
||||
Future<void> _openUrl(String value) async {
|
||||
final uri = Uri.tryParse(value);
|
||||
if (uri == null) return;
|
||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import '../../../../api/model/extension.dart';
|
||||
import '../../../../util/util.dart';
|
||||
import '../../application/extensions_controller.dart';
|
||||
|
||||
enum ExtensionIconSourceKind { asset, file, network }
|
||||
|
||||
@immutable
|
||||
class ExtensionIconSource {
|
||||
const ExtensionIconSource(this.kind, this.location);
|
||||
|
||||
final ExtensionIconSourceKind kind;
|
||||
final String location;
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
ExtensionIconSource resolveExtensionIconSource(ExtensionListItem item, {bool? web, String? storageDirectory}) {
|
||||
const fallbackAsset = 'assets/extension/default_icon.png';
|
||||
final installed = item.installed;
|
||||
if (installed != null) {
|
||||
final icon = installed.icon.trim();
|
||||
if (icon.isEmpty) {
|
||||
return const ExtensionIconSource(ExtensionIconSourceKind.asset, fallbackAsset);
|
||||
}
|
||||
|
||||
if (web ?? kIsWeb) {
|
||||
return ExtensionIconSource(ExtensionIconSourceKind.network, _installedExtensionIconUrl(installed, icon));
|
||||
}
|
||||
|
||||
final rootDirectory = installed.devMode
|
||||
? installed.devPath
|
||||
: path.join(storageDirectory ?? Util.getStorageDir(), 'extensions', installed.identity);
|
||||
return ExtensionIconSource(ExtensionIconSourceKind.file, path.join(rootDirectory, icon));
|
||||
}
|
||||
|
||||
final remoteIcon = item.store?.icon?.trim() ?? '';
|
||||
if (remoteIcon.isNotEmpty) {
|
||||
return ExtensionIconSource(ExtensionIconSourceKind.network, remoteIcon);
|
||||
}
|
||||
return const ExtensionIconSource(ExtensionIconSourceKind.asset, fallbackAsset);
|
||||
}
|
||||
|
||||
String _installedExtensionIconUrl(Extension extension, String icon) {
|
||||
final iconSegments = icon
|
||||
.replaceAll('\\', '/')
|
||||
.split('/')
|
||||
.where((segment) => segment.isNotEmpty && segment != '.')
|
||||
.toList();
|
||||
return Uri(pathSegments: ['', 'fs', 'extensions', extension.identity, ...iconSegments]).toString();
|
||||
}
|
||||
|
||||
class ExtensionIcon extends StatelessWidget {
|
||||
const ExtensionIcon({
|
||||
super.key,
|
||||
required this.item,
|
||||
required this.size,
|
||||
required this.borderRadius,
|
||||
this.fallbackPadding = EdgeInsets.zero,
|
||||
this.fallbackBackgroundColor,
|
||||
this.fallbackFit = BoxFit.contain,
|
||||
});
|
||||
|
||||
final ExtensionListItem item;
|
||||
final double size;
|
||||
final BorderRadius borderRadius;
|
||||
final EdgeInsetsGeometry fallbackPadding;
|
||||
final Color? fallbackBackgroundColor;
|
||||
final BoxFit fallbackFit;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final fallback = Container(
|
||||
width: size,
|
||||
height: size,
|
||||
padding: fallbackPadding,
|
||||
color: fallbackBackgroundColor,
|
||||
child: Image.asset('assets/extension/default_icon.png', fit: fallbackFit),
|
||||
);
|
||||
final source = resolveExtensionIconSource(item);
|
||||
final image = switch (source.kind) {
|
||||
ExtensionIconSourceKind.asset => fallback,
|
||||
ExtensionIconSourceKind.file => Image.file(
|
||||
File(source.location),
|
||||
width: size,
|
||||
height: size,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, _, _) => fallback,
|
||||
),
|
||||
ExtensionIconSourceKind.network => Image.network(
|
||||
source.location,
|
||||
width: size,
|
||||
height: size,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, _, _) => fallback,
|
||||
),
|
||||
};
|
||||
return ClipRRect(borderRadius: borderRadius, child: image);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user