Skip to content

Commit

Permalink
Merge pull request #87 from sparcs-kaist/feature/make-vote-faster
Browse files Browse the repository at this point in the history
좋아요, 싫어요 반응속도 빠르게 하기
  • Loading branch information
sangohkim authored Feb 16, 2024
2 parents 622c081 + e1948cd commit 8b1813e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 40e3ca182cfbd571c46e751b6eb8a6f2cbbf72fc

COCOAPODS: 1.15.2
COCOAPODS: 1.14.3
91 changes: 74 additions & 17 deletions lib/pages/post_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import 'package:new_ara_app/utils/profile_image.dart';
import 'package:new_ara_app/utils/handle_hidden.dart';
import 'package:new_ara_app/widgets/snackbar_noti.dart';

// TODO: Dio 사용방식 createDioWithHeaders~ 로 변경하기

/// 하나의 post에 대한 내용 뷰, 이벤트 처리를 모두 담당하는 StatefulWidget.
class PostViewPage extends StatefulWidget {
/// 보여주고 싶은 대상 post의 id.
Expand Down Expand Up @@ -752,12 +754,23 @@ class _PostViewPageState extends State<PostViewPage> {
children: [
// 좋아요 버튼
InkWell(
// API 요청 이전에 먼저 state를 변경한 뒤에 요청 결과에 따라 처리하기
onTap: () async {
// 원래의 상태를 보관하기 위해 임시 모델 생성
ArticleModel tmpArticle = ArticleModel.fromJson(_article.toJson());
ArticleController(model: _article, userProvider: userProvider)
.setVote(true);
_updateState();
bool res = await ArticleController(
model: _article,
model: tmpArticle,
userProvider: userProvider,
).posVote();
if (res) _updateState();
debugPrint('좋아요 결과 ${res}');
if (!res) {
ArticleController(model: _article, userProvider: userProvider)
.setVote(true);
_updateState();
}
},
child: _buildVoteIcons(
true, _article.my_vote, ColorsInfo.posVote, 20.17, 22),
Expand All @@ -774,13 +787,23 @@ class _PostViewPageState extends State<PostViewPage> {
const SizedBox(width: 20),
// 싫어요 버튼
InkWell(
onTap: () {
ArticleController(
model: _article,
// API 요청 이전에 먼저 state를 변경한 뒤에 요청 결과에 따라 처리하기
onTap: () async {
// 원래의 상태를 보관하기 위해 임시 모델 생성
ArticleModel tmpArticle = ArticleModel.fromJson(_article.toJson());
ArticleController(model: _article, userProvider: userProvider)
.setVote(false);
_updateState();
bool res = await ArticleController(
model: tmpArticle,
userProvider: userProvider,
).negVote().then((result) {
if (result) _updateState();
});
).negVote();
debugPrint('싫어요 결과 ${res}');
if (!res) {
ArticleController(model: _article, userProvider: userProvider)
.setVote(false);
_updateState();
}
},
child: _buildVoteIcons(
false, _article.my_vote, ColorsInfo.negVote, 20.17, 22),
Expand Down Expand Up @@ -1332,13 +1355,30 @@ class _PostViewPageState extends State<PostViewPage> {
child: Row(
children: [
InkWell(
onTap: () {
// API 요청 이전에 먼저 state를 변경한 뒤에 요청 결과에 따라 처리하기
onTap: () async {
// 원래 모델값을 저장하기 위해 임시 모델 생성
CommentNestedCommentListActionModel
tmpCurComment =
CommentNestedCommentListActionModel
.fromJson(curComment.toJson());
CommentController(
model: curComment,
model: curComment,
userProvider: userProvider)
.setVote(true);
_updateState();
bool res = await CommentController(
model: tmpCurComment,
userProvider: userProvider,
).posVote().then((result) {
if (result) _updateState();
});
).posVote();
debugPrint('좋아요 결과 ${res}');
if (!res) {
CommentController(
model: curComment,
userProvider: userProvider)
.setVote(true);
_updateState();
}
},
child: _buildVoteIcons(true, curComment.my_vote,
ColorsInfo.posVote, 11.52, 12.57),
Expand All @@ -1356,13 +1396,30 @@ class _PostViewPageState extends State<PostViewPage> {
),
const SizedBox(width: 12),
InkWell(
// API 요청 이전에 먼저 state를 변경한 뒤에 요청 결과에 따라 처리하기
onTap: () async {
// 원래 모델값을 저장하기 위해 임시 모델 생성
CommentNestedCommentListActionModel
tmpCurComment =
CommentNestedCommentListActionModel
.fromJson(curComment.toJson());
CommentController(
model: curComment,
model: curComment,
userProvider: userProvider)
.setVote(false);
_updateState();
bool res = await CommentController(
model: tmpCurComment,
userProvider: userProvider,
).negVote().then((result) {
if (result) _updateState();
});
).negVote();
debugPrint('좋아요 결과 ${res}');
if (!res) {
CommentController(
model: curComment,
userProvider: userProvider)
.setVote(false);
_updateState();
}
},
child: _buildVoteIcons(
false,
Expand Down
63 changes: 57 additions & 6 deletions lib/providers/user_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,25 @@ class UserProvider with ChangeNotifier {
late dynamic response;
try {
response = await dio.post(totUrl, data: payload);
} catch (error) {
debugPrint("POST /api/$apiUrl failed with error: $error");
} on DioException catch (e) {
debugPrint("getApiRes failed with DioException: $e");
// 서버에서 response를 보냈지만 invalid한 statusCode일 때
if (e.response != null) {
debugPrint("${e.response!.data}");
debugPrint("${e.response!.headers}");
debugPrint("${e.response!.requestOptions}");
return e.response;
}
// request의 setting, sending에서 문제 발생
// requestOption, message를 출력.
else {
debugPrint("${e.requestOptions}");
debugPrint("${e.message}");
return null;
}
} catch (e) {
debugPrint("_fetchUser failed with error: $e");
return null;
}
return response;
}
Expand All @@ -192,8 +209,25 @@ class UserProvider with ChangeNotifier {
late dynamic response;
try {
response = await dio.delete(totUrl, data: payload);
} catch (error) {
debugPrint("DELETE /api/$apiUrl failed with error: $error");
} on DioException catch (e) {
debugPrint("getApiRes failed with DioException: $e");
// 서버에서 response를 보냈지만 invalid한 statusCode일 때
if (e.response != null) {
debugPrint("${e.response!.data}");
debugPrint("${e.response!.headers}");
debugPrint("${e.response!.requestOptions}");
return e.response;
}
// request의 setting, sending에서 문제 발생
// requestOption, message를 출력.
else {
debugPrint("${e.requestOptions}");
debugPrint("${e.message}");
return null;
}
} catch (e) {
debugPrint("_fetchUser failed with error: $e");
return null;
}
return response;
}
Expand All @@ -208,8 +242,25 @@ class UserProvider with ChangeNotifier {
late dynamic response;
try {
response = await dio.patch(totUrl, data: payload);
} catch (error) {
debugPrint("PATCH /api/$apiUrl failed with error: $error");
} on DioException catch (e) {
debugPrint("getApiRes failed with DioException: $e");
// 서버에서 response를 보냈지만 invalid한 statusCode일 때
if (e.response != null) {
debugPrint("${e.response!.data}");
debugPrint("${e.response!.headers}");
debugPrint("${e.response!.requestOptions}");
return e.response;
}
// request의 setting, sending에서 문제 발생
// requestOption, message를 출력.
else {
debugPrint("${e.requestOptions}");
debugPrint("${e.message}");
return null;
}
} catch (e) {
debugPrint("_fetchUser failed with error: $e");
return null;
}
return response;
}
Expand Down
22 changes: 9 additions & 13 deletions lib/utils/post_view_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ class ArticleController {
var cancelRes = await userProvider.postApiRes(
"articles/$id/vote_cancel/",
);
if (cancelRes.statusCode != 200) return false;
if (cancelRes == null || cancelRes.statusCode != 200) return false;
} else {
var postRes = await userProvider.postApiRes(
"articles/$id/vote_positive/",
);
if (postRes.statusCode != 200) return false;
if (postRes == null || postRes.statusCode != 200) return false;
}
_setVote(true);
return true;
}

Expand All @@ -55,21 +54,20 @@ class ArticleController {
var cancelRes = await userProvider.postApiRes(
"articles/$id/vote_cancel/",
);
if (cancelRes.statusCode != 200) return false;
if (cancelRes == null || cancelRes.statusCode != 200) return false;
} else {
var postRes = await userProvider.postApiRes(
"articles/$id/vote_negative/",
);
if (postRes.statusCode != 200) return false;
if (postRes == null || postRes.statusCode != 200) return false;
}
_setVote(false);
return true;
}

/// 멤버 변수 model 내부의
/// 좋아요, 싫어요 상태를 [value]에 맞게 한번에 업데이트.
/// value: true = 좋아요, false = 싫어요
void _setVote(bool value) {
void setVote(bool value) {
/* positive_vote_count, negative_vote_count 모두 int?
타입이므로 null일 경우 0으로 초기화함. */
model.positive_vote_count ??= 0;
Expand Down Expand Up @@ -268,14 +266,13 @@ class CommentController {
var cancelRes = await userProvider.postApiRes(
"comments/$id/vote_cancel/",
);
if (cancelRes.statusCode != 200) return false;
if (cancelRes == null || cancelRes.statusCode != 200) return false;
} else {
var postRes = await userProvider.postApiRes(
"comments/$id/vote_positive/",
);
if (postRes.statusCode != 200) return false;
if (postRes == null || postRes.statusCode != 200) return false;
}
setVote(true);
return true;
}

Expand All @@ -287,14 +284,13 @@ class CommentController {
var cancelRes = await userProvider.postApiRes(
"comments/$id/vote_cancel/",
);
if (cancelRes.statusCode != 200) return false;
if (cancelRes == null || cancelRes.statusCode != 200) return false;
} else {
var postRes = await userProvider.postApiRes(
"comments/$id/vote_negative/",
);
if (postRes.statusCode != 200) return false;
if (postRes == null || postRes.statusCode != 200) return false;
}
setVote(false);
return true;
}

Expand Down

0 comments on commit 8b1813e

Please sign in to comment.