Skip to content

Commit

Permalink
search feature
Browse files Browse the repository at this point in the history
  • Loading branch information
PriyanshuPz committed Jun 5, 2024
1 parent 88efb1c commit 5ae2bfd
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 26 deletions.
8 changes: 5 additions & 3 deletions lib/apis/song_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ class SongAPI extends ISongAPI {
@override
FutureEither<List<SongModel>> fetchSearchData({required String query}) async {
try {
final uri =
Uri.https(Constants.serverUrl, 'api/search/songs', {"query": query});
final uri = Uri.https(Constants.serverUrl, 'api/search/songs', {
"query": query,
"limit": "24",
});
final res = await http.get(uri);
if (res.statusCode != 200) throw Error();

Map<String, dynamic> jsonMap = jsonDecode(res.body);
// Extract the list of songs from the Map
List<dynamic> songsObj = jsonMap['data']['songs']['results'];
List<dynamic> songsObj = jsonMap['data']['results'];
List<SongModel> songs =
songsObj.map((song) => SongModel.fromMap(song)).toList();
return right(songs);
Expand Down
19 changes: 13 additions & 6 deletions lib/functions/search/controllers/search_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ final searchControllerProvider =
);
});

final searchDataProvider = FutureProvider<List<SongModel>>((ref) async {
return ref.watch(searchControllerProvider.notifier).searchData;
});

class SearchController extends StateNotifier<bool> {
final SongAPI _songAPI;

Expand All @@ -18,14 +22,17 @@ class SearchController extends StateNotifier<bool> {

// ADD SEARCH METHODS

Future<List<SongModel>> searchSong({required String query}) async {
List<SongModel> songs = [];
List<SongModel> searchData = [];

Future<void> searchSong({required String query}) async {
state = true;
final fetchedsongs = await _songAPI.fetchSearchData(query: query);

fetchedsongs.fold((l) {
throw Error.throwWithStackTrace(l.message, l.stackTrace);
}, (r) => songs = r);
fetchedsongs.fold(
(l) => throw Error.throwWithStackTrace(l.message, l.stackTrace),
(r) => searchData = r,
);

return songs;
state = false;
}
}
47 changes: 41 additions & 6 deletions lib/functions/search/views/search_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:saavn/core/core.dart';
import 'package:saavn/functions/player/controllers/player_controller.dart';
import 'package:saavn/functions/search/controllers/search_controller.dart';
import 'package:saavn/functions/search/widgets/searchbar.dart';
import 'package:saavn/functions/search/widgets/song_tile.dart';

class SearchView extends ConsumerStatefulWidget {
const SearchView({super.key});
Expand All @@ -12,19 +16,50 @@ class SearchView extends ConsumerStatefulWidget {
class _SearchViewState extends ConsumerState<SearchView> {
final TextEditingController _searchController = TextEditingController();

void search(String q) async {
await ref.watch(searchControllerProvider.notifier).searchSong(query: q);
ref.invalidate(searchDataProvider);
}

@override
Widget build(BuildContext context) {
return Column(
children: [
BaseSearchBar(
controller: _searchController,
onChanged: (value) {},
onSubmit: (value) {},
onPressed: () => search(_searchController.text),
onSubmit: (value) => search(value),
),
const Expanded(
child: Center(
child: Text('Search results will be displayed here!'),
),
Expanded(
child: (ref.watch(searchDataProvider).when(
skipLoadingOnRefresh: false,
data: (songs) {
if (songs.isEmpty) {
return const Center(
child: Text('Search results will be displayed here!'),
);
}

return ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: [
for (final song in songs)
SearchSongTile(
song: song,
onTap: () => ref
.read(playerControllerProvider.notifier)
.setSong(song: song),
),
const Center(
child: Text(
'Only 24 results because this feature is in test phase.'),
)
],
);
},
error: (error, st) => ErrorPage(error: error.toString()),
loading: () => const Loader(),
)),
),
],
);
Expand Down
9 changes: 4 additions & 5 deletions lib/functions/search/widgets/searchbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import 'package:flutter/material.dart';
class BaseSearchBar extends StatelessWidget {
final TextEditingController controller;
final void Function(String)? onSubmit;
final void Function(String)? onChanged;
final void Function()? onPressed;

const BaseSearchBar({
super.key,
required this.controller,
required this.onSubmit,
required this.onChanged,
required this.onPressed,
});

@override
Expand All @@ -18,7 +18,7 @@ class BaseSearchBar extends StatelessWidget {
decoration: BoxDecoration(
border: Border.all(
width: 2,
color: Theme.of(context).primaryColorDark.withOpacity(.2),
color: Theme.of(context).primaryColorLight.withOpacity(.2),
),
borderRadius: BorderRadius.circular(25),
),
Expand All @@ -30,13 +30,12 @@ class BaseSearchBar extends StatelessWidget {
border: InputBorder.none,
contentPadding: const EdgeInsets.all(10),
suffixIcon: IconButton(
onPressed: () {},
onPressed: onPressed,
icon: const Icon(Icons.search),
),
),
controller: controller,
onSubmitted: onSubmit,
onChanged: onChanged,
),
);
}
Expand Down
22 changes: 22 additions & 0 deletions lib/functions/search/widgets/song_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter/material.dart';
import 'package:saavn/models/song_model.dart';

class SearchSongTile extends StatelessWidget {
final SongModel song;
final VoidCallback onTap;
const SearchSongTile({super.key, required this.song, required this.onTap});

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(song.name),
subtitle: Text("${song.label} - ${song.year}"),
leading: CircleAvatar(
radius: 25,
backgroundColor: Theme.of(context).primaryColorDark,
foregroundImage: NetworkImage(song.image[0].url),
),
onTap: onTap,
);
}
}
12 changes: 6 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -268,26 +268,26 @@ packages:
dependency: "direct main"
description:
name: just_audio
sha256: "5abfab1d199e01ab5beffa61b3e782350df5dad036cb8c83b79fa45fc656614e"
sha256: b7cb6bbf3750caa924d03f432ba401ec300fd90936b3f73a9b33d58b1e96286b
url: "https://pub.dev"
source: hosted
version: "0.9.38"
version: "0.9.37"
just_audio_platform_interface:
dependency: transitive
description:
name: just_audio_platform_interface
sha256: "0243828cce503c8366cc2090cefb2b3c871aa8ed2f520670d76fd47aa1ab2790"
sha256: c3dee0014248c97c91fe6299edb73dc4d6c6930a2f4f713579cd692d9e47f4a1
url: "https://pub.dev"
source: hosted
version: "4.3.0"
version: "4.2.2"
just_audio_web:
dependency: transitive
description:
name: just_audio_web
sha256: "0edb481ad4aa1ff38f8c40f1a3576013c3420bf6669b686fe661627d49bc606c"
sha256: "134356b0fe3d898293102b33b5fd618831ffdc72bb7a1b726140abdf22772b70"
url: "https://pub.dev"
source: hosted
version: "0.4.11"
version: "0.4.9"
just_audio_windows:
dependency: "direct main"
description:
Expand Down

0 comments on commit 5ae2bfd

Please sign in to comment.