Skip to content

Commit

Permalink
feat: chirp post upvotes and downvotes working
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMuuo committed Sep 19, 2024
1 parent 5411183 commit 8800628
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void main() async {
home: const Academia(),
theme: lightModeTheme,
debugShowCheckedModeBanner: false,
// darkTheme: darkModeTheme,
darkTheme: darkModeTheme,
),
);
}
Expand Down
3 changes: 0 additions & 3 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ class _SettingsPageState extends State<SettingsPage> {
),
);
}

// if (context.mounted)
// exit(0);
},
child: const Text("Yes leave"),
)
Expand Down
2 changes: 1 addition & 1 deletion lib/tools/chirp/models/services/chirp_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ChirpService {
static const urlPrefix = "http://192.168.100.2:8080";
static const urlPrefix = "http://127.0.0.1:8080";
// static const urlPrefix = "http://62.169.16.219:84";
}
22 changes: 22 additions & 0 deletions lib/tools/chirp/models/services/post_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,26 @@ class PostService with ChirpService {
return Left("Failed to post comment error: ${e.toString()}");
}
}

Future<Either<String, bool>> postVote(
Map<String, String> authHeaders, String action, String postID) async {
try {
authHeaders.addAll({"content-type": "application/json"});
final response = await http.get(
Uri.parse("${ChirpService.urlPrefix}/posts/vote/$action/$postID"),
headers: authHeaders,
);

if (response.statusCode == 200) {
return right(true);
}
if (response.statusCode == 404) {
return right(false);
}

return left("Something went wrong while attempting to vote for post");
} catch (e) {
return left("Please check your internet connection and try again");
}
}
}
114 changes: 78 additions & 36 deletions lib/tools/chirp/widgets/post_card.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import 'package:academia/exports/barrel.dart';
import 'package:get/get.dart';
import '../pages/post_view_page.dart';
import 'package:timeago/timeago.dart' as timeago;

class PostCard extends StatelessWidget {
class PostCard extends StatefulWidget {
const PostCard({
super.key,
required this.post,
});

final Post post;
@override
State<PostCard> createState() => _PostCardState();
}

class _PostCardState extends State<PostCard> {
int upvotes = 0;
int downvotes = 0;
PostService ps = PostService();
final UserController userController = Get.find<UserController>();

@override
void initState() {
super.initState();
upvotes = widget.post.upvotes;
downvotes = widget.post.downvotes;
}

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => PostViewPage(
post: post,
post: widget.post,
),
));
},
Expand All @@ -26,10 +44,10 @@ class PostCard extends StatelessWidget {
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
post.user!.profilePhoto != null
widget.post.user!.profilePhoto != null
? CircleAvatar(
backgroundImage: CachedNetworkImageProvider(
post.user?.profilePhoto ?? '',
widget.post.user?.profilePhoto ?? '',
),
)
: Image.asset(
Expand All @@ -42,14 +60,14 @@ class PostCard extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
"@${post.user?.username ?? 'anon'}",
"@${widget.post.user?.username ?? 'anon'}",
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(fontWeight: FontWeight.w800),
),
Text(
timeago.format(post.createdAt),
timeago.format(widget.post.createdAt),
style: Theme.of(context).textTheme.bodySmall,
),
],
Expand All @@ -66,20 +84,20 @@ class PostCard extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
post.title,
widget.post.title,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 4),
Visibility(
visible: post.postAttachmentMedia.isNotEmpty,
visible: widget.post.postAttachmentMedia.isNotEmpty,
child: SizedBox(
height: 200,
child: ListView.separated(
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
final data = post.postAttachmentMedia[index];
final data = widget.post.postAttachmentMedia[index];
return CachedNetworkImage(
imageUrl: data.image ?? "",
fit: BoxFit.fitWidth,
Expand All @@ -88,48 +106,72 @@ class PostCard extends StatelessWidget {
},
separatorBuilder: (context, index) =>
const SizedBox(width: 4),
itemCount: post.postAttachmentMedia.length,
itemCount: widget.post.postAttachmentMedia.length,
),
),
),
const SizedBox(height: 4),
Text(
trimTo99Characters(post.content),
trimTo99Characters(widget.post.content),
style: Theme.of(context).textTheme.bodySmall,
),
],
),
const SizedBox(height: 4),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
IconButton(
onPressed: () {},
icon: Row(
children: [
const Icon(Ionicons.arrow_up_circle_outline),
Text(
post.upvotes.toString(),
)
],
),
FilledButton.tonalIcon(
icon: const Icon(Ionicons.arrow_up_circle_outline),
onPressed: () {
ps
.postVote(userController.authHeaders, "upvote",
widget.post.id)
.then((value) {
value.fold((l) {
debugPrint(l);
}, (r) {
if (r) {
setState(() {
upvotes++;
});
} else {
setState(() {
upvotes--;
});
}
});
});
},
label: Text(upvotes.toString()),
),
const SizedBox(width: 2),
IconButton(
onPressed: () {},
const SizedBox(width: 4),
FilledButton.tonalIcon(
label: Text(downvotes.toString()),
onPressed: () {
ps
.postVote(userController.authHeaders, "downvote",
widget.post.id)
.then((value) {
value.fold((l) {
debugPrint(l);
}, (r) {
if (r) {
setState(() {
downvotes++;
});
} else {
setState(() {
downvotes--;
});
}
});
});
},
icon: const Icon(Ionicons.arrow_down_circle_outline),
),
const Spacer(),
IconButton(
onPressed: null,
icon: Row(
children: [
const Icon(Ionicons.chatbox_outline),
const SizedBox(width: 2),
Text(post.commentsCount.toString())
],
),
),
],
)
),
],
),
),
Expand Down

0 comments on commit 8800628

Please sign in to comment.