Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add] feed added #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Add] feed added
  • Loading branch information
SimCoderYoutube committed Mar 20, 2021
commit 7187f1c0ba6abd2721a0e0f8d6272c259426902e
9 changes: 8 additions & 1 deletion frontend/lib/screens/home/feed.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:twitter/screens/main/posts/list.dart';
import 'package:twitter/services/posts.dart';

class Feed extends StatefulWidget {
Feed({Key key}) : super(key: key);
Expand All @@ -8,8 +11,12 @@ class Feed extends StatefulWidget {
}

class FeedState extends State<Feed> {
PostService _postService = PostService();
@override
Widget build(BuildContext context) {
return Text('feed');
return FutureProvider.value(
value: _postService.getFeed(),
child: Scaffold(body: ListPosts()),
);
}
}
1 change: 0 additions & 1 deletion frontend/lib/screens/main/posts/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class _ListPostsState extends State<ListPosts> {
final posts = Provider.of<List<PostModel>>(context) ?? [];

return ListView.builder(
physics: const NeverScrollableScrollPhysics(),
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
Expand Down
33 changes: 33 additions & 0 deletions frontend/lib/services/posts.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:developer';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:twitter/models/post.dart';
import 'package:twitter/services/user.dart';
import 'package:quiver/iterables.dart';

class PostService {
List<PostModel> _postListFromSnapshot(QuerySnapshot snapshot) {
Expand Down Expand Up @@ -29,4 +33,33 @@ class PostService {
.snapshots()
.map(_postListFromSnapshot);
}

Future<List<PostModel>> getFeed() async {
List<String> usersFollowing = await UserService() //['uid1', 'uid2']
.getUserFollowing(FirebaseAuth.instance.currentUser.uid);

var splitUsersFollowing = partition<dynamic>(usersFollowing, 10);
inspect(splitUsersFollowing);

List<PostModel> feedList = [];

for (int i = 0; i < splitUsersFollowing.length; i++) {
inspect(splitUsersFollowing.elementAt(i));
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('posts')
.where('creator', whereIn: splitUsersFollowing.elementAt(i))
.orderBy('timestamp', descending: true)
.get();

feedList.addAll(_postListFromSnapshot(querySnapshot));
}

feedList.sort((a, b) {
var adate = a.timestamp;
var bdate = b.timestamp;
return bdate.compareTo(adate);
});

return feedList;
}
}
11 changes: 11 additions & 0 deletions frontend/lib/services/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ class UserService {
.map(_userFromFirebaseSnapshot);
}

Future<List<String>> getUserFollowing(uid) async {
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('following')
.get();

final users = querySnapshot.docs.map((doc) => doc.id).toList();
return users;
}

Stream<List<UserModel>> queryByName(search) {
return FirebaseFirestore.instance
.collection("users")
Expand Down
42 changes: 21 additions & 21 deletions frontend/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.5.0-nullsafety.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.0-nullsafety.1"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.0-nullsafety.3"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.0-nullsafety.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.0-nullsafety.1"
cloud_firestore:
dependency: "direct main"
description:
Expand Down Expand Up @@ -63,7 +63,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.15.0-nullsafety.3"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -91,7 +91,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.0-nullsafety.1"
firebase_auth:
dependency: "direct main"
description:
Expand Down Expand Up @@ -218,21 +218,21 @@ packages:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
version: "0.6.2"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.10-nullsafety.1"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.0-nullsafety.3"
nested:
dependency: transitive
description:
Expand All @@ -246,7 +246,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.0-nullsafety.1"
pedantic:
dependency: transitive
description:
Expand Down Expand Up @@ -286,56 +286,56 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.0-nullsafety.2"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0"
version: "1.10.0-nullsafety.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.0-nullsafety.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.0-nullsafety.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.0-nullsafety.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.2.19-nullsafety.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.0-nullsafety.3"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.0-nullsafety.3"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.20.0"
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.20.0 <2.0.0"
2 changes: 0 additions & 2 deletions frontend/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ dependencies:
cloud_firestore: ^0.16.0
firebase_storage: ^7.0.0
image_picker: ^0.6.7+21


# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
Expand Down