Skip to content

Commit

Permalink
Merge pull request #753 from bounswe/app-589/modifications-on-comment…
Browse files Browse the repository at this point in the history
…-widget

modifications on comment widget
  • Loading branch information
tbaturhanakbulut authored Dec 25, 2023
2 parents 1481e2a + e721341 commit b3d6b55
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 28 deletions.
18 changes: 15 additions & 3 deletions app/mobile/lib/models/comment.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
class CommentData {
final String user;
final String username;
final String commentText;
final String commentId;
final String dateTime;
final String userId;

CommentData({required this.user, required this.commentText});
CommentData({
required this.username,
required this.commentText,
required this.commentId,
required this.dateTime,
required this.userId,
});

factory CommentData.fromJson(Map<String, dynamic> json) {
return CommentData(
user: json['user']['username'],
userId: json['user']['id'],
username: json['user']['username'],
commentText: json['description'],
commentId: json['id'],
dateTime: json['created_date'],
);
}
}
23 changes: 23 additions & 0 deletions app/mobile/lib/services/ReportService.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:dio/dio.dart';
import 'package:mobile_app/services/apiService.dart';

class ReportService {
static Future<bool> report(String userId, String reason) async {
String reportEndpoint = '/user/report/$userId';

final Map<String, dynamic> data = {
'reason': reason,
};

try {
final Response response = await ApiService.dio.post(
reportEndpoint,
data: data,
);
return response.statusCode == 201;
} catch (e) {
print('$e');
return false;
}
}
}
14 changes: 13 additions & 1 deletion app/mobile/lib/services/createCommentService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ class PostCommentService {
rethrow;
}
}
}

static Future<bool> deleteComment(String commentId) async {
final String commentEndpoint = '/comment/$commentId';

try {
final Response response = await ApiService.dio.delete(commentEndpoint);
return response.statusCode == 200;
} catch (e) {
print('Error: $e');
return false;
}
}
}
2 changes: 2 additions & 0 deletions app/mobile/lib/view/login/loginScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class _LoginScreenState extends State<LoginScreen> {
if (loggedInUserData.statusCode == 200) {
AppState.isModerator = false;
AppState.loggedInUserId = loggedInUserData.data['id'];
// TODO: insallah dogrudur
AppState.loggedInUserUsername = loggedInUserData.data['username'];
AppState.isGuest = false;
print("loginScreen.login: ${AppState.loggedInUserId}");
} else {
Expand Down
149 changes: 138 additions & 11 deletions app/mobile/lib/view/pollView/commentWidget.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,154 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:mobile_app/services/ReportService.dart';
import 'package:mobile_app/services/createCommentService.dart';
import 'package:mobile_app/view/helpers/dateTime.dart';
import 'package:mobile_app/view/pollView/clickableUsername.dart';
import 'package:mobile_app/view/state.dart';

import '../constants.dart';

class CommentWidget extends StatelessWidget {
final String user;
class CommentWidget extends StatefulWidget {
final String username;
final String userId;
final String commentText;
final String commentId;
final String dateTime;
final void Function() parentSetState;

const CommentWidget(
{super.key, required this.user, required this.commentText});
const CommentWidget({
super.key,
required this.username,
required this.commentText,
required this.commentId,
required this.dateTime,
required this.userId,
required this.parentSetState,
});

@override
State<CommentWidget> createState() => _CommentWidgetState();
}

class _CommentWidgetState extends State<CommentWidget> {
void _deleteComment() async {
await PostCommentService.deleteComment(widget.commentId);
widget.parentSetState();
}

void report(String reason) async {
var result = await ReportService.report(widget.userId, reason);
// show a dialog to inform the user about the result
if (!context.mounted) return;
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Report User'),
content: Text(result
? 'User reported successfully'
: 'User could not be reported'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'),
),
],
);
},
);
}

