Skip to content

Commit

Permalink
feat:#35 add pagination + search api 검색
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwest00 committed Oct 17, 2023
1 parent 709a7f6 commit 4d3fcfc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ import 'package:match/util/method/get_storage.dart';
import '../../../model/enum/search_statu.dart';
import '../../../model/search/search.dart';
import '../../../model/today_project/today_project.dart';
import '../../../provider/api/project_api.dart';
import '../../../provider/api/search_api.dart';
import '../../../provider/api/util/global_api_field.dart';
import '../../../util/const/style/global_logger.dart';

class DonationSearchController extends GetxController {
//검색 필드 controller
Rx<TextEditingController> searchTextController = TextEditingController().obs;


//최근 검색어 위젯 활성화 여부
Rx<SEARCH_STATUS> searchStatus = SEARCH_STATUS.INIT.obs;

///* 검색 결과 리스트
RxList<TodayProject> projectList = <TodayProject>[].obs;
RxInt totalSearchLength = 0.obs;

///* 추천 검색어 리스트
RxList<String> recommendSearchList = <String>[].obs;
RxList<String> recommendSearchList = List.generate(10, (index) => "").obs;

///* 아래 함수에서 사용하는 1초를 측정하는 Timer
Timer? _timer;
Expand Down Expand Up @@ -56,6 +60,28 @@ class DonationSearchController extends GetxController {
});
}

Future<void> getSearchList({required String content}) async {
projectList.assignAll(await ProjectApi.getProjectList(content: content));
totalSearchLength.value = ProjectApi.project.totalCnt;

searchStatus.value = SEARCH_STATUS.SEARCH;
}

Future<void> getMoreSearchList(
{required String content, required int index}) async {
logger.d(
"2: 총 페이지수 : ${ProjectApi.project.totalCnt ~/ PAGINATION_SIZE}, 불러오고자 하는 페이지: ${index}");
if (!(ProjectApi.project.totalCnt ~/ PAGINATION_SIZE >= index) &&
!ProjectApi.project.isLast) {
ProjectApi.project.currentpage = index;
searchStatus.value = SEARCH_STATUS.SEARCH;
projectList.addAll(await ProjectApi.getProjectList(
getMore: true,
content: content,
));
}
}

