Skip to content

Commit

Permalink
MySnapsPage: correctly build when snapchange done (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier committed Jun 16, 2022
1 parent e356cca commit 7801c3f
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 176 deletions.
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
"size": "Size",
"name": "Name",
"sortBy": "Sort by",
"media": "Media"
"media": "Media",
"done": "Done"
}
6 changes: 3 additions & 3 deletions lib/services/app_change_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AppChangeService {
final SnapdClient _snapDClient;
final NotificationsClient _notificationsClient;

Future<void> addChange(Snap snap, String id) async {
Future<void> addChange(Snap snap, String id, String doneString) async {
final newChange = await _snapDClient.getChange(id);
_snapChanges.putIfAbsent(snap, () => newChange);
if (!_snapChangesController.isClosed) {
Expand All @@ -22,12 +22,12 @@ class AppChangeService {
removeChange(snap);
_notificationsClient.notify(
'Software',
body: newChange.summary,
body: '$doneString: ${newChange.summary}',
appName: snap.name,
appIcon: 'snap-store',
hints: [
NotificationHint.desktopEntry('software'),
NotificationHint.urgency(NotificationUrgency.critical)
NotificationHint.urgency(NotificationUrgency.normal)
],
);
break;
Expand Down
4 changes: 3 additions & 1 deletion lib/store_app/common/snap_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:snapd/snapd.dart';
import 'package:software/l10n/l10n.dart';
import 'package:software/services/app_change_service.dart';
import 'package:software/store_app/common/constants.dart';
import 'package:software/store_app/common/snap_channel_expandable.dart';
Expand All @@ -23,7 +24,8 @@ class SnapDialog extends StatefulWidget {
required String huskSnapName,
}) =>
ChangeNotifierProvider<SnapModel>(
create: (context) => SnapModel(
create: (_) => SnapModel(
doneString: context.l10n.done,
getService<SnapdClient>(),
getService<AppChangeService>(),
huskSnapName: huskSnapName,
Expand Down
77 changes: 13 additions & 64 deletions lib/store_app/common/snap_model.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:intl/intl.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';
import 'package:snapd/snapd.dart';
import 'package:software/services/app_change_service.dart';
import 'package:software/services/color_generator.dart';
import 'package:software/snapx.dart';
import 'package:xdg_icons/xdg_icons.dart';
import 'package:yaru_icons/yaru_icons.dart';

class SnapModel extends SafeChangeNotifier {
final AppChangeService _appChangeService;
Expand All @@ -21,10 +17,12 @@ class SnapModel extends SafeChangeNotifier {
Snap? _storeSnap;
Snap? _localSnap;
bool online;
final String doneString;

SnapModel(
this._client,
this._appChangeService, {
required this.doneString,
this.colorGenerator,
required this.huskSnapName,
this.online = true,
Expand Down Expand Up @@ -245,6 +243,7 @@ class SnapModel extends SafeChangeNotifier {
await _appChangeService.addChange(
_storeSnap!,
changeId,
doneString,
);
_localSnap = await _findLocalSnap(huskSnapName);
notifyListeners();
Expand All @@ -254,7 +253,11 @@ class SnapModel extends SafeChangeNotifier {
if (name == null) return;
await _client.loadAuthorization();
final changeId = await _client.remove(name!);
await _appChangeService.addChange(_localSnap!, changeId);
await _appChangeService.addChange(
_localSnap!,
changeId,
doneString,
);
_localSnap = await _findLocalSnap(huskSnapName);
notifyListeners();
}
Expand All @@ -264,7 +267,11 @@ class SnapModel extends SafeChangeNotifier {
await _client.loadAuthorization();
final changeId =
await _client.refresh(name!, channel: channelToBeInstalled);
await _appChangeService.addChange(_localSnap!, changeId);
await _appChangeService.addChange(
_localSnap!,
changeId,
doneString,
);
notifyListeners();
}

Expand Down Expand Up @@ -342,62 +349,4 @@ class SnapModel extends SafeChangeNotifier {
mode: ProcessStartMode.detached,
);
}

String? get _desktopFile =>
apps != null && apps!.isNotEmpty && apps!.first.desktopFile != null
? apps!.first.desktopFile!
: null;

Widget offlineIcon = fallbackSnapIcon;
String _iconLine = '';

void loadOfflineIcon() {
if (_desktopFile != null) {
File? file = File(_desktopFile!);
(file
.openRead()
.map(utf8.decode)
.transform(const LineSplitter())
.where((line) => line.contains('Icon='))
.first)
.then((line) {
_iconLine = line.replaceAll('Icon=', '');
if (_iconLine.endsWith('.png') ||
_iconLine.endsWith('.jpg') ||
_iconLine.endsWith('.jpeg')) {
offlineIcon = Image.file(
File(_iconLine),
filterQuality: FilterQuality.medium,
width: 50,
);
}
if (_iconLine.endsWith('.svg')) {
try {
offlineIcon = SvgPicture.file(
File(_iconLine),
width: 50,
);
} finally {
if (offlineIcon != fallbackSnapIcon) {
offlineIcon = fallbackSnapIcon;
}
}
}
if (!_iconLine.contains('/')) {
offlineIcon = XdgIconTheme(
data: const XdgIconThemeData(theme: 'Yaru'),
child: XdgIcon(name: _iconLine, size: 48),
);
}
notifyListeners();
return;
});
}
notifyListeners();
}
}

const fallbackSnapIcon = Icon(
YaruIcons.package_snap,
size: 50,
);
15 changes: 14 additions & 1 deletion lib/store_app/explore/section_banner_grid.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:software/snapx.dart';
import 'package:software/store_app/common/snap_dialog.dart';
import 'package:software/store_app/common/snap_section.dart';
import 'package:software/store_app/explore/explore_model.dart';
import 'package:software/store_app/explore/snap_banner.dart';
Expand Down Expand Up @@ -43,7 +45,18 @@ class _SectionBannerGridState extends State<SectionBannerGrid> {
),
children: sections != null && sections.isNotEmpty
? sections.take(widget.amount).map((snap) {
return SnapBanner.create(context, snap);
return SnapBanner(
name: snap.name,
summary: snap.summary,
url: snap.iconUrl,
onTap: () => showDialog(
context: context,
builder: (context) => SnapDialog.create(
context: context,
huskSnapName: snap.name,
),
),
);
}).toList()
: [],
);
Expand Down
53 changes: 14 additions & 39 deletions lib/store_app/explore/snap_banner.dart
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:snapd/snapd.dart';
import 'package:software/services/app_change_service.dart';
import 'package:software/snapx.dart';
import 'package:software/store_app/common/safe_image.dart';
import 'package:software/store_app/common/snap_dialog.dart';
import 'package:software/store_app/common/app_banner.dart';

import 'package:software/store_app/common/snap_model.dart';
import 'package:ubuntu_service/ubuntu_service.dart';
import 'package:software/store_app/common/safe_image.dart';
import 'package:yaru_colors/yaru_colors.dart';
import 'package:yaru_icons/yaru_icons.dart';

class SnapBanner extends StatelessWidget {
const SnapBanner({
Key? key,
required this.snap,
this.onTap,
this.surfaceTintColor,
this.watermark = false,
required this.name,
required this.summary,
this.url,
}) : super(key: key);

final Snap snap;
final String name;
final String summary;
final String? url;
final Function()? onTap;
final Color? surfaceTintColor;
final bool watermark;

static Widget create(BuildContext context, Snap snap) {
final snapModel = SnapModel(
getService<SnapdClient>(),
getService<AppChangeService>(),
huskSnapName: snap.name,
);
return ChangeNotifierProvider<SnapModel>(
create: (context) => snapModel,
child: SnapBanner(
snap: snap,
onTap: () => showDialog(
context: context,
builder: (context) => ChangeNotifierProvider.value(
value: snapModel,
child: const SnapDialog(),
),
),
),
);
}

@override
Widget build(BuildContext context) {
final borderRadius = BorderRadius.circular(10);
Expand All @@ -61,11 +36,11 @@ class SnapBanner extends StatelessWidget {
AppBanner(
borderRadius: borderRadius,
color: surfaceTintColor!,
title: snap.title ?? '',
summary: snap.summary,
title: name,
summary: summary,
elevation: light ? 4 : 6,
icon: SafeImage(
url: snap.iconUrl,
url: url,
fallBackIconData: YaruIcons.package_snap,
),
textOverflow: TextOverflow.fade,
Expand All @@ -80,7 +55,7 @@ class SnapBanner extends StatelessWidget {
child: SizedBox(
height: 130,
child: SafeImage(
url: snap.iconUrl,
url: url,
fallBackIconData: YaruIcons.package_snap,
),
),
Expand All @@ -96,11 +71,11 @@ class SnapBanner extends StatelessWidget {
: Theme.of(context).colorScheme.onBackground,
elevation: light ? 2 : 1,
icon: SafeImage(
url: snap.iconUrl,
url: url,
fallBackIconData: YaruIcons.package_snap,
),
title: snap.title ?? '',
summary: snap.summary,
title: name,
summary: summary,
textOverflow: TextOverflow.ellipsis,
),
);
Expand Down
13 changes: 10 additions & 3 deletions lib/store_app/explore/snap_banner_carousel.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:snapd/snapd.dart';
import 'package:software/l10n/l10n.dart';
import 'package:software/services/app_change_service.dart';
import 'package:software/snapx.dart';
import 'package:software/store_app/common/snap_model.dart';
import 'package:software/store_app/common/snap_section.dart';
import 'package:software/store_app/explore/explore_model.dart';
Expand Down Expand Up @@ -77,6 +79,7 @@ class _AppBannerCarouselItem extends StatefulWidget {
getService<AppChangeService>(),
huskSnapName: snap.name,
colorGenerator: getService<ColorGenerator>(),
doneString: context.l10n.done,
),
child: _AppBannerCarouselItem(snap: snap),
);
Expand All @@ -98,12 +101,16 @@ class _AppBannerCarouselItemState extends State<_AppBannerCarouselItem> {
final model = context.watch<SnapModel>();
return SnapBanner(
watermark: true,
snap: widget.snap,
name: widget.snap.name,
summary: widget.snap.summary,
url: widget.snap.iconUrl,
surfaceTintColor: model.surfaceTintColor,
onTap: () => showDialog(
context: context,
builder: (context) =>
SnapDialog.create(context: context, huskSnapName: widget.snap.name),
builder: (context) => ChangeNotifierProvider.value(
value: model,
child: const SnapDialog(),
),
),
);
}
Expand Down
Loading

0 comments on commit 7801c3f

Please sign in to comment.