Skip to content

Commit

Permalink
feat: got chirp homepage to display posts from the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMuuo committed Aug 15, 2024
1 parent efeedd0 commit 9d19670
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 28 deletions.
71 changes: 71 additions & 0 deletions lib/tools/chirp/models/core/chirp_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class ChirpUser {
final String id;
final String password;
final DateTime? lastLogin;
final bool isSuperuser;
final String username;
final bool isStaff;
final bool isActive;
final DateTime dateJoined;
final String email;
final String? profilePhoto;
final String firstName;
final String lastName;
final int upvotes;

ChirpUser({
required this.id,
required this.password,
this.lastLogin,
required this.isSuperuser,
required this.username,
required this.isStaff,
required this.isActive,
required this.dateJoined,
required this.email,
this.profilePhoto,
required this.firstName,
required this.lastName,
required this.upvotes,
});

// Factory method to create a ChirpUser instance from JSON
factory ChirpUser.fromJson(Map<String, dynamic> json) {
return ChirpUser(
id: json['id'] as String,
password: json['password'] as String,
lastLogin: json['last_login'] != null
? DateTime.parse(json['last_login'])
: null,
isSuperuser: json['is_superuser'] as bool,
username: json['username'] as String,
isStaff: json['is_staff'] as bool,
isActive: json['is_active'] as bool,
dateJoined: DateTime.parse(json['date_joined'] as String),
email: json['email'] as String,
profilePhoto: json['profile_photo'] as String?,
firstName: json['first_name'] as String,
lastName: json['last_name'] as String,
upvotes: json['upvotes'] as int,
);
}

// Method to convert ChirpUser instance to JSON
Map<String, dynamic> toJson() {
return {
'id': id,
'password': password,
'last_login': lastLogin?.toIso8601String(),
'is_superuser': isSuperuser,
'username': username,
'is_staff': isStaff,
'is_active': isActive,
'date_joined': dateJoined.toIso8601String(),
'email': email,
'profile_photo': profilePhoto,
'first_name': firstName,
'last_name': lastName,
'upvotes': upvotes,
};
}
}
1 change: 1 addition & 0 deletions lib/tools/chirp/models/core/core.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'post_attachment_media.dart';
export 'chirp_user.dart';
export 'post.dart';
10 changes: 2 additions & 8 deletions lib/tools/chirp/models/core/post.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import 'package:academia/models/core/user/user.dart';

import 'core.dart';

class Post {
final String id;
final String username;
final String title;
final String content;
final int upvotes;
Expand All @@ -14,12 +11,11 @@ class Post {
final DateTime createdAt;
final DateTime modifiedAt;
final String? link;
final User? user;
final ChirpUser? user;
final List<PostAttachmentMedia> postAttachmentMedia;

Post({
required this.id,
required this.username,
required this.title,
required this.content,
required this.upvotes,
Expand All @@ -36,7 +32,6 @@ class Post {
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
id: json['id'] as String,
username: json['username'] as String,
title: json['title'] as String,
content: json['content'] as String,
upvotes: json['upvotes'] as int,
Expand All @@ -46,7 +41,7 @@ class Post {
createdAt: DateTime.parse(json['created_at'] as String),
modifiedAt: DateTime.parse(json['modified_at'] as String),
link: json['link'] as String?,
user: User.fromJson(json["user"]),
user: ChirpUser.fromJson(json["user"]),
postAttachmentMedia: (json['post_attachment_media'] as List<dynamic>)
.map((e) => PostAttachmentMedia.fromJson(e as Map<String, dynamic>))
.toList(),
Expand All @@ -56,7 +51,6 @@ class Post {
Map<String, dynamic> toJson() {
return {
'id': id,
'username': username,
'title': title,
'content': content,
'upvotes': upvotes,
Expand Down
4 changes: 2 additions & 2 deletions lib/tools/chirp/models/core/post_attachment_media.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PostAttachmentMedia {
final String id;
final String image;
final String? image;
final DateTime createdAt;
final String post;

Expand All @@ -14,7 +14,7 @@ class PostAttachmentMedia {
factory PostAttachmentMedia.fromJson(Map<String, dynamic> json) {
return PostAttachmentMedia(
id: json['id'] as String,
image: json['image'] as String,
image: json['image'] as String?,
createdAt: DateTime.parse(json['created_at'] as String),
post: json['post'] as String,
);
Expand Down
4 changes: 2 additions & 2 deletions lib/tools/chirp/pages/post_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PostViewPage extends StatelessWidget {
slivers: [
SliverAppBar(
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
title: Text("@${post.username}"),
title: Text("@${post.user?.username ?? 'anon'}"),
),
SliverPadding(
padding: const EdgeInsets.all(12),
Expand Down Expand Up @@ -45,7 +45,7 @@ class PostViewPage extends StatelessWidget {
itemBuilder: (context, index) {
final data = post.postAttachmentMedia[index];
return CachedNetworkImage(
imageUrl: data.image,
imageUrl: data.image ?? "",
height: 300,
width: MediaQuery.of(context).size.width,
);
Expand Down
42 changes: 26 additions & 16 deletions lib/tools/chirp/widgets/post_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ class PostCard extends StatelessWidget {
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
CircleAvatar(
backgroundImage:
CachedNetworkImageProvider(post.user!.profileUrl),
),
post.user!.profilePhoto != null
? CircleAvatar(
backgroundImage: CachedNetworkImageProvider(
post.user?.profilePhoto ?? '',
),
)
: Image.asset(
"assets/images/male_student.png",
height: 30,
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
"@${post.username}",
"@${post.user?.username ?? 'anon'}",
style: Theme.of(context)
.textTheme
.bodyMedium
Expand Down Expand Up @@ -70,20 +76,23 @@ class PostCard extends StatelessWidget {
visible: post.postAttachmentMedia.isNotEmpty,
child: SizedBox(
height: 200,
child: ListView.builder(
child: ListView.separated(
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
final data = post.postAttachmentMedia[index];
return CachedNetworkImage(
imageUrl: data.image,
height: 300,
imageUrl: data.image ?? "",
fit: BoxFit.fitWidth,
width: MediaQuery.of(context).size.width,
);
},
separatorBuilder: (context, index) =>
const SizedBox(width: 4),
itemCount: post.postAttachmentMedia.length,
),
),
),
const SizedBox(height: 4),
Text(
trimTo99Characters(post.content),
style: Theme.of(context).textTheme.bodySmall,
Expand Down Expand Up @@ -112,14 +121,15 @@ class PostCard extends StatelessWidget {
),
const Spacer(),
const IconButton(
onPressed: null,
icon: Row(
children: [
Icon(Ionicons.chatbox_outline),
SizedBox(width: 2),
Text("30")
],
))
onPressed: null,
icon: Row(
children: [
Icon(Ionicons.chatbox_outline),
SizedBox(width: 2),
Text("30")
],
),
),
],
)
],
Expand Down

0 comments on commit 9d19670

Please sign in to comment.