/
comment_item.dart
209 lines (196 loc) · 6.22 KB
/
comment_item.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:git_touch/graphql/github.data.gql.dart';
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/widgets/action_button.dart';
import 'package:git_touch/widgets/markdown_view.dart';
import 'package:provider/provider.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:primer/primer.dart';
import '../utils/utils.dart';
import 'avatar.dart';
import 'link.dart';
import 'user_name.dart';
final emojiMap = {
'THUMBS_UP': '👍',
'THUMBS_DOWN': '👎',
'LAUGH': '😄',
'HOORAY': '🎉',
'CONFUSED': '😕',
'HEART': '❤️',
'ROCKET': '🚀',
'EYES': '👀'
};
class GhEmojiAction extends StatefulWidget {
final Map<String, dynamic> payload;
GhEmojiAction(this.payload);
@override
_GhEmojiActionState createState() => _GhEmojiActionState();
}
class _GhEmojiActionState extends State<GhEmojiAction> {
get payload => widget.payload;
onReaction(String emojiKey) async {
if (emojiKey == null) return;
final isRemove = _hasReacted(emojiKey);
var id = payload['id'] as String;
var operation = isRemove ? 'remove' : 'add';
try {
await context.read<AuthModel>().query('''
mutation {
${operation}Reaction(input: {subjectId: "$id", content: $emojiKey}) {
clientMutationId
}
}
''');
setState(() {
payload[emojiKey]['totalCount'] += isRemove ? -1 : 1;
payload[emojiKey]['viewerHasReacted'] = !isRemove;
});
} catch (e) {
context.read<ThemeModel>().showWarning(context, e);
}
}
bool _hasReacted(String emojiKey) {
if (payload[emojiKey] == null) return false;
return payload[emojiKey]['viewerHasReacted'] as bool;
}
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
return Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
...emojiMap.entries
.where((entry) => payload[entry.key]['totalCount'] as int != 0)
.map<Widget>((entry) {
var emojiKey = entry.key;
var emoji = entry.value;
var count = payload[entry.key]['totalCount'] as int;
return Link(
onTap: () {
onReaction(emojiKey);
},
child: Container(
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
color: _hasReacted(emojiKey)
? (theme.brightness == Brightness.dark
? PrimerColors.blue900
: PrimerColors.blue000)
: Colors.transparent,
),
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
Text(emoji, style: TextStyle(fontSize: 18)),
SizedBox(width: 4),
Text(numberFormat.format(count),
style:
TextStyle(color: theme.palette.primary, fontSize: 14))
],
),
),
);
}),
Link(
onTap: () async {
await theme.showActions(
context,
emojiMap.entries.map((entry) {
final emojiKey = entry.key;
return ActionItem(
text: emojiKey + ' ' + entry.value,
onTap: (_) {
onReaction(emojiKey);
},
);
}).toList(),
);
},
child: Container(
padding: EdgeInsets.all(4),
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
Text('+', style: TextStyle(color: theme.palette.primary)),
Icon(Octicons.smiley, color: theme.palette.primary, size: 18),
],
),
),
),
],
);
}
}
class CommentItem extends StatelessWidget {
final Avatar avatar;
final String login;
final DateTime createdAt;
final String body;
final String prefix;
final List<Widget> widgets;
final List<ActionItem> commentActionItemList;
// p.author could be null (deleted user)
CommentItem.gql(GCommentParts p)
: avatar = Avatar(
url: p.author?.avatarUrl ??
'https://avatars.githubusercontent.com/u/10137?s=460&u=b1951d34a583cf12ec0d3b0781ba19be97726318&v=4',
linkUrl: '/github/' + (p.author?.login ?? 'ghost'),
),
login = p.author?.login ?? 'ghost',
createdAt = p.createdAt,
body = p.body,
widgets = [], // [GhEmojiAction(payload)], // TODO:
prefix = 'github',
commentActionItemList = []; // TODO
CommentItem({
@required this.avatar,
@required this.login,
@required this.createdAt,
@required this.body,
@required this.prefix,
this.widgets,
this.commentActionItemList,
});
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(children: <Widget>[
avatar,
SizedBox(width: 8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
UserName(login, prefix),
SizedBox(height: 2),
Text(
timeago.format(createdAt),
style: TextStyle(
color: theme.palette.tertiaryText, fontSize: 13),
),
],
),
),
Align(
alignment: Alignment.centerRight,
child: ActionButton(
iconData: Octicons.kebab_horizontal,
title: 'Comment Actions',
items: [
if (commentActionItemList != null) ...commentActionItemList
],
)),
]),
SizedBox(height: 12),
MarkdownFlutterView(body, padding: EdgeInsets.zero), // TODO: link
SizedBox(height: 12),
if (widgets != null) ...widgets
],
);
}
}