diff --git a/lib/data/tvshows.dart b/lib/data/tvshows.dart index bea8a82..b6bcd9a 100644 --- a/lib/data/tvshows.dart +++ b/lib/data/tvshows.dart @@ -3,6 +3,42 @@ import 'package:intl/intl.dart'; import 'package:flutter/material.dart'; import 'package:syncfusion_flutter_datagrid/datagrid.dart'; +class EpisodeCount { + int season; + int cnt; + + EpisodeCount({ + required this.season, + required this.cnt, + }); + + factory EpisodeCount.fromJson(Map json) { + return EpisodeCount( + season: int.parse(json['seasonNumber']), + cnt: int.parse(json['cnt']), + ); + } +} + +class Season { + int seasonNumber; + int seasonID; + String state; + + Season({ + required this.seasonNumber, + required this.seasonID, + required this.state, + }); + + factory Season.fromJson(Map json) { + return Season( + seasonNumber: int.parse(json['seasonNumber']), + seasonID: int.parse(json['seasonID']), + state: json['state']); + } +} + class TVShow { int seriesID; String seriesName; @@ -11,6 +47,7 @@ class TVShow { String resolution; int cliffhanger; String status; + List seasonStatus; TVShow({ required this.seriesID, @@ -20,6 +57,7 @@ class TVShow { required this.resolution, required this.cliffhanger, required this.status, + required this.seasonStatus, }); factory TVShow.fromJson(Map json) { @@ -56,56 +94,8 @@ class TVShow { resolution: json['resolution'], status: json['status'], cliffhanger: int.parse(json['cliffhanger']), + seasonStatus: + json['seasons'].map((json) => Season.fromJson(json)).toList(), ); } } - -class TVShowDataSource extends DataGridSource { - /// Creates the employee data source class with required details. - TVShowDataSource(this.tvshows) { - buildDataGridRow(); - } - - void buildDataGridRow() { - _tvshowDataGridRows = tvshows - .map((e) => DataGridRow(cells: [ - DataGridCell(columnName: 'seriesID', value: e.seriesID), - DataGridCell( - columnName: 'seriesState', value: e.seriesState), - DataGridCell(columnName: 'status', value: e.status), - DataGridCell( - columnName: 'cliffhanger', value: e.cliffhanger), - DataGridCell( - columnName: 'seriesName', value: e.seriesName), - DataGridCell( - columnName: 'firstAired', - value: DateFormat('yyyy-MM-dd').format(e.firstAired)), - DataGridCell( - columnName: 'resolution', value: e.resolution), - ])) - .toList(); - } - - List tvshows = []; - - List _tvshowDataGridRows = []; - - @override - List get rows => _tvshowDataGridRows; - - @override - DataGridRowAdapter buildRow(DataGridRow row) { - return DataGridRowAdapter( - cells: row.getCells().map((e) { - return Container( - alignment: Alignment.center, - padding: const EdgeInsets.all(8.0), - child: Text(e.value.toString()), - ); - }).toList()); - } - - void updateDataGrid() { - notifyListeners(); - } -} diff --git a/lib/widgets/tvshowsScreen.dart b/lib/widgets/tvshowsScreen.dart index 13410e9..189faba 100644 --- a/lib/widgets/tvshowsScreen.dart +++ b/lib/widgets/tvshowsScreen.dart @@ -26,9 +26,10 @@ class TVShowsScreen extends StatefulWidget { class _TVShowsScreenState extends State { final DataGridController _dataGridController = DataGridController(); late int maxSeason; - late Map episodeCount; + late List episodeCount; late TVShowDataSource tvshowDataSource; List _columns = []; + late bool initDone = false; Future> generateTVShowList() async { SharedPreferences prefs = await SharedPreferences.getInstance(); @@ -37,8 +38,6 @@ class _TVShowsScreenState extends State { String? dbHost = prefs.getString('dbHost'); String? dbPath = prefs.getString('dbPath'); - print(dbHost); - Uri url = Uri( scheme: dbProtocol, host: dbHost, @@ -52,27 +51,53 @@ class _TVShowsScreenState extends State { final response = await http.get(url); bool? error; String? errorMessage; + String? mSeason; Map tvshows = json.decode(response.body); error = tvshows['error']; errorMessage = tvshows['errmsg']; - var mSeason = tvshows['maxSeason']; + mSeason = tvshows['maxSeason']; + var tvshowList = tvshows['tvshows']; - // var episodeList = tvshows['episodeCount']; - // - // for (var episode in episodeList) { - // episodeCount[episode['seasonNumber']] = episode['cnr']; - // } + var episodeList = tvshows['episodeCount']; + + if (mSeason != null) { + if (int.tryParse(mSeason!) != null) { + maxSeason = int.parse(mSeason); + } + } List _tvshows = await tvshowList.map((json) => TVShow.fromJson(json)).toList(); - tvshowDataSource = TVShowDataSource(_tvshows); + tvshowDataSource = TVShowDataSource(_tvshows, maxSeason); + + List _episodecount = await episodeList + .map((json) => EpisodeCount.fromJson(json)) + .toList(); + + episodeCount = _episodecount; - maxSeason = mSeason; + if (initDone == false) { + addSeasonColumns(maxSeason); + } + + initDone = true; return _tvshows; } + void addSeasonColumns(int maxSeason) { + for (int x = 1; x <= maxSeason; x++) { + _columns.add(GridColumn( + columnName: 'season$x', + columnWidthMode: ColumnWidthMode.auto, + label: Container( + padding: const EdgeInsets.all(8.0), + alignment: Alignment.centerLeft, + child: Text('Season $x')))); + } + } + List getColumns() { return [ GridColumn( @@ -128,13 +153,6 @@ class _TVShowsScreenState extends State { padding: const EdgeInsets.all(8.0), alignment: Alignment.centerLeft, child: const Text('Resolution'))), - GridColumn( - columnName: 'season1', - columnWidthMode: ColumnWidthMode.auto, - label: Container( - padding: const EdgeInsets.all(8.0), - alignment: Alignment.centerLeft, - child: const Text('Season 1'))), ]; } @@ -178,10 +196,25 @@ class _TVShowsScreenState extends State { class TVShowDataSource extends DataGridSource { /// Creates the employee data source class with required details. - TVShowDataSource(this.tvshows) { + TVShowDataSource(this.tvshows, this.maxSeason) { buildDataGridRow(); } + List setSeasonData(int maxSeason, List seasonList) { + List dataGridCell = []; + + for (int x = 1; x <= maxSeason; x++) { + String status = ''; + + if (seasonList.length <= x) { + status = seasonList[x].state; + } + dataGridCell.add(DataGridCell(columnName: 'season$x', value: 'status')); + } + + return dataGridCell; + } + void buildDataGridRow() { _tvshowDataGridRows = tvshows .map((e) => DataGridRow(cells: [ @@ -198,12 +231,13 @@ class TVShowDataSource extends DataGridSource { value: DateFormat('yyyy-MM-dd').format(e.firstAired)), DataGridCell( columnName: 'resolution', value: e.resolution), - DataGridCell(columnName: 'season1', value: ''), + ...setSeasonData(maxSeason, e.seasonStatus), ])) .toList(); } List tvshows = []; + int maxSeason; List _tvshowDataGridRows = []; @@ -242,13 +276,14 @@ class TVShowDataSource extends DataGridSource { } } - // if (e.columnName.startsWith('season')) { - // return Container( - // alignment: Alignment.center, - // padding: EdgeInsets.symmetric(horizontal: 16), - // child: Icon(Icons.location_on), - // ); - // } + if (e.columnName.startsWith('season')) { + return Container( + alignment: Alignment.center, + padding: EdgeInsets.symmetric(horizontal: 16), + child: Icon(Icons.location_on), + ); + } + return Container( alignment: Alignment.centerLeft, padding: const EdgeInsets.all(8.0),