Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 13 additions & 9 deletions lib/presentation/community/controllers/community_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class CommunityController extends GetxController {
super.onReady();
}


bool isPostLiked(int postId) {
return _cachedLikedIds.contains(postId);
}

Future<void> fetchRecommendPosts({bool refresh = false}) async {
if (refresh) {
_currentPage = 0;
Expand All @@ -77,12 +82,12 @@ class CommunityController extends GetxController {
page: _currentPage,
);

// 로컬 캐시 기반으로 isFavorite 덮어쓰기 (앱 재시작 후에도 유지)

final applied = result.content.map((post) {
if (_cachedLikedIds.contains(post.postId)) {
return post.copyWith(isFavorite: true);
}
// API가 이미 좋아요 상태를 반환한 경우 캐시에도 반영

if (post.isFavorite) {
_cachedLikedIds.add(post.postId);
}
Expand Down Expand Up @@ -127,7 +132,7 @@ class CommunityController extends GetxController {
int postId,
bool isLiked,
) async {
// 옵티미스틱 업데이트: API 결과 전에 즉시 UI 반영

_updatePostLikeInList(postId, isLiked ? 1 : -1, isLiked);

try {
Expand All @@ -137,15 +142,15 @@ class CommunityController extends GetxController {
await _unlikeUseCase(postId);
}

// 로컬 캐시 갱신 및 영속화

if (isLiked) {
_cachedLikedIds.add(postId);
} else {
_cachedLikedIds.remove(postId);
}
await _savePostLikeLocalUseCase(postId, isLiked);

// 홈 목록과 cross-sync

if (Get.isRegistered<HomeController>()) {
final post = viewPosts.firstWhereOrNull((p) => p.postId == postId);
if (post != null) {
Expand All @@ -161,7 +166,7 @@ class CommunityController extends GetxController {
'[CommunityController] 좋아요 토글 실패 - error: $e',
);

// API 실패 시 롤백

_updatePostLikeInList(postId, isLiked ? -1 : 1, !isLiked);
}
}
Expand Down Expand Up @@ -201,7 +206,7 @@ class CommunityController extends GetxController {
int likeCount,
bool isFavorite,
) async {
// 로컬 캐시 동기화 (PostViewController → CommunityController 방향)

if (isFavorite) {
_cachedLikedIds.add(postId);
} else {
Expand Down Expand Up @@ -303,8 +308,7 @@ class CommunityController extends GetxController {
}
}

class CommunityResultController
extends GetxController {
class CommunityResultController extends GetxController {
final RxList<PostContentModel> viewPosts =
<PostContentModel>[].obs;

Expand Down
120 changes: 76 additions & 44 deletions lib/presentation/home/controllers/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ class HomeController extends GetxController with BaseHomeController {
viewPostList.assignAll(_cachePostList);
}


bool isPostLiked(int postId) {
return _cachedLikedIds.contains(postId);
}

Future<void> toggleLike(int postId, bool isLiked) async {
// 옵티미스틱 업데이트

_updatePostLikeInList(postId, isLiked ? 1 : -1, isLiked);

try {
Expand All @@ -94,7 +99,7 @@ class HomeController extends GetxController with BaseHomeController {
}
await savePostLikeLocalUseCase(postId, isLiked);

// 커뮤니티 목록과 cross-sync

if (Get.isRegistered<CommunityController>()) {
final post = viewPostList.firstWhereOrNull((p) => p.postId == postId);
if (post != null) {
Expand Down Expand Up @@ -129,21 +134,48 @@ class HomeController extends GetxController with BaseHomeController {
}
}

void updatePostLike(int postId, int likeCount, bool isFavorite) {
final index = viewPostList.indexWhere((p) => p.postId == postId);
void updatePostLike(
int postId,
int likeCount,
bool isFavorite,
) {

if (isFavorite) {
_cachedLikedIds.add(postId);
} else {
_cachedLikedIds.remove(postId);
}
Comment thread
ryusuye0n marked this conversation as resolved.
Outdated

final index = viewPostList.indexWhere(
(p) => p.postId == postId,
);

if (index != -1) {
viewPostList[index] = viewPostList[index].copyWith(
final updatedPost =
viewPostList[index].copyWith(
likeCount: likeCount,
isFavorite: isFavorite,
);
viewPostList.refresh();

viewPostList[index] = updatedPost;


viewPostList.assignAll(
List<PostEntity>.from(viewPostList),
Comment thread
ryusuye0n marked this conversation as resolved.
Outdated
);
Comment thread
ryusuye0n marked this conversation as resolved.
Outdated
}
final cacheIndex = _cachePostList.indexWhere((p) => p.postId == postId);

final cacheIndex =
_cachePostList.indexWhere(
(p) => p.postId == postId,
);

if (cacheIndex != -1) {
_cachePostList[cacheIndex] = _cachePostList[cacheIndex].copyWith(
likeCount: likeCount,
isFavorite: isFavorite,
);
_cachePostList[cacheIndex] =
_cachePostList[cacheIndex].copyWith(
likeCount: likeCount,
isFavorite: isFavorite,
);
}
}

Expand Down Expand Up @@ -183,57 +215,57 @@ class HomeSearchResultController extends GetxController
with BaseHomeController {
///홈 검색 결과 업데이트
void updateResult(
Iterable<PostEntity> posts,
Iterable<UserEntity> profiles,
) {
Iterable<PostEntity> posts,
Iterable<UserEntity> profiles,
) {
viewUserList.assignAll(profiles);
viewPostList.assignAll(posts);
}
}

//TODO : 임시 데이터 삭제
typedef HomeRecentPopularPostInfo = ({
int postId,
String title,
Duration creatAt,
int favorites,
bool isFavorite,
int postId,
String title,
Duration creatAt,
int favorites,
bool isFavorite,
});
typedef HomeProfileInfo = ({String name, String skill, int rating});
typedef PostInfo = ({
int postId,
List<String> skills,
String title,
String name,
int favoites,
int bookmarks,
DateTime createAt,
bool isBookmark,
bool isFavorite,
int postId,
List<String> skills,
String title,
String name,
int favoites,
int bookmarks,
DateTime createAt,
bool isBookmark,
bool isFavorite,
});

List<HomeRecentPopularPostInfo> _getRanks() => [
for (int i = 1; i < 5; i++) ...{
(
postId: i + 1,
title: "요즘 공부 어케 하시나요 다들",
creatAt: Duration(days: 3),
favorites: 160 * Random().nextInt(i),
isFavorite: i % 2 == 0,
postId: i + 1,
title: "요즘 공부 어케 하시나요 다들",
creatAt: Duration(days: 3),
favorites: 160 * Random().nextInt(i),
isFavorite: i % 2 == 0,
),
(
postId: i + 10,
title: "10년차 개발자는 무슨 공부할까",
creatAt: Duration(days: 5),
favorites: 121 * Random().nextInt(i),
isFavorite: i % 2 == 0,
postId: i + 10,
title: "10년차 개발자는 무슨 공부할까",
creatAt: Duration(days: 5),
favorites: 121 * Random().nextInt(i),
isFavorite: i % 2 == 0,
),
(
postId: i + 120,
title: "팀장 퇴사해서 디자인빵꾸남",
creatAt: Duration(days: 2),
favorites: 73 * Random().nextInt(i),
isFavorite: i % 2 == 0,
postId: i + 120,
title: "팀장 퇴사해서 디자인빵꾸남",
creatAt: Duration(days: 2),
favorites: 73 * Random().nextInt(i),
isFavorite: i % 2 == 0,
),
},
];
];
60 changes: 36 additions & 24 deletions lib/presentation/home/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,42 @@ class HomeScreen extends GetView<HomeController> {
AppGap.v16,
HomeProfileList(title: "커피챗 추천", controller: controller),
AppGap.v16,
PostGridList(
title: "추천 게시물",
list: controller.viewPostList
.map(
(post) => PostItem(
postId: post.postId,
skills: post.tags,
title: post.title,
author: post.authorName,
bookmarks: post.bookmarkCount,
favorites: post.likeCount,
createAt: post.createAt,
bookmarkAction: (isBookmark, total) {
//TODO : 북마크 api 개발 이후 구현
},
heartAction: (isFavorite, total) {
controller.toggleLike(post.postId, isFavorite);
},
initialBookmark: false,
initialFavorite: post.isFavorite,
isMy: true,
),
)
.toList(),
Obx(
() => PostGridList(
title: "추천 게시물",
list: controller.viewPostList
.map(
(post) => PostItem(
postId: post.postId,
skills: post.tags,
title: post.title,
author: post.authorName,
bookmarks: post.bookmarkCount,
favorites: post.likeCount,
createAt: post.createAt,
bookmarkAction: (
isBookmark,
total,
) {
// TODO : 북마크 api 개발 이후 구현
},
heartAction: (
isFavorite,
total,
) {
controller.toggleLike(
post.postId,
isFavorite,
);
},
initialBookmark: false,
initialFavorite:
post.isFavorite,
isMy: true,
),
)
.toList(),
),
),
AppGap.v16,
],
Expand Down
Loading