Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bruig: Add subcribe button to feed content #382

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions bruig/flutterui/bruig/lib/components/chat/active_chat.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:bruig/components/manage_gc.dart';
import 'package:bruig/models/menus.dart';
import 'package:bruig/util.dart';
import 'package:bruig/models/client.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -121,9 +122,11 @@ class _ActiveChatState extends State<ActiveChat> {
: darkTextColor;

bool isScreenSmall = MediaQuery.of(context).size.width <= 500;
List<ChatMenuItem> activeSubMenu =
client.activeSubMenu.whereType<ChatMenuItem>().toList();
return Consumer<ThemeNotifier>(
builder: (context, theme, _) => isScreenSmall
? client.activeSubMenu.isNotEmpty
? activeSubMenu.isNotEmpty
? Stack(
alignment: Alignment.topRight,
children: [
Expand Down Expand Up @@ -154,13 +157,13 @@ class _ActiveChatState extends State<ActiveChat> {
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: client.activeSubMenu.length,
itemCount: activeSubMenu.length,
itemBuilder: (context, index) => ListTile(
title: Text(client.activeSubMenu[index].label,
title: Text(activeSubMenu[index].label,
style: TextStyle(
fontSize: theme.getSmallFont(context))),
onTap: () {
client.activeSubMenu[index]
activeSubMenu[index]
.onSelected(context, client);
client.hideSubMenu();
},
Expand Down Expand Up @@ -191,7 +194,7 @@ class _ActiveChatState extends State<ActiveChat> {
_itemScrollController, _itemPositionsListener),
),
Container(
margin: EdgeInsets.all(10),
margin: const EdgeInsets.all(10),
child: Input(sendMsg, chat, inputFocusNode))
])
: Row(children: [
Expand All @@ -202,12 +205,12 @@ class _ActiveChatState extends State<ActiveChat> {
_itemScrollController, _itemPositionsListener),
),
Container(
padding: EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
child: Input(sendMsg, chat, inputFocusNode))
]),
),
Visibility(
visible: client.activeSubMenu.isNotEmpty,
visible: activeSubMenu.isNotEmpty,
child: Container(
width: 250,
decoration: BoxDecoration(
Expand Down Expand Up @@ -243,13 +246,13 @@ class _ActiveChatState extends State<ActiveChat> {
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: client.activeSubMenu.length,
itemCount: activeSubMenu.length,
itemBuilder: (context, index) => ListTile(
title: Text(client.activeSubMenu[index].label,
title: Text(activeSubMenu[index].label,
style: TextStyle(
fontSize: theme.getSmallFont(context))),
onTap: () {
client.activeSubMenu[index]
activeSubMenu[index]
.onSelected(context, client);
client.hideSubMenu();
},
Expand Down
1 change: 1 addition & 0 deletions bruig/flutterui/bruig/lib/components/chat/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ class _PostSubscriptionEventWState extends State<PostSubscriptionEventW> {
setState(() {
var chat = client.getExistingChat(event.id);
chat!.isSubscribed = true;
chat.isSubscribing = false;
client.updateUserMenu(event.id, buildUserChatMenu(chat));
});
msg = "Subscribed to user's posts!";
Expand Down
7 changes: 4 additions & 3 deletions bruig/flutterui/bruig/lib/components/context_menu.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';

typedef PopMenuList = List<PopupMenuItem>;
typedef PopMenuList = List<PopupMenuItem?>;

class ContextMenu extends StatefulWidget {
const ContextMenu(
Expand All @@ -25,7 +25,8 @@ class _ContextMenuState extends State<ContextMenu> {
final RenderObject overlay =
Overlay.of(context).context.findRenderObject() as RenderBox;
Offset offs = const Offset(10, 30);

final List<PopupMenuItem> items =
widget.items.whereType<PopupMenuItem>().toList();
final result = await showMenu(
context: context,
position: RelativeRect.fromRect(
Expand All @@ -37,7 +38,7 @@ class _ContextMenuState extends State<ContextMenu> {
),
Offset.zero & overlay.paintBounds.size,
),
items: widget.items,
items: items,
);

widget.handleItemTap(result);
Expand Down
2 changes: 1 addition & 1 deletion bruig/flutterui/bruig/lib/components/feed_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _FeedBarState extends State<FeedBar> {
child: ListView(
children: [
ListTile(
title: Text("News Feed",
title: Text("Feed",
style: TextStyle(
color: selectedIndex == 0
? selectedTextColor
Expand Down
3 changes: 1 addition & 2 deletions bruig/flutterui/bruig/lib/components/sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ class _SidebarState extends State<Sidebar> {
.map((e) => SidebarXItem(
label: e.label,
iconWidget: (e.label == "Chats" && client.hasUnreadChats) ||
(e.label == "News Feed" &&
feed.hasUnreadPostsComments)
(e.label == "Feed" && feed.hasUnreadPostsComments)
? Stack(children: [
Container(
padding: const EdgeInsets.all(3),
Expand Down
38 changes: 27 additions & 11 deletions bruig/flutterui/bruig/lib/components/user_context_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class UserContextMenu extends StatelessWidget {
case 'subscribe':
targetUserChat!.subscribeToPosts();
break;
case 'unsubscribe':
targetUserChat!.unsubscribeToPosts();
break;
case 'rename':
showRenameModalBottom(context, targetUserChat!);
break;
Expand All @@ -47,25 +50,38 @@ class UserContextMenu extends StatelessWidget {
};
}

PopMenuList _buildUserMenu() {
return const [
PopupMenuItem(
PopMenuList _buildUserMenu(ChatModel? chat) {
bool isSubscribed = false;
bool isSubscribing = false;
if (chat != null) {
isSubscribed = chat.isSubscribed;
isSubscribing = chat.isSubscribing;
}
return [
const PopupMenuItem(
value: 'tip',
child: Text('Pay Tip'),
),
PopupMenuItem(
const PopupMenuItem(
value: 'reqRatchetReset',
child: Text('Request Ratchet Reset'),
),
PopupMenuItem(
value: 'subscribe',
child: Text('Subscribe to Posts'),
),
PopupMenuItem(
isSubscribed
? const PopupMenuItem(
value: 'unsubscribe',
child: Text('Unsubscribe to Posts'),
)
: !isSubscribing
? const PopupMenuItem(
value: 'subscribe',
child: Text('Subscribe to Posts'),
)
: null,
const PopupMenuItem(
value: 'rename',
child: Text('Rename User'),
),
PopupMenuItem(
const PopupMenuItem(
value: 'suggestToKX',
child: Text('Suggest User to KX'),
),
Expand Down Expand Up @@ -104,7 +120,7 @@ class UserContextMenu extends StatelessWidget {
return ContextMenu(
disabled: disabled,
handleItemTap: _handleItemTap(context),
items: _buildUserMenu(),
items: _buildUserMenu(targetUserChat),
child: child,
);
}
Expand Down
22 changes: 15 additions & 7 deletions bruig/flutterui/bruig/lib/models/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,25 @@ class ChatModel extends ChangeNotifier {
}

String workingMsg = "";
bool _isSubscribing = false;
bool get isSubscribing => _isSubscribing;
set isSubscribing(bool b) {
_isSubscribing = b;
notifyListeners();
}

void subscribeToPosts() {
var event = SynthChatEvent("Subscribing to user's posts");
event.state = SCE_sending;
isSubscribing = true;
append(ChatEventModel(event, null), false);
(() async {
try {
await Golib.subscribeToPosts(id);
event.state = SCE_sent;
} catch (error) {
event.error = Exception(error);
isSubscribing = false;
}
})();
}
Expand Down Expand Up @@ -585,20 +593,20 @@ class ClientModel extends ChangeNotifier {
UnmodifiableMapView<String, List<ChatMenuItem>> get subGCMenus =>
UnmodifiableMapView(_subGCMenus);

final Map<String, List<ChatMenuItem>> _subUserMenus = {};
UnmodifiableMapView<String, List<ChatMenuItem>> get subUserMenus =>
final Map<String, List<ChatMenuItem?>> _subUserMenus = {};
UnmodifiableMapView<String, List<ChatMenuItem?>> get subUserMenus =>
UnmodifiableMapView(_subUserMenus);

List<ChatMenuItem> _activeSubMenu = [];
UnmodifiableListView<ChatMenuItem> get activeSubMenu =>
List<ChatMenuItem?> _activeSubMenu = [];
UnmodifiableListView<ChatMenuItem?> get activeSubMenu =>
UnmodifiableListView(_activeSubMenu);

void updateUserMenu(String id, List<ChatMenuItem> menu) {
void updateUserMenu(String id, List<ChatMenuItem?> menu) {
_subUserMenus[id] = menu;
//notifyListeners();
notifyListeners();
}

void set activeSubMenu(List<ChatMenuItem> sm) {
void set activeSubMenu(List<ChatMenuItem?> sm) {
_activeSubMenu = sm;
notifyListeners();
}
Expand Down
39 changes: 27 additions & 12 deletions bruig/flutterui/bruig/lib/models/menus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SubMenuInfo {
}

final List<SubMenuInfo> FeedScreenSub = [
SubMenuInfo(0, "News Feed"),
SubMenuInfo(0, "Feed"),
SubMenuInfo(1, "Your Posts"),
SubMenuInfo(2, "Subscriptions"),
SubMenuInfo(3, "New Post")
Expand All @@ -67,7 +67,7 @@ final List<SubMenuInfo> LnScreenSub = [

final List<MainMenuItem> mainMenu = [
MainMenuItem(
"News Feed",
"Feed",
FeedScreen.routeName,
(context) => Consumer<MainMenuModel>(
builder: (context, menu, child) => FeedScreen(menu)),
Expand Down Expand Up @@ -177,7 +177,7 @@ class ChatMenuItem {
const ChatMenuItem(this.label, this.onSelected);
}

List<ChatMenuItem> buildUserChatMenu(ChatModel chat) {
List<ChatMenuItem?> buildUserChatMenu(ChatModel chat) {
void sendFile(BuildContext context, ChatModel chat) async {
var filePickRes = await FilePicker.platform.pickFiles();
if (filePickRes == null) return;
Expand Down Expand Up @@ -254,7 +254,14 @@ List<ChatMenuItem> buildUserChatMenu(ChatModel chat) {
}
}

return <ChatMenuItem>[
void subscribeToPosts(
BuildContext context, ClientModel client, ChatModel chat) async {
chat.subscribeToPosts();
client.updateUserMenu(chat.id, buildUserChatMenu(chat));
}

print("${chat.nick} ${chat.isSubscribed} ${chat.isSubscribing}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

return <ChatMenuItem?>[
ChatMenuItem(
"User Profile", (context, chats) => chats.profile = chats.active),
//.of(context, rootNavigator: true).pushNamed('/userProfile', arguments: UserProfileArgs(chat))),
Expand All @@ -275,14 +282,22 @@ List<ChatMenuItem> buildUserChatMenu(ChatModel chat) {
"Unsubscribe to Posts",
(context, chats) => chats.active!.unsubscribeToPosts(),
)
: ChatMenuItem(
"Subscribe to Posts",
(context, chats) => chats.active!.subscribeToPosts(),
),
ChatMenuItem(
chat.isSubscribed ? "List Posts" : "",
(context, chats) => listUserPosts(context, chats, chats.active!),
),
: !chat.isSubscribing
? ChatMenuItem(
"Subscribe to Posts",
(context, chats) =>
subscribeToPosts(context, chats, chats.active!),
)
: ChatMenuItem(
"Subscribing to Posts",
(context, chats) => null,
),
chat.isSubscribed
? ChatMenuItem(
"List Posts",
(context, chats) => listUserPosts(context, chats, chats.active!),
)
: null,
ChatMenuItem(
"Send File",
(context, chats) => sendFile(context, chats.active!),
Expand Down
4 changes: 2 additions & 2 deletions bruig/flutterui/bruig/lib/screens/feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class FeedScreenTitle extends StatelessWidget {
return Consumer2<MainMenuModel, ThemeNotifier>(
builder: (context, menu, theme, child) {
if (menu.activePageTab <= 0) {
return Text("Bison Relay / News Feed",
return Text("Bison Relay / Feed",
style: TextStyle(
fontSize: theme.getLargeFont(context),
color: Theme.of(context).focusColor));
}
var idx = LnScreenSub.indexWhere((e) => e.pageTab == menu.activePageTab);

return Text("Bison Relay / News Feed / ${FeedScreenSub[idx].label}",
return Text("Bison Relay / Feed / ${FeedScreenSub[idx].label}",
style: TextStyle(
fontSize: theme.getLargeFont(context),
color: Theme.of(context).focusColor));
Expand Down
Loading
Loading