@override
Future<void> dispose() async {
searchTextController.value.dispose();
Expand Down
87 changes: 49 additions & 38 deletions lib/modules/donation_search/view/donation_search_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import 'package:match/util/method/get_storage.dart';
import '../../../model/enum/search_statu.dart';
import '../../../model/recent_search/recent_search.dart';
import '../../../provider/api/search_api.dart';
import '../../../provider/api/util/global_api_field.dart';
import '../../../util/components/gloabl_text_field.dart';
import '../../../util/const/global_variable.dart';
import '../../../util/const/style/global_color.dart';
import '../../../util/const/style/global_logger.dart';
import '../../../util/const/style/global_text_styles.dart';
import '../../donate/widget/donate_widget.dart';
import '../controller/donation_search_controller.dart';
Expand All @@ -39,9 +41,7 @@ class DonationSearchScreen extends GetView<DonationSearchController> {
textStatus: controller.searchStatus,
suffixOnTap: () async {},
onSubmitted: (value) async {
//TODO: add search api
// controller.projectList.addAll(
// await SearchApi.getSearchResult(content: value));
await controller.getSearchList(content: value);
},
onChanged: (value) async {}),
),
Expand Down Expand Up @@ -76,8 +76,14 @@ class DonationSearchScreen extends GetView<DonationSearchController> {
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
recentSearchList(startIdx: 1,keywords: controller.recommendSearchList.sublist(0,5)),
recentSearchList(startIdx: 6,keywords: controller.recommendSearchList.sublist(5,10)),
recentSearchList(
startIdx: 1,
keywords: controller.recommendSearchList
.sublist(0, 5)),
recentSearchList(
startIdx: 6,
keywords: controller.recommendSearchList
.sublist(5, 10)),
],
),
],
Expand All @@ -88,7 +94,7 @@ class DonationSearchScreen extends GetView<DonationSearchController> {
? Padding(
padding: EdgeInsets.only(left: 33.w),
child: Text(
"총 ${controller.projectList.length}개의 검색결과",
"총 ${controller.totalSearchLength.value}개의 검색결과",
style: AppTextStyles.T1Bold13.copyWith(
color: AppColors.grey5,
fontWeight: FontWeight.w600),
Expand All @@ -98,42 +104,48 @@ class DonationSearchScreen extends GetView<DonationSearchController> {
SizedBox(
height: 20.h,
),
// 검색 field 밑의 contents
//*4.프로젝트 리스트
controller.searchStatus.value == SEARCH_STATUS.SEARCH
? controller.searchStatus.value == SEARCH_STATUS.SEARCH
? Expanded(
child: ListView.separated(
shrinkWrap: true,
separatorBuilder: (context, index) =>
SizedBox(height: 14.h),
itemCount: controller.projectList.length,
itemBuilder: (context, index) {
final project = controller.projectList[index];
return Container(
padding: EdgeInsets.symmetric(horizontal: 20.w),
margin: EdgeInsets.only(
bottom:
index == controller.projectList.length - 1

Expanded(
child: // 검색 field 밑의 contents
//*4.프로젝트 리스트
controller.searchStatus.value == SEARCH_STATUS.SEARCH
? controller.projectList.isNotEmpty
? ListView.builder(
shrinkWrap: true,
itemCount: controller.projectList.length,
itemBuilder: (context, index) {
if (index % (PAGINATION_SIZE - 1) == 0 &&
index != 0) {
logger.d("1. getMoreFlame 호출!");
Future.wait({
controller.getMoreSearchList(
content: controller
.searchTextController.value.text,
index: index ~/ (PAGINATION_SIZE - 1))
});
}
final project = controller.projectList[index];
return Container(
padding:
EdgeInsets.symmetric(horizontal: 20.w,vertical: 8.h).copyWith(top: 0),
margin: EdgeInsets.only(
bottom: index ==
controller.projectList.length - 1
? 14.h
: 0.h),
child: ProjectWidget(project: project));
},
),
)
: Center(
child: Text(
"최근 검색 결과가 없습니다",
style: AppTextStyles.T1Bold18,
),
)
: SizedBox.shrink(),
child: ProjectWidget(project: project));
},
)
: Center(child: emptyWidget())
: const SizedBox.shrink(),
)
],
),
));
}

Widget recentSearchList({required int startIdx, required List<String> keywords }) {
Widget recentSearchList(
{required int startIdx, required List<String> keywords}) {
return Wrap(
direction: Axis.vertical,
spacing: 12.h,
Expand All @@ -147,10 +159,9 @@ class DonationSearchScreen extends GetView<DonationSearchController> {

Widget _recentSearchItem({required int priority, required String keyword}) {
return GestureDetector(
onTap: (){
onTap: () async {
controller.searchTextController.value.text = keyword;
//TODO: add search api
controller.searchStatus.value = SEARCH_STATUS.SEARCH;
await controller.getSearchList(content: keyword);
},
child: SizedBox(
width: 120.w,
Expand Down
7 changes: 6 additions & 1 deletion lib/provider/api/project_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:dio/dio.dart';
import 'package:match/model/enum/project_type.dart';
import 'package:match/model/project_detail/project_detail.dart';
import 'package:match/model/project_history.dart/project_history.dart';
import 'package:match/modules/donate/view/donate_view.dart';
import 'package:match/provider/api/util/dio_services.dart';
import 'package:match/provider/api/util/global_api_field.dart';

Expand All @@ -15,6 +16,8 @@ class ProjectApi {
static Pagination projectHistory = Pagination(isLast: false, totalCnt: 0);

///<h2>3-5 API | 프로젝트 리스트</h2>
///* search탭에서 공통으로 사용
///* [DonateScreen] , [DonationSearchScreen] 에서 사용
///* pagination
static Future<List<TodayProject>> getProjectList({
ProjectType? type,
Expand All @@ -23,7 +26,6 @@ class ProjectApi {
}) async {
if (!getMore) {
project.currentpage = 0;

}
var queryParameters = {
"page": project.currentpage,
Expand All @@ -33,6 +35,9 @@ class ProjectApi {
if (type != null) {
queryParameters['projectKind'] = type.name;
}
if (content != null) {
queryParameters['content'] = content;
}
try {
Response response = await DioServices()
.to()
Expand Down

0 comments on commit 4d3fcfc

Please sign in to comment.