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] follow and unfollow users #9

Merged
merged 1 commit into from
Mar 17, 2021
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
34 changes: 29 additions & 5 deletions frontend/lib/screens/main/profile/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class _ProfileState extends State<Profile> {
final String uid = ModalRoute.of(context).settings.arguments;
return MultiProvider(
providers: [
StreamProvider.value(
value: _userService.isFollowing(
FirebaseAuth.instance.currentUser.uid, uid),
),
StreamProvider.value(
value: _postService.getPostsByUser(uid),
),
Expand Down Expand Up @@ -65,11 +69,31 @@ class _ProfileState extends State<Profile> {
.profileImageUrl),
)
: Icon(Icons.person, size: 50),
FlatButton(
onPressed: () {
Navigator.pushNamed(context, '/edit');
},
child: Text("Edit Profile"))
if (FirebaseAuth.instance.currentUser.uid ==
uid)
TextButton(
onPressed: () {
Navigator.pushNamed(context, '/edit');
},
child: Text("Edit Profile"))
else if (FirebaseAuth
.instance.currentUser.uid !=
uid &&
!Provider.of<bool>(context))
TextButton(
onPressed: () {
_userService.followUser(uid);
},
child: Text("Follow"))
else if (FirebaseAuth
.instance.currentUser.uid !=
uid &&
Provider.of<bool>(context))
TextButton(
onPressed: () {
_userService.unfollowUser(uid);
},
child: Text("Unfollow")),
]),
Align(
alignment: Alignment.centerLeft,
Expand Down
44 changes: 44 additions & 0 deletions frontend/lib/services/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,50 @@ class UserService {
.map(_userListFromQuerySnapshot);
}

Stream<bool> isFollowing(uid, otherId) {
return FirebaseFirestore.instance
.collection("users")
.doc(uid)
.collection("following")
.doc(otherId)
.snapshots()
.map((snapshot) {
return snapshot.exists;
});
}

Future<void> followUser(uid) async {
await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser.uid)
.collection('following')
.doc(uid)
.set({});

await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('followers')
.doc(FirebaseAuth.instance.currentUser.uid)
.set({});
}

Future<void> unfollowUser(uid) async {
await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser.uid)
.collection('following')
.doc(uid)
.delete();

await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('followers')
.doc(FirebaseAuth.instance.currentUser.uid)
.delete();
}

Future<void> updateProfile(
File _bannerImage, File _profileImage, String name) async {
String bannerImageUrl = '';
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-nullsafety.1"
version: "2.5.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.3"
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
version: "1.1.0"
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-nullsafety.3"
version: "1.15.0"
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-nullsafety.1"
version: "1.2.0"
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.2"
version: "0.6.3"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10-nullsafety.1"
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
version: "1.3.0"
nested:
dependency: transitive
description:
Expand All @@ -246,7 +246,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0-nullsafety.1"
version: "1.8.0"
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-nullsafety.2"
version: "1.8.0"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0-nullsafety.1"
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19-nullsafety.2"
version: "0.2.19"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.3"
version: "2.1.0"
sdks:
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.20.0 <2.0.0"
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.20.0"