✨ Subscriptions
This commit is contained in:
@ -1,11 +1,16 @@
|
||||
import 'package:auto_route/annotations.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/post.dart';
|
||||
import 'package:island/models/user.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
import 'package:island/widgets/account/badge.dart';
|
||||
import 'package:island/widgets/account/status.dart';
|
||||
import 'package:island/widgets/alert.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:island/widgets/content/cloud_files.dart';
|
||||
import 'package:island/widgets/post/post_list.dart';
|
||||
@ -21,6 +26,27 @@ Future<SnPublisher> publisher(Ref ref, String uname) async {
|
||||
return SnPublisher.fromJson(resp.data);
|
||||
}
|
||||
|
||||
@riverpod
|
||||
Future<List<SnAccountBadge>> publisherBadges(Ref ref, String pubName) async {
|
||||
final pub = await ref.watch(publisherProvider(pubName).future);
|
||||
if (pub.publisherType != 0) return [];
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
final resp = await apiClient.get("/accounts/${pub.name}/badges");
|
||||
return List<SnAccountBadge>.from(
|
||||
resp.data.map((x) => SnAccountBadge.fromJson(x)),
|
||||
);
|
||||
}
|
||||
|
||||
@riverpod
|
||||
Future<SnSubscriptionStatus> publisherSubscriptionStatus(
|
||||
Ref ref,
|
||||
String pubName,
|
||||
) async {
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
final resp = await apiClient.get("/publishers/$pubName/subscription");
|
||||
return SnSubscriptionStatus.fromJson(resp.data);
|
||||
}
|
||||
|
||||
@RoutePage()
|
||||
class PublisherProfileScreen extends HookConsumerWidget {
|
||||
final String name;
|
||||
@ -32,6 +58,38 @@ class PublisherProfileScreen extends HookConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final publisher = ref.watch(publisherProvider(name));
|
||||
final badges = ref.watch(publisherBadgesProvider(name));
|
||||
final subStatus = ref.watch(publisherSubscriptionStatusProvider(name));
|
||||
|
||||
final subscribing = useState(false);
|
||||
|
||||
Future<void> subscribe() async {
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
subscribing.value = true;
|
||||
try {
|
||||
await apiClient.post("/publishers/$name/subscribe", data: {'tier': 0});
|
||||
ref.invalidate(publisherSubscriptionStatusProvider(name));
|
||||
HapticFeedback.heavyImpact();
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
} finally {
|
||||
subscribing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> unsubscribe() async {
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
subscribing.value = true;
|
||||
try {
|
||||
await apiClient.post("/publishers/$name/unsubscribe");
|
||||
ref.invalidate(publisherSubscriptionStatusProvider(name));
|
||||
HapticFeedback.heavyImpact();
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
} finally {
|
||||
subscribing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
final iconShadow = Shadow(
|
||||
color: Colors.black54,
|
||||
@ -64,6 +122,41 @@ class PublisherProfileScreen extends HookConsumerWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
subStatus.when(
|
||||
data:
|
||||
(status) => IconButton(
|
||||
onPressed:
|
||||
subscribing.value
|
||||
? null
|
||||
: (status.isSubscribed
|
||||
? unsubscribe
|
||||
: subscribe),
|
||||
icon: Icon(
|
||||
status.isSubscribed
|
||||
? Icons.remove_circle
|
||||
: Icons.add_circle,
|
||||
shadows: [iconShadow],
|
||||
),
|
||||
),
|
||||
error: (_, __) => const SizedBox(),
|
||||
loading:
|
||||
() => const SizedBox(
|
||||
width: 48,
|
||||
height: 48,
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Gap(8),
|
||||
],
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Row(
|
||||
@ -95,14 +188,14 @@ class PublisherProfileScreen extends HookConsumerWidget {
|
||||
],
|
||||
).padding(horizontal: 24, top: 24, bottom: 24),
|
||||
),
|
||||
// if (data.badges.isNotEmpty)
|
||||
// SliverToBoxAdapter(
|
||||
// child: BadgeList(
|
||||
// badges: data.badges,
|
||||
// ).padding(horizontal: 24, bottom: 24),
|
||||
// )
|
||||
// else
|
||||
// const Gap(16),
|
||||
if (badges.value?.isNotEmpty ?? false)
|
||||
SliverToBoxAdapter(
|
||||
child: BadgeList(
|
||||
badges: badges.value!,
|
||||
).padding(horizontal: 24, bottom: 24),
|
||||
)
|
||||
else
|
||||
const SliverGap(16),
|
||||
SliverToBoxAdapter(child: const Divider(height: 1)),
|
||||
if (data.bio.isNotEmpty)
|
||||
SliverToBoxAdapter(
|
||||
|
Reference in New Issue
Block a user