Skip to content

Commit

Permalink
follow
Browse files Browse the repository at this point in the history
  • Loading branch information
Alireza Mohammadian authored and Alireza Mohammadian committed Apr 1, 2024
1 parent 9874ef5 commit 3bc929b
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 65 deletions.
41 changes: 39 additions & 2 deletions lib/data/firebase_service/firestor.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ffi';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_instagram_clone/data/model/usermodel.dart';
Expand Down Expand Up @@ -28,11 +30,11 @@ class Firebase_Firestor {
return true;
}

Future<Usermodel> getUser() async {
Future<Usermodel> getUser({String? UID}) async {
try {
final user = await _firebaseFirestore
.collection('users')
.doc(_auth.currentUser!.uid)
.doc(UID != null ? UID : _auth.currentUser!.uid)
.get();
final snapuser = user.data()!;
return Usermodel(
Expand Down Expand Up @@ -133,4 +135,39 @@ class Firebase_Firestor {
}
return res;
}

Future<void> flollow({
required String uid,
}) async {
DocumentSnapshot snap = await _firebaseFirestore
.collection('users')
.doc(_auth.currentUser!.uid)
.get();
List following = (snap.data()! as dynamic)['following'];
try {
if (following.contains(uid)) {
_firebaseFirestore
.collection('users')
.doc(_auth.currentUser!.uid)
.update({
'following': FieldValue.arrayRemove([uid])
});
await _firebaseFirestore.collection('users').doc(uid).update({
'followers': FieldValue.arrayRemove([_auth.currentUser!.uid])
});
} else {
_firebaseFirestore
.collection('users')
.doc(_auth.currentUser!.uid)
.update({
'following': FieldValue.arrayUnion([uid])
});
_firebaseFirestore.collection('users').doc(uid).update({
'followers': FieldValue.arrayUnion([_auth.currentUser!.uid])
});
}
} on Exception catch (e) {
print(e.toString());
}
}
}
155 changes: 109 additions & 46 deletions lib/screen/explore.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_instagram_clone/screen/post_screen.dart';
import 'package:flutter_instagram_clone/screen/profile_screen.dart';
import 'package:flutter_instagram_clone/util/image_cached.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
Expand All @@ -15,6 +17,7 @@ class ExploreScreen extends StatefulWidget {
class _ExploreScreenState extends State<ExploreScreen> {
final search = TextEditingController();
final FirebaseFirestore _firebaseFirestore = FirebaseFirestore.instance;
bool show = true;
@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -24,57 +27,108 @@ class _ExploreScreenState extends State<ExploreScreen> {
child: CustomScrollView(
slivers: [
SearchBox(),
StreamBuilder(
stream: _firebaseFirestore.collection('posts').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SliverToBoxAdapter(
child: Center(
child: CircularProgressIndicator(),
if (show)
StreamBuilder<QuerySnapshot>(
stream: _firebaseFirestore.collection('posts').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SliverToBoxAdapter(
child: Center(
child: CircularProgressIndicator(),
),
);
}
return SliverGrid(
delegate: SliverChildBuilderDelegate(
(context, index) {
final snap = snapshot.data!.docs[index];
return GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostScreen(
snap.data(),
),
),
);
},
child: Container(
decoration: BoxDecoration(
color: Colors.grey,
),
child: CachedImage(
snap['postImage'],
),
),
);
},
childCount: snapshot.data!.docs.length,
),
gridDelegate: SliverQuiltedGridDelegate(
crossAxisCount: 3,
mainAxisSpacing: 3,
crossAxisSpacing: 3,
pattern: const [
QuiltedGridTile(2, 1),
QuiltedGridTile(2, 2),
QuiltedGridTile(1, 1),
QuiltedGridTile(1, 1),
QuiltedGridTile(1, 1),
],
),
);
}
return SliverGrid(
delegate: SliverChildBuilderDelegate(
(context, index) {
final snap = snapshot.data!.docs[index];
return GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostScreen(
snap.data(),
},
),
if (!show)
StreamBuilder(
stream: _firebaseFirestore
.collection('users')
.where('username', isGreaterThanOrEqualTo: search.text)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SliverToBoxAdapter(
child: Center(child: CircularProgressIndicator()));
}
return SliverPadding(
padding:
EdgeInsets.symmetric(horizontal: 15.w, vertical: 5.h),
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
final snap = snapshot.data!.docs[index];
return Column(
children: [
SizedBox(height: 10.h),
GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
ProfileScreen(Uid: snap.id),
));
},
child: Row(
children: [
CircleAvatar(
radius: 23.r,
backgroundImage: NetworkImage(
snap['profile'],
),
),
SizedBox(width: 15.w),
Text(snap['username']),
],
),
),
),
],
);
},
child: Container(
decoration: BoxDecoration(
color: Colors.grey,
),
child: CachedImage(
snap['postImage'],
),
),
);
},
childCount: snapshot.data!.docs.length,
),
gridDelegate: SliverQuiltedGridDelegate(
crossAxisCount: 3,
mainAxisSpacing: 3,
crossAxisSpacing: 3,
pattern: const [
QuiltedGridTile(2, 1),
QuiltedGridTile(2, 2),
QuiltedGridTile(1, 1),
QuiltedGridTile(1, 1),
QuiltedGridTile(1, 1),
],
),
);
},
),
childCount: snapshot.data!.docs.length,
),
),
);
},
)
],
),
),
Expand Down Expand Up @@ -105,6 +159,15 @@ class _ExploreScreenState extends State<ExploreScreen> {
SizedBox(width: 10.w),
Expanded(
child: TextField(
onChanged: (value) {
setState(() {
if (value.length > 0) {
show = false;
} else {
show = true;
}
});
},
controller: search,
decoration: const InputDecoration(
hintText: 'Search User',
Expand Down
Loading

0 comments on commit 3bc929b

Please sign in to comment.