Skip to content

Commit

Permalink
Merge pull request #231 from AmityCo/tr/feat-custom-post
Browse files Browse the repository at this point in the history
[ASC-23158] Create and Edit Custom Post
  • Loading branch information
kornsitti authored Jun 13, 2024
2 parents 84d388e + fc51f4f commit bfb3264
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lib/core/route/app_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class AppRoute {
static const createPollPost = 'createPollPost';
static const createPollPostRoute = 'createPollPost';

static const createCustomPost = 'createCustomPost';
static const createCustomPostRoute = 'createCustomPost';

static const chat = 'chat';
static const chatRoute = 'chatRoute/:channelId';

Expand Down
8 changes: 8 additions & 0 deletions lib/core/route/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:flutter_social_sample_app/presentation/screen/community_notifica
import 'package:flutter_social_sample_app/presentation/screen/community_pending_post/community_pending_post_screen.dart';
import 'package:flutter_social_sample_app/presentation/screen/community_profile/community_profile_screen.dart';
import 'package:flutter_social_sample_app/presentation/screen/community_update/community_update_screen.dart';
import 'package:flutter_social_sample_app/presentation/screen/create_custom_post/create_custom_post_screen.dart';
import 'package:flutter_social_sample_app/presentation/screen/create_livestream_post/create_livestream_post.dart';
import 'package:flutter_social_sample_app/presentation/screen/create_poll_post/create_poll_post_screen.dart';
import 'package:flutter_social_sample_app/presentation/screen/create_post/create_post_screen.dart';
Expand Down Expand Up @@ -317,6 +318,13 @@ class AppRouter {
path: AppRoute.createPollPostRoute,
builder: (context, state) => const CreatePollPostScreen(),
),

GoRoute(
name: AppRoute.createCustomPost,
path: AppRoute.createCustomPostRoute,
builder: (context, state) => const CreateCustomPostScreen(),
),

GoRoute(
name: AppRoute.chat,
path: AppRoute.chatRoute,
Expand Down
40 changes: 31 additions & 9 deletions lib/core/widget/feed_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_social_sample_app/core/route/app_route.dart';
import 'package:flutter_social_sample_app/core/utils/extension/date_extension.dart';
import 'package:flutter_social_sample_app/core/widget/add_comment_widget.dart';
import 'package:flutter_social_sample_app/core/widget/common_snackbar.dart';
import 'package:flutter_social_sample_app/core/widget/dynamic_text_highlighting.dart';
import 'package:flutter_social_sample_app/core/widget/poll_widget.dart';
import 'package:flutter_social_sample_app/core/widget/reaction_action_widget.dart';
import 'package:flutter_social_sample_app/core/widget/shadow_container_widget.dart';
import 'package:flutter_social_sample_app/core/widget/user_profile_info_row_widget.dart';
import 'package:flutter_social_sample_app/presentation/screen/update_post/update_custom_post_screen.dart';
import 'package:flutter_social_sample_app/presentation/screen/update_post/update_post_screen.dart';
import 'package:flutter_social_sample_app/presentation/screen/video_player/full_screen_video_player.dart';
import 'package:go_router/go_router.dart';
Expand Down Expand Up @@ -94,15 +94,28 @@ class FeedWidget extends StatelessWidget {
),
onSelected: (index) {
if (index == 1) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => UpdatePostScreen(
amityPost: value,
communityId: communityId,
isPublic: isPublic,
if (value.data is CustomData) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
UpdateCustomPostScreen(
amityPost: value),
),
),
);
);

} else {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
UpdatePostScreen(
amityPost: value,
communityId: communityId,
isPublic: isPublic,
),
),
);
}
}
if (index == 2) {
value.delete();
Expand Down Expand Up @@ -482,6 +495,15 @@ class FeedContentWidget extends StatelessWidget {
);
}

if (amityPostData is CustomData) {
final data = amityPostData as CustomData;
return Container(
// color: Colors.green,
child:
Text('Custom post content -->>>> ${data.rawData}'),
);
}

