✨ Existing friends management
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/utils/service_url.dart';
|
||||
import 'package:solian/widgets/common_wrapper.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class AccountScreen extends StatefulWidget {
|
||||
const AccountScreen({super.key});
|
||||
@ -40,21 +40,25 @@ class _AccountScreenState extends State<AccountScreen> {
|
||||
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 24),
|
||||
child: NameCard(),
|
||||
),
|
||||
InkWell(
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 18),
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.logout),
|
||||
title: Text("Sign out"),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 34),
|
||||
leading: const Icon(Icons.diversity_1),
|
||||
title: Text(AppLocalizations.of(context)!.friend),
|
||||
onTap: () {
|
||||
router.goNamed('account.friend');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 34),
|
||||
leading: const Icon(Icons.logout),
|
||||
title: Text(AppLocalizations.of(context)!.signOut),
|
||||
onTap: () {
|
||||
auth.signoff();
|
||||
setState(() {
|
||||
isAuthorized = false;
|
||||
});
|
||||
},
|
||||
)
|
||||
),
|
||||
],
|
||||
)
|
||||
: Center(
|
||||
@ -126,8 +130,7 @@ class NameCard extends StatelessWidget {
|
||||
children: [
|
||||
FutureBuilder(
|
||||
future: renderAvatar(context),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<Widget> snapshot) {
|
||||
builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
} else {
|
||||
@ -138,8 +141,7 @@ class NameCard extends StatelessWidget {
|
||||
const SizedBox(width: 20),
|
||||
FutureBuilder(
|
||||
future: renderLabel(context),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<Column> snapshot) {
|
||||
builder: (BuildContext context, AsyncSnapshot<Column> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
} else {
|
||||
@ -161,12 +163,7 @@ class ActionCard extends StatelessWidget {
|
||||
final String caption;
|
||||
final Function onTap;
|
||||
|
||||
const ActionCard(
|
||||
{super.key,
|
||||
required this.onTap,
|
||||
required this.title,
|
||||
required this.caption,
|
||||
required this.icon});
|
||||
const ActionCard({super.key, required this.onTap, required this.title, required this.caption, required this.icon});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
218
lib/screens/account/friend.dart
Normal file
218
lib/screens/account/friend.dart
Normal file
@ -0,0 +1,218 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/models/account.dart';
|
||||
import 'package:solian/models/friendship.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/utils/service_url.dart';
|
||||
import 'package:solian/widgets/indent_wrapper.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class FriendScreen extends StatefulWidget {
|
||||
const FriendScreen({super.key});
|
||||
|
||||
@override
|
||||
State<FriendScreen> createState() => _FriendScreenState();
|
||||
}
|
||||
|
||||
class _FriendScreenState extends State<FriendScreen> {
|
||||
bool _isSubmitting = false;
|
||||
|
||||
int _currentSideId = 0;
|
||||
List<Friendship> _friendships = List.empty();
|
||||
|
||||
Future<void> fetchFriendships() async {
|
||||
final auth = context.read<AuthProvider>();
|
||||
final prof = await auth.getProfiles();
|
||||
if (!await auth.isAuthorized()) return;
|
||||
|
||||
_currentSideId = prof['id'];
|
||||
|
||||
var uri = getRequestUri('passport', '/api/users/me/friends');
|
||||
|
||||
var res = await auth.client!.get(uri);
|
||||
if (res.statusCode == 200) {
|
||||
final result = jsonDecode(utf8.decode(res.bodyBytes)) as List<dynamic>;
|
||||
setState(() {
|
||||
_friendships = result.map((x) => Friendship.fromJson(x)).toList();
|
||||
});
|
||||
} else {
|
||||
var message = utf8.decode(res.bodyBytes);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Something went wrong... $message")),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateFriendship(Friendship relation, int status) async {
|
||||
setState(() => _isSubmitting = true);
|
||||
|
||||
final otherside = getOtherside(relation);
|
||||
|
||||
final auth = context.read<AuthProvider>();
|
||||
if (!await auth.isAuthorized()) {
|
||||
setState(() => _isSubmitting = false);
|
||||
return;
|
||||
}
|
||||
|
||||
var res = await auth.client!.put(
|
||||
getRequestUri('passport', '/api/users/me/friends/${otherside.id}'),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: jsonEncode(<String, dynamic>{
|
||||
'status': status,
|
||||
}),
|
||||
);
|
||||
if (res.statusCode == 200) {
|
||||
await fetchFriendships();
|
||||
} else {
|
||||
var message = utf8.decode(res.bodyBytes);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Something went wrong... $message")),
|
||||
);
|
||||
}
|
||||
|
||||
setState(() => _isSubmitting = false);
|
||||
}
|
||||
|
||||
List<Friendship> filterWithStatus(int status) {
|
||||
return _friendships.where((x) => x.status == status).toList();
|
||||
}
|
||||
|
||||
DismissDirection getDismissDirection(Friendship relation) {
|
||||
if (relation.status == 2) return DismissDirection.endToStart;
|
||||
if (relation.status == 1) return DismissDirection.startToEnd;
|
||||
if (relation.status == 0 && relation.relatedId != _currentSideId) return DismissDirection.startToEnd;
|
||||
return DismissDirection.horizontal;
|
||||
}
|
||||
|
||||
Account getOtherside(Friendship relation) {
|
||||
if (relation.accountId != _currentSideId) {
|
||||
return relation.account;
|
||||
} else {
|
||||
return relation.related;
|
||||
}
|
||||
}
|
||||
|
||||
String getAvatarUrl(String uuid) {
|
||||
return getRequestUri('passport', '/api/avatar/$uuid').toString();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
Future.delayed(Duration.zero, () {
|
||||
fetchFriendships();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget friendshipTileBuilder(context, index, status) {
|
||||
final element = filterWithStatus(status)[index];
|
||||
final otherside = getOtherside(element);
|
||||
|
||||
final randomId = DateTime.now().microsecondsSinceEpoch >> 10;
|
||||
|
||||
return Dismissible(
|
||||
key: Key(randomId.toString()),
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: const Icon(Icons.close, color: Colors.white),
|
||||
),
|
||||
secondaryBackground: Container(
|
||||
color: Colors.green,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
alignment: Alignment.centerRight,
|
||||
child: const Icon(Icons.check, color: Colors.white),
|
||||
),
|
||||
direction: getDismissDirection(element),
|
||||
child: ListTile(
|
||||
title: Text(otherside.nick),
|
||||
subtitle: Text(otherside.name),
|
||||
leading: CircleAvatar(
|
||||
backgroundImage: NetworkImage(getAvatarUrl(otherside.avatar)),
|
||||
),
|
||||
),
|
||||
onDismissed: (direction) {
|
||||
if (direction == DismissDirection.startToEnd) {
|
||||
updateFriendship(element, 2);
|
||||
}
|
||||
if (direction == DismissDirection.endToStart) {
|
||||
updateFriendship(element, 1);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return IndentWrapper(
|
||||
title: AppLocalizations.of(context)!.friend,
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () => fetchFriendships(),
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: _isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.8),
|
||||
child: Text(AppLocalizations.of(context)!.friendPending),
|
||||
),
|
||||
),
|
||||
SliverList.builder(
|
||||
itemCount: filterWithStatus(0).length,
|
||||
itemBuilder: (_, __) => friendshipTileBuilder(_, __, 0),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.8),
|
||||
child: Text(AppLocalizations.of(context)!.friendActive),
|
||||
),
|
||||
),
|
||||
SliverList.builder(
|
||||
itemCount: filterWithStatus(1).length,
|
||||
itemBuilder: (_, __) => friendshipTileBuilder(_, __, 1),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.8),
|
||||
child: Text(AppLocalizations.of(context)!.friendBlocked),
|
||||
),
|
||||
),
|
||||
SliverList.builder(
|
||||
itemCount: filterWithStatus(2).length,
|
||||
itemBuilder: (_, __) => friendshipTileBuilder(_, __, 2),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(
|
||||
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.8),
|
||||
width: 0.3,
|
||||
)),
|
||||
),
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text(
|
||||
AppLocalizations.of(context)!.friendListHint,
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -83,26 +83,21 @@ class _ExploreScreenState extends State<ExploreScreen> {
|
||||
onRefresh: () => Future.sync(
|
||||
() => _pagingController.refresh(),
|
||||
),
|
||||
child: Center(
|
||||
child: Container(
|
||||
constraints: const BoxConstraints(maxWidth: 640),
|
||||
child: PagedListView<int, Post>(
|
||||
pagingController: _pagingController,
|
||||
builderDelegate: PagedChildBuilderDelegate<Post>(
|
||||
itemBuilder: (context, item, index) => PostItem(
|
||||
item: item,
|
||||
onUpdate: () => _pagingController.refresh(),
|
||||
onTap: () {
|
||||
router.pushNamed(
|
||||
'posts.screen',
|
||||
pathParameters: {
|
||||
'alias': item.alias,
|
||||
'dataset': '${item.modelType}s',
|
||||
},
|
||||
);
|
||||
child: PagedListView<int, Post>(
|
||||
pagingController: _pagingController,
|
||||
builderDelegate: PagedChildBuilderDelegate<Post>(
|
||||
itemBuilder: (context, item, index) => PostItem(
|
||||
item: item,
|
||||
onUpdate: () => _pagingController.refresh(),
|
||||
onTap: () {
|
||||
router.pushNamed(
|
||||
'posts.screen',
|
||||
pathParameters: {
|
||||
'alias': item.alias,
|
||||
'dataset': '${item.modelType}s',
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -49,7 +49,7 @@ class _NotificationScreenState extends State<NotificationScreen> {
|
||||
index: index,
|
||||
item: element,
|
||||
onDismiss: () => setState(() {
|
||||
nty.notifications.removeAt(index);
|
||||
nty.clearAt(index);
|
||||
}),
|
||||
);
|
||||
},
|
||||
|
@ -23,12 +23,10 @@ class PostScreen extends StatefulWidget {
|
||||
class _PostScreenState extends State<PostScreen> {
|
||||
final _client = http.Client();
|
||||
|
||||
final PagingController<int, Post> _commentPagingController =
|
||||
PagingController(firstPageKey: 0);
|
||||
final PagingController<int, Post> _commentPagingController = PagingController(firstPageKey: 0);
|
||||
|
||||
Future<Post?> fetchPost(BuildContext context) async {
|
||||
final uri = getRequestUri(
|
||||
'interactive', '/api/p/${widget.dataset}/${widget.alias}');
|
||||
final uri = getRequestUri('interactive', '/api/p/${widget.dataset}/${widget.alias}');
|
||||
final res = await _client.get(uri);
|
||||
if (res.statusCode != 200) {
|
||||
final err = utf8.decode(res.bodyBytes);
|
||||
@ -51,32 +49,27 @@ class _PostScreenState extends State<PostScreen> {
|
||||
future: fetchPost(context),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData && snapshot.data != null) {
|
||||
return Center(
|
||||
child: Container(
|
||||
constraints: const BoxConstraints(maxWidth: 640),
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: PostItem(
|
||||
item: snapshot.data!,
|
||||
brief: false,
|
||||
ripple: false,
|
||||
),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: CommentListHeader(
|
||||
related: snapshot.data!,
|
||||
paging: _commentPagingController,
|
||||
),
|
||||
),
|
||||
CommentList(
|
||||
related: snapshot.data!,
|
||||
dataset: widget.dataset,
|
||||
paging: _commentPagingController,
|
||||
),
|
||||
],
|
||||
return CustomScrollView(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: PostItem(
|
||||
item: snapshot.data!,
|
||||
brief: false,
|
||||
ripple: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: CommentListHeader(
|
||||
related: snapshot.data!,
|
||||
paging: _commentPagingController,
|
||||
),
|
||||
),
|
||||
CommentList(
|
||||
related: snapshot.data!,
|
||||
dataset: widget.dataset,
|
||||
paging: _commentPagingController,
|
||||
),
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return const Center(
|
||||
|
Reference in New Issue
Block a user