From 3e5641b3c2553f2db4bf23f73f9ce37d21e262bf Mon Sep 17 00:00:00 2001 From: exttex Date: Sun, 16 Aug 2020 22:17:22 +0200 Subject: [PATCH] Missing scrolling, homepage reload, sections page --- lib/api/deezer.dart | 13 ++++++++- lib/api/definitions.dart | 14 ++++++++-- lib/api/definitions.g.dart | 4 +++ lib/api/player.dart | 7 ++--- lib/main.dart | 6 ++++- lib/ui/home_screen.dart | 55 ++++++++++++++++++++++++++------------ lib/ui/search.dart | 37 ++++++++++++++++++------- 7 files changed, 102 insertions(+), 34 deletions(-) diff --git a/lib/api/deezer.dart b/lib/api/deezer.dart index d5743bd..92b3102 100644 --- a/lib/api/deezer.dart +++ b/lib/api/deezer.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:dio/dio.dart'; import 'package:cookie_jar/cookie_jar.dart'; import 'package:dio_cookie_manager/dio_cookie_manager.dart'; @@ -29,6 +31,7 @@ class DeezerAPI { "Accept-Language": "${settings.deezerLanguage??"en"}-${settings.deezerCountry??'US'},${settings.deezerLanguage??"en"};q=0.9,en-US;q=0.8,en;q=0.7", "Connection": "keep-alive" }; + Future _authorizing; CookieJar _cookieJar = new CookieJar(); @@ -75,8 +78,16 @@ class DeezerAPI { return response.data; } + //Wrapper so it can be globally awaited + Future authorize() async { + if (_authorizing == null) { + this._authorizing = this._authorize(); + } + return _authorizing; + } + //Authorize, bool = success - Future authorize() async { + Future _authorize() async { try { Map data = await callApi('deezer.getUserData'); if (data['results']['USER']['USER_ID'] == 0) { diff --git a/lib/api/definitions.dart b/lib/api/definitions.dart index 7faced5..bc7b7f6 100644 --- a/lib/api/definitions.dart +++ b/lib/api/definitions.dart @@ -553,14 +553,24 @@ class HomePageSection { String title; HomePageSectionLayout layout; + //For loading more items + String pagePath; + bool hasMore; + @JsonKey(fromJson: _homePageItemFromJson, toJson: _homePageItemToJson) List items; - HomePageSection({this.layout, this.items, this.title}); + HomePageSection({this.layout, this.items, this.title, this.pagePath, this.hasMore}); //JSON factory HomePageSection.fromPrivateJson(Map json) { - HomePageSection hps = HomePageSection(title: json['title'], items: []); + HomePageSection hps = HomePageSection( + title: json['title'], + items: [], + pagePath: json['target'], + hasMore: json['hasMoreItems']??false + ); + String layout = json['layout']; //No ads there if (layout == 'ads') return null; diff --git a/lib/api/definitions.g.dart b/lib/api/definitions.g.dart index 8d840b8..3d36a2d 100644 --- a/lib/api/definitions.g.dart +++ b/lib/api/definitions.g.dart @@ -273,6 +273,8 @@ HomePageSection _$HomePageSectionFromJson(Map json) { _$enumDecodeNullable(_$HomePageSectionLayoutEnumMap, json['layout']), items: HomePageSection._homePageItemFromJson(json['items']), title: json['title'] as String, + pagePath: json['pagePath'] as String, + hasMore: json['hasMore'] as bool, ); } @@ -280,6 +282,8 @@ Map _$HomePageSectionToJson(HomePageSection instance) => { 'title': instance.title, 'layout': _$HomePageSectionLayoutEnumMap[instance.layout], + 'pagePath': instance.pagePath, + 'hasMore': instance.hasMore, 'items': HomePageSection._homePageItemToJson(instance.items), }; diff --git a/lib/api/player.dart b/lib/api/player.dart index dbf4078..2bf7bde 100644 --- a/lib/api/player.dart +++ b/lib/api/player.dart @@ -62,12 +62,12 @@ class PlayerHelper { await AudioService.start( backgroundTaskEntrypoint: backgroundTaskEntrypoint, androidEnableQueue: true, - androidStopForegroundOnPause: false, + androidStopForegroundOnPause: true, androidNotificationOngoing: false, androidNotificationClickStartsActivity: true, androidNotificationChannelDescription: 'Freezer', androidNotificationChannelName: 'Freezer', - androidNotificationIcon: 'drawable/ic_logo' + androidNotificationIcon: 'drawable/ic_logo', ); } @@ -470,6 +470,7 @@ class AudioPlayerTask extends BackgroundAudioTask { @override Future onClose() async { + print('onClose'); await onStop(); } @@ -478,7 +479,7 @@ class AudioPlayerTask extends BackgroundAudioTask { _player.stop(); if (_eventSub != null) _eventSub.cancel(); - super.onStop(); + await super.onStop(); } //Get queue save file path diff --git a/lib/main.dart b/lib/main.dart index de73e8d..b3f2ff7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -43,10 +43,14 @@ class _FreezerAppState extends State { void initState() { //Make update theme global updateTheme = _updateTheme; - super.initState(); } + @override + void dispose() { + super.dispose(); + } + void _updateTheme() { setState(() { settings.themeData; diff --git a/lib/ui/home_screen.dart b/lib/ui/home_screen.dart index 2d54e2d..755432e 100644 --- a/lib/ui/home_screen.dart +++ b/lib/ui/home_screen.dart @@ -22,18 +22,6 @@ class HomeScreen extends StatelessWidget { ], ), ); - - /* - return ListView( - children: [ - Padding( - padding: EdgeInsets.symmetric(vertical: 16.0), - child: FreezerTitle(), - ), - HomePageScreen() - ], - ); - */ } } @@ -104,7 +92,7 @@ class _HomePageScreenState extends State { } catch (e) {} //On background load from API try { - if (settings.offlineMode) return; + if (settings.offlineMode) await deezerAPI.authorize(); HomePage _hp = await deezerAPI.homePage(); if (_hp != null) { if (_cancel) return; @@ -148,12 +136,14 @@ class _HomePageScreenState extends State { @override Widget build(BuildContext context) { if (_homePage == null) - return Center(child: CircularProgressIndicator(),); + return Center(child: Padding( + padding: EdgeInsets.all(8.0), + child: CircularProgressIndicator(), + )); if (_error) return ErrorScreen(); return ListView.builder( shrinkWrap: true, - addAutomaticKeepAlives: true, physics: NeverScrollableScrollPhysics(), itemCount: _homePage.sections.length, itemBuilder: (context, i) { @@ -179,7 +169,36 @@ class _HomePageScreenState extends State { SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( - children: List.generate(section.items.length, (i) { + children: List.generate(section.items.length + 1, (i) { + //Has more items + if (i == section.items.length) { + if (section.hasMore??false) { + return FlatButton( + child: Text( + 'Show more', + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 20.0 + ), + ), + onPressed: () => Navigator.of(context).push(MaterialPageRoute( + builder: (context) => Scaffold( + appBar: AppBar( + title: Text(section.title), + ), + body: SingleChildScrollView( + child: HomePageScreen( + channel: DeezerChannel(target: section.pagePath) + ) + ), + ), + )), + ); + } + return Container(height: 0, width: 0); + } + + //Show item HomePageItem item = section.items[i]; return HomePageItemWidget(item); }), @@ -255,7 +274,9 @@ class HomePageItemWidget extends StatelessWidget { Navigator.of(context).push(MaterialPageRoute( builder: (context) => Scaffold( appBar: AppBar(title: Text(item.value.title.toString()),), - body: HomePageScreen(channel: item.value,), + body: SingleChildScrollView( + child: HomePageScreen(channel: item.value,) + ), ) )); }, diff --git a/lib/ui/search.dart b/lib/ui/search.dart index ae534b5..c6b8a17 100644 --- a/lib/ui/search.dart +++ b/lib/ui/search.dart @@ -19,6 +19,7 @@ class _SearchScreenState extends State { String _query; bool _offline = false; + TextEditingController _controller = new TextEditingController(); void _submit(BuildContext context, {String query}) { if (query != null) _query = query; @@ -52,17 +53,33 @@ class _SearchScreenState extends State { child: Row( children: [ Expanded( - child: TextField( - onChanged: (String s) => _query = s, - decoration: InputDecoration( - labelText: 'Search' - ), - onSubmitted: (String s) => _submit(context, query: s), - ), + child: Stack( + alignment: Alignment(1.0, 1.0), + children: [ + TextField( + onChanged: (String s) => _query = s, + decoration: InputDecoration( + labelText: 'Search' + ), + controller: _controller, + onSubmitted: (String s) => _submit(context, query: s), + ), + IconButton( + icon: Icon(Icons.clear), + onPressed: () { + _controller.clear(); + }, + ), + ], + ) ), - IconButton( - icon: Icon(Icons.search), - onPressed: () => _submit(context), + + Padding( + padding: EdgeInsets.fromLTRB(0, 8, 0, 0), + child: IconButton( + icon: Icon(Icons.search), + onPressed: () => _submit(context), + ), ) ], ),