-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ieeeuoft/blogModel
IEEE 11: update blog model
- Loading branch information
Showing
6 changed files
with
441 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
|
||
class BlogModel { | ||
final String id; | ||
final List<dynamic> tags; | ||
final String title; | ||
final String description; | ||
final String memberID; | ||
final Timestamp createdTime; | ||
final Timestamp updatedTime; | ||
final String image; | ||
|
||
BlogModel( | ||
{required this.id, | ||
required this.tags, | ||
required this.title, | ||
required this.description, | ||
required this.memberID, | ||
required this.createdTime, | ||
required this.updatedTime, | ||
required this.image}); | ||
|
||
// Converts a Firestore Document to a UserModel | ||
factory BlogModel.fromDocument(Map<String, dynamic> doc, String id) { | ||
return BlogModel( | ||
id: id, | ||
tags: doc['tags'], | ||
title: doc['title'], | ||
description: doc['description'], | ||
memberID: doc['memberID'], | ||
createdTime: doc['createdTime'], | ||
updatedTime: doc['updatedTime'], | ||
image: doc['image'], | ||
); | ||
} | ||
|
||
// Converts a UserModel to a Firestore Document | ||
Map<String, dynamic> toDocument() { | ||
return {// The 'id' field is added here to prevent errors when creating a new post | ||
'tags': tags, | ||
'title': title, | ||
'description': description, | ||
'memberID': memberID, | ||
'createdTime': createdTime, | ||
'updatedTime': updatedTime, | ||
'image': image, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:app/core/models/blog_model.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'blog_viewmodel.dart'; | ||
import 'package:flutter_html/flutter_html.dart'; | ||
import 'package:html_editor_enhanced/html_editor.dart'; | ||
|
||
class BlogPage extends StatefulWidget { | ||
const BlogPage({Key? key}) : super(key: key); | ||
|
||
@override | ||
BlogPageState createState() => BlogPageState(); | ||
} | ||
|
||
class BlogPageState extends State<BlogPage> { | ||
HtmlEditorController controller = HtmlEditorController(); | ||
final ScrollController _scrollController = ScrollController(); | ||
String htmlContent = ''; | ||
Future<void> saveContent() async { | ||
htmlContent = await controller.getText(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ChangeNotifierProvider<BlogViewModel>( | ||
create: (context) => BlogViewModel(), | ||
child: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Profile'), | ||
), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Expanded( | ||
child: Consumer<BlogViewModel>( | ||
builder: (context, blogViewModel, child) { | ||
Provider.of<BlogViewModel>(context, listen: false) | ||
.fetchAll(); | ||
final blogs = blogViewModel.blogPosts; | ||
|
||
return Column( | ||
children: [ | ||
HtmlEditor( | ||
controller: controller, | ||
htmlEditorOptions:const HtmlEditorOptions( | ||
hint: "Insert the blog description here...", | ||
//initalText: "text content initial, if any", | ||
), | ||
otherOptions: const OtherOptions( | ||
height: 200, | ||
), | ||
), | ||
Expanded( | ||
child: blogs.isEmpty | ||
? const Center( | ||
child: Text( | ||
'No blog posts found. Click the button below to create one.'), | ||
) | ||
: ListView.builder( | ||
controller: _scrollController, | ||
itemCount: blogs.length, | ||
itemBuilder: (context, index) { | ||
final blog = blogs[index]; | ||
return ListTile( | ||
title: Text(blog.title), | ||
subtitle: Html(data: blog.description), | ||
trailing: IconButton( | ||
icon: const Icon(Icons.delete), | ||
onPressed: () { | ||
blogViewModel.delete(blog.id); | ||
}, | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
ElevatedButton( | ||
onPressed: () async { | ||
await saveContent(); | ||
BlogModel newBlog = BlogModel( | ||
id: '1', // The 'id' should be generated by Firestore when creating a new post | ||
tags: ['Flutter', 'Firebase'], | ||
title: 'New Blog Post', | ||
description: htmlContent, | ||
memberID: 'member123', | ||
createdTime: Timestamp.now(), | ||
updatedTime: Timestamp.now(), | ||
image: 'url_to_image', | ||
); | ||
// Call createBlog method from BlogViewModel | ||
Provider.of<BlogViewModel>(context, listen: false) | ||
.createBlog((blogs.length+1).toString(), newBlog); | ||
}, | ||
child: const Text('Create Blog'), | ||
), | ||
const SizedBox(height: 20), | ||
ElevatedButton( | ||
onPressed: () async { | ||
await saveContent(); | ||
// Call createBlog method from BlogViewModel | ||
Provider.of<BlogViewModel>(context, listen: false) | ||
.updateBlog("1", htmlContent); | ||
}, | ||
child: const Text('Update Blog 1 Description'), | ||
), | ||
const SizedBox(height: 20), | ||
], | ||
); | ||
}, | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:app/core/models/blog_model.dart'; | ||
|
||
class BlogViewModel with ChangeNotifier { | ||
List<BlogModel> _blogPosts = []; | ||
List<BlogModel> get blogPosts => _blogPosts; | ||
|
||
BlogModel? _blogPost; | ||
BlogModel? get blogPost => _blogPost; | ||
|
||
Future<void> fetchAll() async { | ||
// Fetch all documents from Firestore | ||
var querySnapshot = | ||
await FirebaseFirestore.instance.collection('blogPosts').get(); | ||
// Map the documents to BlogModel instances | ||
_blogPosts = querySnapshot.docs | ||
.map((doc) => BlogModel.fromDocument(doc.data(), doc.id)) | ||
.toList(); | ||
// Notify listeners that the blog posts have been updated | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> fetchById(String id) async { | ||
var doc = | ||
await FirebaseFirestore.instance.collection('blogPosts').doc(id).get(); | ||
if (doc.exists) { | ||
_blogPost = BlogModel.fromDocument(doc.data()!, doc.id); | ||
} | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> delete(String id) async { | ||
await FirebaseFirestore.instance.collection('blogPosts').doc(id).delete(); | ||
// Remove the blog post from the list | ||
_blogPosts.removeWhere((blog) => blog.id == id); | ||
// Notify listeners that the blog posts have been updated | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> createBlog(String id, BlogModel blog) async { | ||
await FirebaseFirestore.instance | ||
.collection('blogPosts') | ||
.doc(id) | ||
.set(blog.toDocument()); | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> updateBlog(String id, String description) async { | ||
var doc = | ||
await FirebaseFirestore.instance.collection('blogPosts').doc(id).get(); | ||
if (doc.exists) { | ||
BlogModel currentBlog = BlogModel.fromDocument(doc.data()!, doc.id); | ||
BlogModel updatedBlog = BlogModel( | ||
id: currentBlog.id, | ||
tags: currentBlog.tags, | ||
title: currentBlog.title, | ||
description: description, // update the description | ||
memberID: currentBlog.memberID, | ||
createdTime: currentBlog.createdTime, | ||
updatedTime: Timestamp.now(), // update the updatedTime | ||
image: currentBlog.image, | ||
); | ||
await FirebaseFirestore.instance | ||
.collection('blogPosts') | ||
.doc(id) | ||
.set(updatedBlog.toDocument()); | ||
int indexToUpdate = _blogPosts.indexWhere((blog) => blog.id == id); | ||
if (indexToUpdate != -1) { | ||
_blogPosts[indexToUpdate] = updatedBlog; | ||
} | ||
} | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.