Skip to content

Commit c2333d9

Browse files
committed
commit
1 parent ef2559b commit c2333d9

File tree

4 files changed

+81
-18
lines changed

4 files changed

+81
-18
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:firebase_auth/firebase_auth.dart';
3+
4+
class FireStoreMethods {
5+
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
6+
Future<void> followUser(String uid, String followId) async {
7+
try {
8+
DocumentSnapshot snapshot =
9+
await _firestore.collection('Users').doc(uid).get();
10+
var data = snapshot.data() as Map<String, dynamic>;
11+
List following = data['following'];
12+
13+
if (following.contains(followId)) {
14+
await _firestore.collection('Users').doc(followId).update({
15+
'followers': FieldValue.arrayRemove([uid]),
16+
});
17+
await _firestore.collection('Users').doc(uid).update({
18+
'following': FieldValue.arrayRemove([followId]),
19+
});
20+
} else {
21+
await _firestore.collection('Users').doc(followId).update({
22+
'followers': FieldValue.arrayUnion([uid]),
23+
});
24+
await _firestore.collection('Users').doc(uid).update({
25+
'following': FieldValue.arrayUnion([followId]),
26+
});
27+
}
28+
} catch (e) {
29+
print(e.toString());
30+
}
31+
}
32+
33+
Future<void> signOut() async {
34+
await FirebaseAuth.instance.signOut();
35+
}
36+
}

lib/responsive/responsive_layout.dart

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,42 @@ class ResponsiveLayout extends StatefulWidget {
1515
}
1616

1717
class _ResponsiveLayoutState extends State<ResponsiveLayout> {
18+
bool isloading = false;
1819
@override
1920
void initState() {
2021
super.initState();
2122
userData();
2223
}
2324

2425
userData() async {
26+
setState(() {
27+
isloading = true;
28+
});
2529
UserProvider userProvider = Provider.of(context, listen: false);
2630
await userProvider.refreshUserDetails();
31+
setState(() {
32+
isloading = false;
33+
});
2734
// print(3);
2835
// print(userProvider.getUser.profileUrl);
2936
}
3037

3138
@override
3239
Widget build(BuildContext context) {
33-
return LayoutBuilder(
34-
builder: (context, constraints) {
35-
if (constraints.maxWidth > 600) {
36-
// display web screen layout
37-
return widget.webScreenLayout;
38-
} else {
39-
// display mobile screen layout
40-
return widget.mobileScreenLayout;
41-
}
42-
},
43-
);
40+
return isloading
41+
? const Center(
42+
child: CircularProgressIndicator(),
43+
)
44+
: LayoutBuilder(
45+
builder: (context, constraints) {
46+
if (constraints.maxWidth > 600) {
47+
// display web screen layout
48+
return widget.webScreenLayout;
49+
} else {
50+
// display mobile screen layout
51+
return widget.mobileScreenLayout;
52+
}
53+
},
54+
);
4455
}
4556
}

lib/screens/profile.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:firebase_auth/firebase_auth.dart';
55
import 'package:flutter/material.dart';
66
import 'package:image_picker/image_picker.dart';
77
import 'package:instagram_clone/backend/providers/user_providers.dart';
8+
import 'package:instagram_clone/backend/storage/firestore_methods.dart';
89
import 'package:instagram_clone/model/user.dart';
910
// import 'package:flutter/widgets.dart';
1011
import 'package:instagram_clone/utils/colors.dart';
@@ -152,23 +153,38 @@ class _ProfileScreenState extends State<ProfileScreen> {
152153
user!.uid == widget.uid
153154
? CustomButton(
154155
function: () {},
155-
text: 'Share',
156+
text: 'Edit',
156157
backgroundcolor: mobileBackgroundColor,
157158
textColor: primaryColor)
158159
: isFollowing
159160
? CustomButton(
160-
function: () {},
161+
function: () async {
162+
await FireStoreMethods()
163+
.followUser(user!.uid, widget.uid);
164+
165+
setState(() {
166+
isFollowing = false;
167+
noOfFollowers--;
168+
});
169+
},
161170
text: 'following',
162171
backgroundcolor: Colors.white,
163172
textColor: Colors.black)
164173
: CustomButton(
165-
function: () {},
174+
function: () async {
175+
await FireStoreMethods()
176+
.followUser(user!.uid, widget.uid);
177+
setState(() {
178+
isFollowing = true;
179+
noOfFollowers++;
180+
});
181+
},
166182
text: 'follow',
167183
backgroundcolor: Colors.blue,
168184
textColor: primaryColor),
169185
CustomButton(
170186
function: () {},
171-
text: 'Edit',
187+
text: 'share',
172188
backgroundcolor: mobileBackgroundColor,
173189
textColor: primaryColor),
174190
],

lib/widgets/custom_button.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class CustomButton extends StatelessWidget {
55
final String text;
66
final Color backgroundcolor;
77
final Color textColor;
8-
final Function function;
8+
final Function() function;
99
const CustomButton(
1010
{super.key,
1111
required this.function,
@@ -16,7 +16,7 @@ class CustomButton extends StatelessWidget {
1616
@override
1717
Widget build(BuildContext context) {
1818
return InkWell(
19-
onTap: () => function,
19+
onTap: function,
2020
borderRadius: BorderRadius.circular(15),
2121
splashColor: Colors.white,
2222
child: Container(
@@ -34,7 +34,7 @@ class CustomButton extends StatelessWidget {
3434
style: TextStyle(
3535
backgroundColor: backgroundcolor,
3636
color: textColor,
37-
fontWeight: FontWeight.w800),
37+
fontWeight: FontWeight.bold),
3838
),
3939
),
4040
),

0 commit comments

Comments
 (0)