-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from AmityCo/tr/feat-custom-post
[ASC-23158] Create and Edit Custom Post
- Loading branch information
Showing
6 changed files
with
317 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
lib/presentation/screen/create_custom_post/create_custom_post_screen.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
lib/presentation/screen/update_post/update_custom_post_screen.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |