From d8e8c11d965bb5e34a1d07e8db9c836d8770affb Mon Sep 17 00:00:00 2001 From: kumarpalsinh25 Date: Fri, 21 Feb 2025 10:47:50 +0530 Subject: [PATCH] Added post update validations --- .../pages/add_news/add_news_post_to_page.dart | 114 ++++++++++-------- app/lib/l10n/app_en.arb | 2 + 2 files changed, 65 insertions(+), 51 deletions(-) diff --git a/app/lib/features/news/pages/add_news/add_news_post_to_page.dart b/app/lib/features/news/pages/add_news/add_news_post_to_page.dart index ebfb3e7fbbf5..9148a44152ed 100644 --- a/app/lib/features/news/pages/add_news/add_news_post_to_page.dart +++ b/app/lib/features/news/pages/add_news/add_news_post_to_page.dart @@ -8,10 +8,11 @@ import 'package:acter/features/news/model/keys.dart'; import 'package:acter/features/news/providers/news_post_editor_providers.dart'; import 'package:acter/features/space/widgets/space_sections/section_header.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -enum PostType { story, boost } +enum PostTypeSelection { story, boost, none } class AddNewsPostToPage extends ConsumerStatefulWidget { final String? initialSelectedSpace; @@ -23,23 +24,27 @@ class AddNewsPostToPage extends ConsumerStatefulWidget { } class _AddNewsPostToPageState extends ConsumerState { - PostType selectedOption = PostType.story; + ValueNotifier selectedPostType = + ValueNotifier(PostTypeSelection.none); ValueNotifier canPostBoost = ValueNotifier(false); + ValueNotifier canPostStories = ValueNotifier(false); @override Widget build(BuildContext context) { + //Initialize variables based on the selected space final selectedSpaceId = ref.read(newsStateProvider).newsPostSpaceId; if (selectedSpaceId != null) { final membership = ref.watch(roomMembershipProvider(selectedSpaceId)).valueOrNull; canPostBoost.value = membership?.canString('CanPostNews') == true; - if (canPostBoost.value == false) { - selectedOption = PostType.story; - } + canPostStories.value = membership?.canString('CanPostStories') == true; + selectedPostType.value = PostTypeSelection.none; } + + //Start build UI return Scaffold( appBar: appBarUI(), - body: postToBodyUI(), + body: postToBodyUI(selectedSpaceId), ); } @@ -48,29 +53,34 @@ class _AddNewsPostToPageState extends ConsumerState { return AppBar(title: Text(lang.postTo)); } - Widget postToBodyUI() { + Widget postToBodyUI(String? selectedSpaceId) { final lang = L10n.of(context); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ spaceSelector(), SectionHeader(title: lang.select), - postOptionItemUI( - true, - lang.story, - lang.storyInfo, - Icons.amp_stories, - PostType.story, + ValueListenableBuilder( + valueListenable: canPostStories, + builder: (context, canPostStories, child) { + return postOptionItemUI( + isEnable: canPostStories, + title: lang.story, + description: lang.storyInfo, + iconData: Icons.amp_stories, + postTypeSelection: PostTypeSelection.story, + ); + }, ), ValueListenableBuilder( valueListenable: canPostBoost, builder: (context, canPostBoost, child) { return postOptionItemUI( - canPostBoost, - lang.boost, - lang.boostInfo, - Icons.rocket_launch_sharp, - PostType.boost, + isEnable: canPostBoost, + title: lang.boost, + description: lang.boostInfo, + iconData: Icons.rocket_launch_sharp, + postTypeSelection: PostTypeSelection.boost, ); }, ), @@ -78,9 +88,20 @@ class _AddNewsPostToPageState extends ConsumerState { Padding( padding: const EdgeInsets.all(18), child: ActerPrimaryActionButton( - onPressed: () => selectedOption == PostType.story - ? sendStory(context, ref) - : sendNews(context, ref), + onPressed: () { + if (selectedSpaceId == null || selectedSpaceId.isEmpty) { + EasyLoading.showToast(lang.pleaseSelectSpace); + } else if (canPostBoost.value == false && + canPostStories.value == false) { + EasyLoading.showToast(lang.notHaveBoostStoryPermission); + } else if (selectedPostType.value == PostTypeSelection.none) { + EasyLoading.showToast(lang.pleaseSelectePostType); + } else if (selectedPostType.value == PostTypeSelection.story) { + sendStory(context, ref); + } else if (selectedPostType.value == PostTypeSelection.boost) { + sendNews(context, ref); + } + }, child: Text(lang.post.toUpperCase()), ), ), @@ -125,22 +146,16 @@ class _AddNewsPostToPageState extends ConsumerState { ); } - Widget postOptionItemUI( - bool isEnable, - String title, - String description, - IconData iconData, - PostType optionValue, - ) { + Widget postOptionItemUI({ + required bool isEnable, + required String title, + required String description, + required IconData iconData, + required PostTypeSelection postTypeSelection, + }) { final color = !isEnable ? Theme.of(context).disabledColor : null; return InkWell( - onTap: isEnable - ? () { - setState(() { - selectedOption = optionValue; - }); - } - : null, + onTap: isEnable ? () => selectedPostType.value = postTypeSelection : null, child: Padding( padding: const EdgeInsets.all(18), child: Row( @@ -152,10 +167,7 @@ class _AddNewsPostToPageState extends ConsumerState { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text( - title, - style: TextStyle(color: color), - ), + Text(title, style: TextStyle(color: color)), Text( description, style: Theme.of(context) @@ -166,18 +178,18 @@ class _AddNewsPostToPageState extends ConsumerState { ], ), ), - Radio( - value: optionValue, - groupValue: selectedOption, - onChanged: isEnable - ? (value) { - setState(() { - selectedOption = value as PostType; - }); - } - : null, - focusNode: FocusNode(), - toggleable: isEnable, + ValueListenableBuilder( + valueListenable: selectedPostType, + builder: (context, postType, child) { + return Radio( + value: postTypeSelection, + groupValue: postType, + onChanged: isEnable + ? (value) => selectedPostType.value = postType + : null, + toggleable: isEnable, + ); + }, ), ], ), diff --git a/app/lib/l10n/app_en.arb b/app/lib/l10n/app_en.arb index b9087e8dab80..f253615a661c 100644 --- a/app/lib/l10n/app_en.arb +++ b/app/lib/l10n/app_en.arb @@ -2347,6 +2347,8 @@ "story": "Story", "storyInfo": "Everyone can see, this is from you. It disappears in 14 days", "boostInfo": "Important News. Sends a push notification to 17 members", + "notHaveBoostStoryPermission": "You do not have permission to post Boost or Story in selected space", + "pleaseSelectePostType": "Please select post type", "postTo": "Post to", "post": "Post", "unableToLoadFile": "Unable to load file",