// a dialog to show input the reason for reporting
void showReportDialog(BuildContext context) {
final TextEditingController reasonController = TextEditingController();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Report User'),
content: TextField(
controller: reasonController,
decoration: const InputDecoration(
hintText: 'Reason',
),
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
report(reasonController.text);
Navigator.pop(context);
},
child: const Text('Report'),
),
],
);
},
);
}

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
padding: const EdgeInsets.fromLTRB(8, 0, 8, 6),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
border: Border.all(color: navy),
color: gray,
borderRadius: BorderRadius.circular(10.0),
border: Border.all(color: gray, width: 2),
color: whitish,
),
child: ListTile(
title: Text(user),
subtitle: Text(commentText),
child: Stack(
children: [
ListTile(
title: ClickableUsername(
username: widget.username,
displayName: widget.username,
),
subtitle: Text(widget.commentText),
),
Positioned(
top: 12,
right: 60,
child: Text(
DateFormat('MMM d\'th\', HH:mm')
.format(DateTime.parse(widget.dateTime)),
),
),
if (widget.username == AppState.loggedInUserUsername)
Positioned(
top: -4,
right: -4,
child: IconButton(
icon: const Icon(Icons.delete),
color: Colors.grey.shade700,
onPressed: () {
_deleteComment();
print("delete onpressed");
},
),
)
else
Positioned(
top: -4,
right: -4,
child: IconButton(
color: Colors.grey.shade700,
icon: const Icon(Icons.report),
onPressed: () {
showReportDialog(context);
print("report onpressed");
},
),
)
],
),
),
);
Expand Down
35 changes: 22 additions & 13 deletions app/mobile/lib/view/pollView/pollView.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class PollPage extends StatefulWidget {
required this.dateTime,
required this.isSettled,
this.approvedStatus,
required this.chosenVoteIndex, required this.annotationIndices, required this.annotationTexts,
required this.chosenVoteIndex,
required this.annotationIndices,
required this.annotationTexts,
});

@override
Expand Down Expand Up @@ -89,9 +91,9 @@ class _PollPageState extends State<PollPage> {
pollId: widget.isSettled == 0 ? widget.pollId : "",
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: buildRichText(widget.postTitle, widget.annotationIndices, widget.annotationTexts)
),
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: buildRichText(widget.postTitle, widget.annotationIndices,
widget.annotationTexts)),
TagListWidget(tags: widget.tags, tagColors: widget.tagColors),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
Expand Down Expand Up @@ -120,7 +122,8 @@ class _PollPageState extends State<PollPage> {
Row(
children: [
LikeCountWidget(likeCount: widget.likeCount),
DateTimeWidget(dateTime: DateTime.parse(widget.dateTime), color: blue),
DateTimeWidget(
dateTime: DateTime.parse(widget.dateTime), color: blue),
],
),
CommentEntryFieldWidget(
Expand All @@ -139,7 +142,9 @@ class _PollPageState extends State<PollPage> {
// If we run into an error, display it to the user
return Text('Error: ${snapshot.error}');
} else {
var fetchedComments = snapshot.hasData ? snapshot.data! : [];
var fetchedComments = snapshot.hasData
? snapshot.data!
: [] as List<CommentData>;

// Whether we have data or not, display the number of fetchedComments
int commentCount = fetchedComments.length;
Expand All @@ -155,8 +160,14 @@ class _PollPageState extends State<PollPage> {
if (snapshot.hasData)
...fetchedComments
.map((comment) => CommentWidget(
user: comment.user,
parentSetState: () {
setState(() {});
},
username: comment.username,
commentText: comment.commentText,
commentId: comment.commentId,
dateTime: comment.dateTime,
userId: comment.userId,
))
.toList(),
// If there's no data, there's nothing else to add to the column
Expand Down Expand Up @@ -188,9 +199,8 @@ class _PollPageState extends State<PollPage> {
print("pressed like");
}

RichText buildRichText(String fullText, List<List<int>> indices, List<String> annotationTexts) {


RichText buildRichText(
String fullText, List<List<int>> indices, List<String> annotationTexts) {
List<TextSpan> textSpans = [];

int previousIndex = 0;
Expand Down Expand Up @@ -220,7 +230,8 @@ class _PollPageState extends State<PollPage> {
..onTap = () {
// Handle tap on the underlined text
_showPopup(context, annotationText);
print('Tapped on underlined text from index $startIndex to $endIndex!');
print(
'Tapped on underlined text from index $startIndex to $endIndex!');
},
),
);
Expand Down Expand Up @@ -320,8 +331,6 @@ class CommentEntryFieldWidget extends StatelessWidget {
}
}



class LikeCountWidget extends StatelessWidget {
final int likeCount;

Expand Down
2 changes: 2 additions & 0 deletions app/mobile/lib/view/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import 'dart:math';
class AppState {
static bool isModerator = false;
static String loggedInUserId = '';
static String loggedInUserUsername = '';
static Random random = Random();
static bool isGuest = true;

static void logout() {
isModerator = false;
loggedInUserId = '';
loggedInUserUsername = '';
isGuest = true;
}
}

0 comments on commit b3d6b55

Please sign in to comment.