generated from jwson-automation/blueberry_template
-
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.
Co-authored-by: Jungwoo <108061510+jwson-automation@users.noreply.github.com>
- Loading branch information
1 parent
2da5e49
commit c5b12ef
Showing
17 changed files
with
449 additions
and
196 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
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
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
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
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
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
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
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,73 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
final dislikeProvider = | ||
StateNotifierProvider<DislikeNotifier, Map<String, bool>>((ref) { | ||
return DislikeNotifier(); | ||
}); | ||
|
||
class DislikeNotifier extends StateNotifier<Map<String, bool>> { | ||
DislikeNotifier() : super({}); | ||
|
||
// 싫어요 상태를 가져오기 | ||
Future<void> fetchDislikeStatus(String postID, String userID) async { | ||
final dislikeDoc = await FirebaseFirestore.instance | ||
.collection('posts') | ||
.doc(postID) | ||
.collection('dislikes') | ||
.doc(userID) | ||
.get(); | ||
|
||
if (dislikeDoc.exists) { | ||
state = { | ||
...state, | ||
postID: true, | ||
}; | ||
} else { | ||
state = { | ||
...state, | ||
postID: false, | ||
}; | ||
} | ||
} | ||
|
||
// 싫어요 토글 기능 | ||
Future<void> toggleDislike(String postID, String userID) async { | ||
final postRef = FirebaseFirestore.instance.collection('posts').doc(postID); | ||
final dislikeRef = postRef.collection('dislikes').doc(userID); | ||
|
||
final dislikeDoc = await dislikeRef.get(); | ||
final isDisliked = dislikeDoc.exists; | ||
|
||
if (isDisliked) { | ||
// 이미 싫어요 상태라면 싫어요 취소 | ||
await dislikeRef.delete(); | ||
state = { | ||
...state, | ||
postID: false, | ||
}; | ||
} else { | ||
// 싫어요 추가 | ||
await dislikeRef.set({ | ||
'createdAt': Timestamp.now(), | ||
'postID': postID, | ||
'userID': userID, | ||
}); | ||
state = { | ||
...state, | ||
postID: true, | ||
}; | ||
} | ||
|
||
// 싫어요 카운트 업데이트 | ||
await _updateDislikesCount(postRef, isDisliked ? -1 : 1); | ||
} | ||
|
||
// 싫어요 카운트 업데이트 기능 | ||
Future<void> _updateDislikesCount( | ||
DocumentReference postRef, int dislikeChange) async { | ||
await postRef.update({ | ||
'dislikesCount': FieldValue.increment(dislikeChange), | ||
}); | ||
} | ||
} |
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,73 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
final likeProvider = | ||
StateNotifierProvider<LikeNotifier, Map<String, bool>>((ref) { | ||
return LikeNotifier(); | ||
}); | ||
|
||
class LikeNotifier extends StateNotifier<Map<String, bool>> { | ||
LikeNotifier() : super({}); | ||
|
||
// 좋아요 상태를 가져오기 | ||
Future<void> fetchLikeStatus(String postID, String userID) async { | ||
final likeDoc = await FirebaseFirestore.instance | ||
.collection('posts') | ||
.doc(postID) | ||
.collection('likes') | ||
.doc(userID) | ||
.get(); | ||
|
||
if (likeDoc.exists) { | ||
state = { | ||
...state, | ||
'postID': true, | ||
}; | ||
} else { | ||
state = { | ||
...state, | ||
'postID': false, | ||
}; | ||
} | ||
} | ||
|
||
// 좋아요 토글 기능 | ||
Future<void> toggleLike(String postID, String userID) async { | ||
final postRef = FirebaseFirestore.instance.collection('posts').doc(postID); | ||
final likeRef = postRef.collection('likes').doc(userID); | ||
|
||
final likeDoc = await likeRef.get(); | ||
final isLiked = likeDoc.exists; | ||
|
||
if (isLiked) { | ||
// 이미 좋아요 상태라면 좋아요 취소 | ||
await likeRef.delete(); | ||
state = { | ||
...state, | ||
postID: false, | ||
}; | ||
} else { | ||
// 좋아요 추가 | ||
await likeRef.set({ | ||
'createdAt': Timestamp.now(), | ||
'postID': postID, | ||
'userID': userID, | ||
}); | ||
state = { | ||
...state, | ||
postID: true, | ||
}; | ||
} | ||
|
||
// 좋아요 카운트 업데이트 | ||
await _updateLikesCount(postRef, isLiked ? -1 : 1); | ||
} | ||
|
||
// 좋아요 카운트 업데이트 기능 | ||
Future<void> _updateLikesCount( | ||
DocumentReference postRef, int likeChange) async { | ||
await postRef.update({ | ||
'likesCount': FieldValue.increment(likeChange), | ||
}); | ||
} | ||
} |
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
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,28 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:firebase_storage/firebase_storage.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:blueberry_flutter_template/model/PostUserInfoModel.dart'; | ||
|
||
final postUserInfoProvider = | ||
FutureProvider.family<PostUserInfoModel, String>((ref, userID) async { | ||
final firestore = FirebaseFirestore.instance; | ||
|
||
final userDoc = await firestore.collection('users_test').doc(userID).get(); | ||
|
||
if (userDoc.exists) { | ||
final data = userDoc.data()!; | ||
final userName = data['name'] as String; | ||
final imageName = data['imageName'] as String; | ||
|
||
final storageRef = | ||
FirebaseStorage.instance.ref().child('profileimage/$imageName'); | ||
final profileImageUrl = await storageRef.getDownloadURL(); | ||
|
||
return PostUserInfoModel( | ||
name: userName, | ||
profileImageUrl: profileImageUrl, | ||
); | ||
} else { | ||
throw Exception('User data not found for userID: $userID'); | ||
} | ||
}); |
Oops, something went wrong.