-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_category.dart
191 lines (135 loc) · 5.88 KB
/
spotify_category.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import 'package:flutter/material.dart';
import 'package:flutter_spotify_africa_assessment/colors.dart';
import 'package:flutter_spotify_africa_assessment/routes.dart';
import 'package:flutter_spotify_africa_assessment/constants.dart';
import 'package:flutter_spotify_africa_assessment/features/spotify/presentation/components/header.dart';
import 'package:flutter_spotify_africa_assessment/features/spotify/presentation/components/grid_section.dart';
import "package:http/http.dart" as http;
import "dart:convert";
import 'package:flutter_spotify_africa_assessment/providers/screen_provider.dart';
import 'package:provider/provider.dart';
import 'package:html/parser.dart' as htmlParser;
// TODO: fetch and populate playlist info and allow for click-through to detail
// Feel free to change this to a stateful widget if necessary
class SpotifyCategory extends StatefulWidget {
final String categoryId;
const SpotifyCategory({
Key? key,
required this.categoryId,
}) : super(key: key);
@override
State<SpotifyCategory> createState() => _SpotifyCategoryState();
}
class _SpotifyCategoryState extends State<SpotifyCategory> {
List<Map<String, Future<String>>> playlists = [];
ScrollController _scrollController = ScrollController();
Future<String>? image;
Future<String>? category;
int page = 1;
int limit = 6;
bool loading = false;
@override
void initState() {
super.initState();
_scrollController.addListener(_scrollListener);
fetchData();
fetchPlaylist();
}
Future<void> fetchData() async {
String endpoint = "$spotifyBaseUrl/browse/categories/${widget.categoryId}";
final response = await http.get(Uri.parse(endpoint),
headers: {'x-functions-key': spotifyApiKey}, );
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
String imageUrl = data["icons"][0]["url"];
String categoryName = data["name"];
setState(() {
image = Future.value(imageUrl);
category = Future.value(categoryName);
});
} else {
print('Request failed with status: ${response.statusCode}');
}
}
Future<void> fetchPlaylist() async {
String endpoint = "$spotifyBaseUrl/browse/categories/${widget.categoryId}/playlists?limit=$limit&offset=$page";
final response = await http.get(Uri.parse(endpoint),
headers: {'x-functions-key': spotifyApiKey}, );
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
final array = data["playlists"]["items"];
List<Map<String, Future<String>>> delta = [];
for (int index = 0; index < array.length; index++) {
String imageUrl = array[index]["images"][0]["url"];
String title = array[index]["name"];
String text = array[index]["description"];
String description = htmlParser.parseFragment(text).text.toString();
String identifier = array[index]["id"];
delta.add({ "image": Future.value(imageUrl),
"title": Future.value(title),
"identifier": Future.value(identifier),
"description": Future.value(description), });
}
setState(() {
playlists.addAll(delta);
page += limit;
});
} else {
print('Request failed with status: ${response.statusCode}');
}
}
void _scrollListener() {
if (loading == true) {
return;
}
if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
setState(() {
loading = true;
});
fetchPlaylist();
setState(() {
loading = false;
});
}
}
@override
Widget build(BuildContext context) {
var selectedPlaylist = context.watch<ScreenProvider>().selectedPlaylist;
return Scaffold(
backgroundColor: AppColors.black,
appBar: AppBar(
title: Text('${widget.categoryId[0].toUpperCase()}${widget.categoryId.substring(1).toLowerCase()}'),
centerTitle: true,
actions: [
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: () => Navigator.of(context).pushNamed(AppRoutes.about),
),
],
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
AppColors.blue,
AppColors.cyan,
AppColors.green,
],
),
),
),
),
body: SingleChildScrollView(controller: _scrollController,
physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
child: Container(padding: const EdgeInsets.symmetric(vertical: 32),
// color: AppColors.black,
child: Column(children: [Container(padding: const EdgeInsets.only(left: 24),
child: Row(mainAxisAlignment: MainAxisAlignment.end,
children: [Header(image: image,
category: category), ], ), ),
const SizedBox(height: 32),
GridSection(playlists: playlists), ], ), ), ),
);
}
}