-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from priyanshuverma-dev/feat-search
[Feat] search function
- Loading branch information
Showing
14 changed files
with
253 additions
and
43 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:saavn/apis/song_api.dart'; | ||
import 'package:saavn/models/song_model.dart'; | ||
|
||
final searchControllerProvider = | ||
StateNotifierProvider<SearchController, bool>((ref) { | ||
return SearchController( | ||
songAPI: ref.watch(songAPIProvider), | ||
); | ||
}); | ||
|
||
final searchDataProvider = FutureProvider<List<SongModel>>((ref) async { | ||
return ref.watch(searchControllerProvider.notifier).searchData; | ||
}); | ||
|
||
class SearchController extends StateNotifier<bool> { | ||
final SongAPI _songAPI; | ||
|
||
SearchController({required SongAPI songAPI}) | ||
: _songAPI = songAPI, | ||
super(false); | ||
|
||
// ADD SEARCH METHODS | ||
|
||
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) => searchData = r, | ||
); | ||
|
||
state = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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}); | ||
|
||
@override | ||
ConsumerState<ConsumerStatefulWidget> createState() => _SearchViewState(); | ||
} | ||
|
||
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, | ||
onPressed: () => search(_searchController.text), | ||
onSubmit: (value) => search(value), | ||
), | ||
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(), | ||
)), | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class BaseSearchBar extends StatelessWidget { | ||
final TextEditingController controller; | ||
final void Function(String)? onSubmit; | ||
final void Function()? onPressed; | ||
|
||
const BaseSearchBar({ | ||
super.key, | ||
required this.controller, | ||
required this.onSubmit, | ||
required this.onPressed, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
decoration: BoxDecoration( | ||
border: Border.all( | ||
width: 2, | ||
color: Theme.of(context).primaryColorLight.withOpacity(.2), | ||
), | ||
borderRadius: BorderRadius.circular(25), | ||
), | ||
margin: const EdgeInsets.all(16), | ||
padding: const EdgeInsets.symmetric(horizontal: 7), | ||
child: TextField( | ||
decoration: InputDecoration( | ||
hintText: 'Search', | ||
border: InputBorder.none, | ||
contentPadding: const EdgeInsets.all(10), | ||
suffixIcon: IconButton( | ||
onPressed: onPressed, | ||
icon: const Icon(Icons.search), | ||
), | ||
), | ||
controller: controller, | ||
onSubmitted: onSubmit, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:audio_session/audio_session.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:system_theme/system_theme.dart'; | ||
import 'package:window_manager/window_manager.dart'; | ||
|
||
Future<void> initialiseAppFunctions() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
await SystemTheme.accentColor.load(); | ||
await initWindowManager(); | ||
final session = await AudioSession.instance; | ||
await session.configure(const AudioSessionConfiguration.speech()); | ||
} | ||
|
||
Future<void> initWindowManager() async { | ||
await windowManager.ensureInitialized(); | ||
|
||
WindowOptions windowOptions = const WindowOptions( | ||
center: true, | ||
title: "Saavn Music Desktop", | ||
); | ||
windowManager.waitUntilReadyToShow(windowOptions, () async { | ||
await windowManager.show(); | ||
await windowManager.focus(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.