Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/bulletin search page separating consonants vowels #90

Merged
merged 7 commits into from
Feb 18, 2024
147 changes: 99 additions & 48 deletions lib/pages/bulletin_search_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:new_ara_app/constants/board_type.dart';
Expand Down Expand Up @@ -31,7 +33,6 @@ class _BulletinSearchPageState extends State<BulletinSearchPage> {
bool _isLoadingNextPage = false;
String _apiUrl = "";
String _hintText = "";
String _searchWord = "";
final ScrollController _scrollController = ScrollController();
final FocusNode _focusNode = FocusNode();
final TextEditingController _textEdtingController = TextEditingController();
Expand Down Expand Up @@ -102,8 +103,6 @@ class _BulletinSearchPageState extends State<BulletinSearchPage> {
final UserProvider userProvider = context.read<UserProvider>();
final Map<String, dynamic>? myMap = await userProvider
.getApiRes("${_apiUrl}1&main_search__contains=$targetWord");
debugPrint(
"refreshPostList : myMap : ${myMap?["results"].length} {$targetWord}");
if (mounted && targetWord == _textEdtingController.text) {
setState(() {
postPreviewList.clear();
Expand All @@ -126,7 +125,7 @@ class _BulletinSearchPageState extends State<BulletinSearchPage> {
void _scrollListener() async {
//스크롤 시 포커스 해제
FocusScope.of(context).unfocus();
if (_searchWord == "") {
if (_textEdtingController.text == "") {
if (mounted) {
setState(() {
postPreviewList.clear();
Expand All @@ -148,7 +147,7 @@ class _BulletinSearchPageState extends State<BulletinSearchPage> {
_currentPage = _currentPage + 1;
//TODO: 더 이상 불러올 게시물이 없을 때의 처리
Map<String, dynamic>? myMap = await userProvider.getApiRes(
"$_apiUrl$_currentPage&main_search__contains=$_searchWord");
"$_apiUrl$_currentPage&main_search__contains=${_textEdtingController.text}");
if (mounted) {
setState(() {
for (int i = 0; i < (myMap!["results"].length ?? 0); i++) {
Expand Down Expand Up @@ -223,18 +222,16 @@ class _BulletinSearchPageState extends State<BulletinSearchPage> {
controller: _textEdtingController,
onSubmitted: (String text) {
setState(() {
_textEdtingController.text = text;
_searchWord = text;
_isLoading = true;
});
refreshPostList(text);
},
onChanged: (String text) {
setState(() {
_textEdtingController.text = text;
_searchWord = text;
_debouncer.run(() {
debugPrint(
"bulletin_search_page: onChanged(${DateTime.now().toString()}) : $text");
refreshPostList(text);
});
refreshPostList(text);
},
style: const TextStyle(
//height * fontSize = line height(커서 크기)
Expand Down Expand Up @@ -311,49 +308,103 @@ class _BulletinSearchPageState extends State<BulletinSearchPage> {
child: Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 18,
child: ListView.builder(
controller: _scrollController,
itemCount: postPreviewList.length +
(_isLoadingNextPage ? 1 : 0), // 아이템 개수
itemBuilder: (BuildContext context, int index) {
// 각 아이템을 위한 위젯 생성

// 숨겨진 게시물이면 일단 표현 안하는 걸로 함.
if (_isLoadingNextPage &&
index == postPreviewList.length) {
return const SizedBox(
height: 63,
child: Column(
children: [
// 검색어가 없을 때와 검색 결과가 없을 때의 처리
if (_textEdtingController.text == "" &&
postPreviewList.isEmpty)
const SizedBox(
height: 100,
child: Center(
child: LoadingIndicator(),
child: Text(
"검색어를 입력해주세요.",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w500),
),
),
);
}
return postPreviewList[index].is_hidden
? Container()
: InkWell(
onTap: () {
Navigator.of(context).push(slideRoute(
PostViewPage(
id: postPreviewList[index].id)));
},
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(11.0),
child: PostPreview(
model: postPreviewList[index]),
),
Container(
height: 1,
color: const Color(0xFFF0F0F0),
),
],
));
},
),
// 검색어가 있는데 검색 결과가 없을 때의 처리
if (_textEdtingController.text != "" &&
postPreviewList.isEmpty)
const SizedBox(
height: 100,
child: Center(
child: Text(
"검색 결과가 없습니다.",
style: TextStyle(
color: ColorsInfo.newara,
fontSize: 16,
fontWeight: FontWeight.w500),
),
),
),

Expanded(
child: ListView.builder(
controller: _scrollController,
itemCount: postPreviewList.length +
(_isLoadingNextPage ? 1 : 0), // 아이템 개수
itemBuilder: (BuildContext context, int index) {
// 각 아이템을 위한 위젯 생성

// 숨겨진 게시물이면 일단 표현 안하는 걸로 함.
skykhs3 marked this conversation as resolved.
Show resolved Hide resolved
if (_isLoadingNextPage &&
index == postPreviewList.length) {
return const SizedBox(
height: 63,
child: Center(
child: LoadingIndicator(),
),
);
}
return postPreviewList[index].is_hidden
skykhs3 marked this conversation as resolved.
Show resolved Hide resolved
? Container()
: InkWell(
onTap: () {
Navigator.of(context).push(slideRoute(
PostViewPage(
id: postPreviewList[index].id)));
},
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(11.0),
child: PostPreview(
model: postPreviewList[index]),
),
Container(
height: 1,
color: const Color(0xFFF0F0F0),
),
],
));
},
),
),
],
),
),
),
),
);
}
}

class Debouncer {
skykhs3 marked this conversation as resolved.
Show resolved Hide resolved
final int milliseconds;
VoidCallback? action;
Timer? _timer;

Debouncer({required this.milliseconds});

run(VoidCallback action) {
if (_timer != null) {
_timer!.cancel();
}
_timer = Timer(Duration(milliseconds: milliseconds), action);
}
}

final _debouncer = Debouncer(milliseconds: 5);
2 changes: 0 additions & 2 deletions lib/providers/user_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ class UserProvider with ChangeNotifier {
late dynamic response;
try {
response = await dio.get(totUrl);
debugPrint(
"GET $totUrl success: ${response.data.toString().substring(0, min(300, response.data.toString().length))}");
} on DioException catch (e) {
debugPrint("getApiRes failed with DioException: $e");
// 서버에서 response를 보냈지만 invalid한 statusCode일 때
Expand Down
Loading