-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathliftoff_action.dart
137 lines (101 loc) · 3.21 KB
/
liftoff_action.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
import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';
import 'stores/accounts_store.dart';
import 'widgets/comment/comment_store.dart';
import 'widgets/post/post_store.dart';
/// An action that can be performed on a post or comment.
/// It is used to build the action buttons in the UI, as
/// swipe actions in a post/comment list.
///
/// They are currently instantiated using their constructors
/// directly, but in the future they will be user configurable.
@immutable
abstract class LiftoffAction {
/// The name of the action, e.g. "Upvote".
String get name;
/// The tooltip of the action, e.g. "upvote".
String get tooltip;
/// The icon of the action, e.g. Icons.arrow_upward.
IconData get icon;
/// The color of the action.
/// This is used to color background of the action button, as
/// well as the area behind the post/comment in a swipe action.
Color get activeColor;
/// Whether the action is currently activated.
/// Will be used to color the action button, and render a
/// crossed out icon in swipe actions.
bool get isActivated;
/// The function that will be invoked when the action is
/// triggered.
/// It will be wrapped in a call to [loggedInAction].
Future<void> Function(UserData userData) get invoke;
}
//////////////////////// Upvote actions ////////////////////////
class PostUpvoteAction extends _UpvoteAction {
final PostStore post;
const PostUpvoteAction({
required this.post,
required super.context,
});
@override
Future<void> Function(UserData userData) get invoke => post.upVote;
@override
bool get isActivated => post.postView.myVote == VoteType.up;
}
class CommentUpvoteAction extends _UpvoteAction {
final CommentStore comment;
const CommentUpvoteAction({
required this.comment,
required super.context,
});
@override
Future<void> Function(UserData userData) get invoke => comment.upVote;
@override
bool get isActivated => comment.comment.myVote == VoteType.up;
}
abstract class _UpvoteAction implements LiftoffAction {
final BuildContext context;
const _UpvoteAction({
required this.context,
});
@override
Color get activeColor => Colors.blue.withAlpha(180);
@override
IconData get icon => Icons.arrow_upward;
@override
String get name => 'Upvote';
@override
String get tooltip => 'upvote';
}
//////////////////////// Save actions ////////////////////////
class PostSaveAction extends _SaveAction {
final PostStore post;
const PostSaveAction({
required this.post,
});
@override
Future<void> Function(UserData userData) get invoke => post.save;
@override
bool get isActivated => post.postView.saved;
}
class CommentSaveAction extends _SaveAction {
final CommentStore comment;
const CommentSaveAction({
required this.comment,
});
@override
Future<void> Function(UserData userData) get invoke => comment.save;
@override
bool get isActivated => comment.comment.saved;
}
abstract class _SaveAction implements LiftoffAction {
const _SaveAction();
@override
Color get activeColor => Colors.green.withAlpha(180);
@override
IconData get icon => Icons.bookmark;
@override
String get name => 'Save';
@override
String get tooltip => 'save';
}