Skip to content

Commit

Permalink
fix: #35 future 반환타입 고려 및 api 호출 조건문 수정, 현재 페이지 변수 통일
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwest00 committed Oct 17, 2023
1 parent 180baf5 commit e1db0d3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 53 deletions.
4 changes: 2 additions & 2 deletions lib/model/api/pagination.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
///* Pagination 처리에 사용되는 변수 data class
class Pagination {
bool isLast;
int totalPage;
int totalCnt;
int currentpage;

Pagination(
{required this.isLast, required this.totalPage, this.currentpage = 0});
{required this.isLast, required this.totalCnt, this.currentpage = 0});
}
10 changes: 6 additions & 4 deletions lib/modules/home/controller/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import 'package:match/provider/api/util/global_api_field.dart';

import '../../../model/flame/flame.dart';
import '../../../model/today_project/today_project.dart';
import '../../../util/const/style/global_logger.dart';

class HomeController extends GetxController {
//임시 변순
Rx<bool> isLike = false.obs;
Rx<int> adCount = 2.obs;
Rx<String> tmpText = ProjectType.ANIMAL.stateName.obs;
RxList<Flame> flameList = <Flame>[].obs;
Rx<int> currentPage = 0.obs;

Future<void> getMoreFlame(int index) async {
currentPage.value++;
if (!(FlameApi.burningFlame.totalPage > currentPage.value) &&
logger.d(
"2: 총 페이지수 : ${FlameApi.burningFlame.totalCnt ~/ PAGINATION_SIZE}, 불러오고자 하는 페이지: ${FlameApi.burningFlame.currentpage + 1}");
if (!(FlameApi.burningFlame.totalCnt ~/ PAGINATION_SIZE >=
FlameApi.burningFlame.currentpage + 1) &&
!FlameApi.burningFlame.isLast) {
FlameApi.burningFlame.currentpage++;
flameList.addAll(await FlameApi.getBurningFlameList(getMore: true));
}
}
Expand All @@ -28,6 +31,5 @@ class HomeController extends GetxController {
void onInit() async {
super.onInit();
flameList.assignAll(await FlameApi.getBurningFlameList());
currentPage.value = FlameApi.burningFlame.currentpage;
}
}
10 changes: 8 additions & 2 deletions lib/modules/home/view/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:match/util/const/style/global_text_styles.dart';

import '../../../provider/api/util/global_api_field.dart';
import '../../../util/components/global_widget.dart';
import '../../../util/const/style/global_logger.dart';
import '../controller/home_controller.dart';
import '../widget/home_widget.dart';

Expand Down Expand Up @@ -113,8 +114,13 @@ class HomeScreen extends GetView<HomeController> {
aspectRatio: 255.w / 320.h,
viewportFraction: 1),
itemBuilder: (context, index, realIndex) {
if (index % PAGINATION_SIZE-1 == 0 && index != 0) {
controller.getMoreFlame(index);
logger.d(index);
if (index % (PAGINATION_SIZE - 1) == 0 && index != 0) {
logger.d("1. getMoreFlame 호출!");
Future.wait({
controller
.getMoreFlame(index ~/ (PAGINATION_SIZE - 1))
});
}
//TODO: pagination 처리
final flame = controller.flameList[index];
Expand Down
70 changes: 35 additions & 35 deletions lib/modules/home/view/today_match_view.dart
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:match/modules/home/controller/home_controller.dart';
import 'package:match/util/components/global_app_bar.dart';
import '../../../model/today_project/today_project.dart';
import '../../donate/widget/donate_widget.dart';

///<h2>오늘의 후원 화면</h2>
///[HomeController]에서 [TodayProject]를 받아와서 화면에 뿌려준다.</br></br>
///[HomeController]에서 [HomeSceen]이 그려질때 해당 데이터를 가져오기때문에
/// 같은 Controller및 Binding 사용
class TodayMatchScreen extends GetView<HomeController> {
const TodayMatchScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CommonAppBar.basic("오늘의 후원"),
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 7.h),
child: Obx(
() => ListView.separated(
separatorBuilder: (context, index) => SizedBox(height: 14.h),
itemCount: controller.projectList.length,
itemBuilder: (context, index) {
final project = controller.projectList[index];
return ProjectWidget(project: project);
},
),
),
),
);
}
}
// import 'package:flutter/material.dart';
// import 'package:flutter_screenutil/flutter_screenutil.dart';
// import 'package:get/get.dart';
// import 'package:match/modules/home/controller/home_controller.dart';
// import 'package:match/util/components/global_app_bar.dart';
// import '../../../model/today_project/today_project.dart';
// import '../../donate/widget/donate_widget.dart';
//
// ///<h2>오늘의 후원 화면</h2>
// ///[HomeController]에서 [TodayProject]를 받아와서 화면에 뿌려준다.</br></br>
// ///[HomeController]에서 [HomeSceen]이 그려질때 해당 데이터를 가져오기때문에
// /// 같은 Controller및 Binding 사용
// class TodayMatchScreen extends GetView<HomeController> {
// const TodayMatchScreen({super.key});
//
// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: CommonAppBar.basic("오늘의 후원"),
// body: Padding(
// padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 7.h),
// child: Obx(
// () => ListView.separated(
// separatorBuilder: (context, index) => SizedBox(height: 14.h),
// itemCount: controller.projectList.length,
// itemBuilder: (context, index) {
// final project = controller.projectList[index];
// return ProjectWidget(project: project);
// },
// ),
// ),
// ),
// );
// }
// }
15 changes: 6 additions & 9 deletions lib/provider/api/flame_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@ import 'util/dio_services.dart';

class FlameApi {
///* flameList pagination 추가 호출 판별 함수
static Pagination burningFlame = Pagination(isLast: false, totalPage: 0);
static Pagination burningFlame = Pagination(isLast: false, totalCnt: 0);

///<h2>5-12 API | 홈 - 고유 불꽃이 조회</h2>
static Future<List<Flame>> getBurningFlameList({bool getMore = false}) async {
try {
//호출횟수에 따라 currentPage 증가
if (getMore) {
burningFlame.currentpage++;
} else {
//최초 호출이면 currentPage 초기화
logger.d("api호출 성공");
if (!getMore) {
burningFlame.currentpage = 0;
}

Response response = await DioServices()
.to()
.get("/donations/burning-flame", queryParameters: {
"page": burningFlame.currentpage,
"size": PAGINATION_SIZE
});

burningFlame.totalPage = response.data[RESULT][TOTAL] ~/ PAGINATION_SIZE;
burningFlame.totalCnt = response.data[RESULT][TOTAL];
burningFlame.isLast = response.data[RESULT][LAST];

logger.d(
"pagination 정보: totalCnt:${burningFlame.totalCnt}, currentPage:${burningFlame.currentpage} isLast:${burningFlame.isLast}");
return List.generate(response.data[RESULT][CONTENTS].length,
(index) => Flame.fromJson(response.data[RESULT][CONTENTS][index]));
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/provider/api/util/global_api_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ const String LAST = "isLast";
const String TOTAL = "totalCnt";
const String CONTENTS = "contents";
///* pagination 기본 호출 단위
const int PAGINATION_SIZE = 10;
const int PAGINATION_SIZE = 5;

0 comments on commit e1db0d3

Please sign in to comment.