return Container(
color: Colors.red,
child: Text('>>>>> $amityPostData <<<<<<'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import 'package:amity_sdk/amity_sdk.dart';
import 'package:flutter/material.dart';

class CreateCustomPostScreen extends StatefulWidget {
const CreateCustomPostScreen({Key? key, this.userId}) : super(key: key);
final String? userId;

@override
State<CreateCustomPostScreen> createState() => _CreateCustomPostScreenState();
}

class _CreateCustomPostScreenState extends State<CreateCustomPostScreen> {
final _targetUserTextEditController = TextEditingController();
final _keyTextController = TextEditingController();
final _valueTextController = TextEditingController();

@override
Widget build(BuildContext context) {
final themeData = Theme.of(context);

if (widget.userId != null) {
_targetUserTextEditController.text = widget.userId!;
}

return Scaffold(
appBar: AppBar(
title: const Text('Create Custom Post'),
actions: [
LoadingButton(
onPressed: () async {
try {
FocusManager.instance.primaryFocus?.unfocus();
final target = _targetUserTextEditController.text;
final key = _keyTextController.text.trim();
final value = _valueTextController.text.trim();

if (key.isEmpty || value.isEmpty) {
CommonSnackbar.showNagativeSnackbar(
context, 'Error', 'Key and value cannot be empty');
return;
}

final customData = {key: value};

final amityPost = await AmitySocialClient.newPostRepository()
.createPost()
.targetUser(target)
.custom('amity.custom', customData)
.post();

CommonSnackbar.showPositiveSnackbar(
context, 'Success', 'Custom Post Created Successfully');
} catch (error, stackTrace) {
print(stackTrace.toString());
CommonSnackbar.showNagativeSnackbar(
context, 'Error', error.toString());
}
},
text: 'POST',
),
],
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: _targetUserTextEditController,
decoration: InputDecoration(
label: Text('Target User'),
),
),
const SizedBox(height: 12),
Text(
'Custom Data',
style: themeData.textTheme.titleMedium!.copyWith(
fontWeight: FontWeight.bold,
),
),
TextFormField(
controller: _keyTextController,
decoration: const InputDecoration(
hintText: 'Key',
),
),
const SizedBox(height: 12),
TextFormField(
controller: _valueTextController,
decoration: const InputDecoration(
hintText: 'Value',
),
),
],
),
),
),
);
}
}

class LoadingButton extends StatelessWidget {
final Future<void> Function() onPressed;
final String text;

const LoadingButton({
Key? key,
required this.onPressed,
required this.text,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () => onPressed(),
child: Text(text),
);
}
}

class CommonSnackbar {
static void showPositiveSnackbar(
BuildContext context, String title, String message) {
final snackBar = SnackBar(
content: Text('$title: $message'),
backgroundColor: Colors.green,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

static void showNagativeSnackbar(
BuildContext context, String title, String message) {
final snackBar = SnackBar(
content: Text('$title: $message'),
backgroundColor: Colors.red,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
7 changes: 7 additions & 0 deletions lib/presentation/screen/dashboard/dashboard_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ class _DashboardScreenState extends State<DashboardScreen> {
child: const Text('Create Poll Post'),
),
const SizedBox(height: 20),
TextButton(
onPressed: () {
GoRouter.of(context).goNamed(AppRoute.createCustomPost);
},
child: const Text('Create Custom Post'),
),
const SizedBox(height: 20),
TextButton(
onPressed: () async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
Expand Down
128 changes: 128 additions & 0 deletions lib/presentation/screen/update_post/update_custom_post_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'package:amity_sdk/amity_sdk.dart';
import 'package:flutter/material.dart';

class UpdateCustomPostScreen extends StatefulWidget {
const UpdateCustomPostScreen({Key? key, required this.amityPost})
: super(key: key);
final AmityPost amityPost;

@override
State<UpdateCustomPostScreen> createState() => _UpdateCustomPostScreenState();
}

class _UpdateCustomPostScreenState extends State<UpdateCustomPostScreen> {
final _keyTextController = TextEditingController();
final _valueTextController = TextEditingController();

@override
Widget build(BuildContext context) {
final themeData = Theme.of(context);

return Scaffold(
appBar: AppBar(
title: const Text('Update Custom Post'),
actions: [
LoadingButton(
onPressed: () async {
try {
FocusManager.instance.primaryFocus?.unfocus();
final key = _keyTextController.text.trim();
final value = _valueTextController.text.trim();

if (key.isEmpty || value.isEmpty) {
CommonSnackbar.showNagativeSnackbar(
context, 'Error', 'Key and value cannot be empty');
return;
}

final customData = {key: value};

await widget.amityPost
.edit()
.custom(customData)
.build()
.update();

CommonSnackbar.showPositiveSnackbar(
context, 'Success', 'Custom Post Updated Successfully');
} catch (error, stackTrace) {
print(stackTrace.toString());
CommonSnackbar.showNagativeSnackbar(
context, 'Error', error.toString());
}
},
text: 'UPDATE',
),
],
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Custom Data',
style: themeData.textTheme.titleMedium!.copyWith(
fontWeight: FontWeight.bold,
),
),
TextFormField(
controller: _keyTextController,
decoration: const InputDecoration(
hintText: 'Key',
),
),
const SizedBox(height: 12),
TextFormField(
controller: _valueTextController,
decoration: const InputDecoration(
hintText: 'Value',
),
),
],
),
),
),
);
}
}

class LoadingButton extends StatelessWidget {
final Future<void> Function() onPressed;
final String text;

const LoadingButton({
Key? key,
required this.onPressed,
required this.text,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () => onPressed(),
child: Text(text),
);
}
}

class CommonSnackbar {
static void showPositiveSnackbar(
BuildContext context, String title, String message) {
final snackBar = SnackBar(
content: Text('$title: $message'),
backgroundColor: Colors.green,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

static void showNagativeSnackbar(
BuildContext context, String title, String message) {
final snackBar = SnackBar(
content: Text('$title: $message'),
backgroundColor: Colors.red,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}

0 comments on commit bfb3264

Please sign in to comment.