Skip to content

Commit

Permalink
Implemented Search in Bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishq5414 committed Mar 22, 2023
1 parent 196ffeb commit 276841c
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies

Large diffs are not rendered by default.

130 changes: 34 additions & 96 deletions lib/features/bookmarks/bookmarks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ import 'package:zoom_tap_animation/zoom_tap_animation.dart';

List<String> BookmarkSearchList = [];

class BookmarksPage extends ConsumerWidget {
class BookmarksPage extends ConsumerStatefulWidget {
const BookmarksPage({super.key});

@override
Widget build(BuildContext context, ref) {
ConsumerState<BookmarksPage> createState() => _BookmarksPageState();
}

class _BookmarksPageState extends ConsumerState<BookmarksPage> {

@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
final user = ref.watch(userProvider);
final allnotes = ref.read(notesDataProvider);
List<Notes> allnoteslist;
var userbookmarklist = user?.bid ?? [''];
List bookmarks = [];

// print(userbookmarklist);
List getBookmarks() {
allnotes.when(
data: (notes) {
Expand Down Expand Up @@ -96,35 +100,32 @@ class BookmarksPage extends ConsumerWidget {
body: SingleChildScrollView(
child: Column(
children: [
// Container(
// height: size.height * 0.05,
// width: size.width * 0.9,
// margin: EdgeInsets.symmetric(horizontal: size.width * 0.035),
// decoration: BoxDecoration(
// color: appBackgroundColor,
// borderRadius: BorderRadius.circular(2),
// ),
// child: InkWell(
// onTap: () {
// showSearch(
// context: context,
// delegate: CustomBookmarkSearchDelegate(),
// );
// },
// child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// children: const [
// Text('Search in bookmarks',
// style: TextStyle(
// color: appGreyColor,
// fontSize: 15,
// fontWeight: FontWeight.bold)),
// Spacer(),
// Icon(Icons.search, color: appGreyColor),
// ],
// ),
// ),
// ),
Container(
height: size.height * 0.05,
width: size.width * 0.9,
margin: EdgeInsets.symmetric(horizontal: size.width * 0.035),
decoration: BoxDecoration(
color: appBackgroundColor,
borderRadius: BorderRadius.circular(2),
),
child: InkWell(
onTap: () {
Routemaster.of(context).push('/bookmarksearchpage');
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: const [
Text('Search in bookmarks',
style: TextStyle(
color: appGreyColor,
fontSize: 15,
fontWeight: FontWeight.bold)),
Spacer(),
Icon(Icons.search, color: appGreyColor),
],
),
),
),
Padding(
padding: EdgeInsets.only(
top: size.height * 0.05,
Expand Down Expand Up @@ -160,66 +161,3 @@ class BookmarksPage extends ConsumerWidget {
}
}

class CustomBookmarkSearchDelegate extends SearchDelegate {
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
onPressed: () {
query = '';
},
icon: const Icon(Icons.clear))
];
}

@override
Widget buildLeading(BuildContext context) {
return IconButton(
onPressed: () {
close(context, null);
},
icon: const Icon(Icons.arrow_back));
}

@override
Widget buildResults(BuildContext context) {
List<String> matchQuery = [];
for (var scourse in BookmarkSearchList) {
if (scourse.toLowerCase().contains(query.toLowerCase())) {
matchQuery.add(scourse);
}
}
return ListView.builder(
itemCount: matchQuery.length,
itemBuilder: (context, index) {
var result = matchQuery[index];
return ListTile(
title: Text(
result,
style: const TextStyle(color: Colors.white),
),
);
});
}

@override
Widget buildSuggestions(BuildContext context) {
List<String> matchQuery = [];
for (String scourse in BookmarkSearchList) {
if (scourse.contains(query.toLowerCase())) {
matchQuery.add(scourse);
}
}
return ListView.builder(
itemCount: matchQuery.length,
itemBuilder: (context, index) {
var result = matchQuery[index];
return ListTile(
title: Text(
result,
style: const TextStyle(color: Colors.white),
),
);
});
}
}
220 changes: 220 additions & 0 deletions lib/features/bookmarks/bookmarks_search.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// ignore_for_file: unused_import, prefer_is_empty

import 'package:companion_rebuild/core/provider/notes_provider.dart';
import 'package:companion_rebuild/modal/notes_modal.dart';
import 'package:companion_rebuild/theme/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_octicons/flutter_octicons.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:routemaster/routemaster.dart';
import 'package:zoom_tap_animation/zoom_tap_animation.dart';

import '../auth/controller/auth_controller.dart';

class BookmarksSearchPage extends ConsumerStatefulWidget {
const BookmarksSearchPage({Key? key}) : super(key: key);

@override
// ignore: library_private_types_in_public_api
_SearchState createState() => _SearchState();
}

class _SearchState extends ConsumerState<BookmarksSearchPage> {
List<Notes> foundedNotes = [];
List<Notes> _notes = [];

List<Notes> getBookmarks(AsyncValue<List<Notes>> allnotes, user) {
List<Notes> bookmarks = [];
List<Notes> allnoteslist;
var userbookmarklist = user?.bid ?? [''];
allnotes.when(
data: (notes) {
allnoteslist = notes;
// print(allnoteslist);
for (var i = 0; i < userbookmarklist.length; i++) {
for (var j = 0; j < allnoteslist.length; j++) {
if (userbookmarklist[i].toString() ==
allnoteslist[j].id.toString()) {
bookmarks.add(allnoteslist[j]);
}
}
}
bookmarks = List.from(bookmarks.reversed);
return bookmarks;
},
error: (error, stackTrace) => Text(error.toString()),
loading: () => const CircularProgressIndicator());
return bookmarks;
}

@override
void initState() {
super.initState();

WidgetsBinding.instance.addPostFrameCallback((_) {
// ignore: todo
// TODO: implement initState
final allnotes = ref.read(notesDataProvider);
var user = ref.watch(userProvider);
_notes = getBookmarks(allnotes, user);
print(_notes);
setState(() {
foundedNotes = _notes;
});
});
}

onSearch(String search) {
setState(() {
final allnotes = ref.read(notesDataProvider);
var user = ref.watch(userProvider);
_notes = getBookmarks(allnotes, user);
foundedNotes = _notes
.where((notes) =>
notes.name.toLowerCase().contains(search) |
notes.course.toLowerCase().contains(search))
.toList();
});
}

@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final allnotes = ref.read(notesDataProvider);
var user = ref.watch(userProvider);
List<Notes> allnoteslist;
var userbookmarklist = user?.bid ?? [''];
List bookmarks = [];
List getBookmarks() {
allnotes.when(
data: (notes) {
allnoteslist = notes;
// print(allnoteslist);
for (var i = 0; i < userbookmarklist.length; i++) {
for (var j = 0; j < allnoteslist.length; j++) {
if (userbookmarklist[i].toString() ==
allnoteslist[j].id.toString()) {
bookmarks.add(allnoteslist[j]);
}
}
}
bookmarks = new List.from(bookmarks.reversed);
return bookmarks;
},
error: (error, stackTrace) => Text(error.toString()),
loading: () => const CircularProgressIndicator());
return bookmarks;
}

var bookmarkslist = getBookmarks();
return SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 30),
child: Scaffold(
appBar: AppBar(
leading: ZoomTapAnimation(
child: IconButton(
icon: const Icon(
OctIcons.arrow_left_16,
size: 15,
),
onPressed: () => Routemaster.of(context).history.back())),
elevation: 0,
backgroundColor: appBackgroundColor,
title: Container(
height: 30,
decoration: BoxDecoration(
color: Colors.grey[800],
borderRadius: BorderRadius.circular(2)),
child: TextField(
autofocus: true,
style: const TextStyle(color: appWhiteColor),
onChanged: (value) => onSearch(value.toLowerCase()),
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey[800],
contentPadding: const EdgeInsets.all(0),
prefixIcon: const Icon(
OctIcons.search_16,
size: 20,
color: appWhiteColor,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
borderSide: BorderSide.none),
hintStyle:
const TextStyle(fontSize: 14, color: appWhiteColor),
hintText: "Search in your library"),
),
),
),
body: Container(
margin: EdgeInsets.only(top: size.width * 0.05),
color: appBackgroundColor,
child: foundedNotes.length > 0
? ListView.builder(
itemCount: foundedNotes.length > 15 ? 15 : foundedNotes.length,
itemBuilder: (context, index) {
return notesComponent(notes: foundedNotes[index]);
})
: const Center(
child: Text(
"No notes found",
style: TextStyle(
color: appWhiteColor,
),
)),
),
),
),
);
}

notesComponent({required Notes notes}) {
return InkWell(
onTap: () => Routemaster.of(context).push('/pdfview', queryParameters: {
'id': notes.id.toString(),
'name': notes.name,
'year': notes.year,
'branch': notes.branch,
'course': notes.course,
'semester': notes.semester,
'version': notes.version,
'unit': notes.unit,
'wdlink': notes.wdlink,
}),
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20),
padding: const EdgeInsets.only(top: 10, bottom: 10),
child: Row(
children: [
ZoomTapAnimation(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
notes.name,
style: const TextStyle(color: appWhiteColor, fontSize: 16),
),
const SizedBox(
height: 5,
),
Text(
notes.course,
style: const TextStyle(color: appGreyColor, fontSize: 14),
),
],
),
),
Spacer(),
Text(
'${notes.unit} Unit',
style: const TextStyle(color: appGreyColor, fontSize: 14),
),
],
),
),
);
}
}
2 changes: 2 additions & 0 deletions lib/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'features/auth/screens/loginEmail/login_email.dart';
import 'features/auth/screens/send_email_verification.dart';
import 'features/auth/screens/signUpEmail/signup_getemail.dart';
import 'features/auth/screens/signUpEmail/signup_getpassword.dart';
import 'features/bookmarks/bookmarks_search.dart';
import 'features/notesView/notes_view.dart';

final loggedOutPages = RouteMap(
Expand Down Expand Up @@ -68,6 +69,7 @@ final loggedInPages = RouteMap(
'/premium': (_) => const MaterialPage(child: PremiumPage()),
'/privacypolicy': (_) => const MaterialPage(child: PrivacyPolicyPage()),
'/changepassword': (_) => const MaterialPage(child: ChangePasswordPage()),
'/bookmarksearchpage': (_) => const MaterialPage(child: BookmarksSearchPage()),
},
);

0 comments on commit 276841c

Please sign in to comment.