394 lines
11 KiB
Dart
394 lines
11 KiB
Dart
import 'dart:ui' as ui;
|
|
|
|
import 'dart:convert';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
|
|
import 'package:syncfusion_flutter_core/theme.dart';
|
|
|
|
import 'package:multimedia/data/tvshows.dart';
|
|
|
|
Color _kColorNone = Colors.black;
|
|
Color _kColorInit = const Color.fromARGB(255, 192, 192, 192);
|
|
Color _kColorProg = const Color.fromARGB(255, 0, 0, 192);
|
|
Color _kColorDone = const Color.fromARGB(255, 0, 192, 0);
|
|
|
|
double _kStateWidth = 6;
|
|
double _kStateHeight = 30;
|
|
|
|
class TVShowsScreen extends StatefulWidget {
|
|
const TVShowsScreen({
|
|
super.key,
|
|
});
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => _TVShowsScreenState();
|
|
}
|
|
|
|
class _TVShowsScreenState extends State<TVShowsScreen> {
|
|
final DataGridController _dataGridController = DataGridController();
|
|
late int maxSeason;
|
|
late List<EpisodeCount> episodeCount;
|
|
late TVShowDataSource tvshowDataSource;
|
|
List<GridColumn> _columns = [];
|
|
late bool initDone = false;
|
|
|
|
Future<List<TVShow>> generateTVShowList() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
String? dbProtocol = prefs.getString('dbProtocol');
|
|
String? dbHost = prefs.getString('dbHost');
|
|
String? dbPath = prefs.getString('dbPath');
|
|
|
|
Uri url = Uri(
|
|
scheme: dbProtocol,
|
|
host: dbHost,
|
|
path: dbPath,
|
|
queryParameters: {
|
|
'module': 'tvshow',
|
|
'function': 'getList',
|
|
},
|
|
);
|
|
|
|
final response = await http.get(url);
|
|
bool? error;
|
|
String? errorMessage;
|
|
String? mSeason;
|
|
|
|
Map<String, dynamic> tvshows = json.decode(response.body);
|
|
error = tvshows['error'];
|
|
errorMessage = tvshows['errmsg'];
|
|
mSeason = tvshows['maxSeason'];
|
|
|
|
var tvshowList = tvshows['tvshows'];
|
|
var episodeList = tvshows['episodeCount'];
|
|
|
|
if (mSeason != null) {
|
|
if (int.tryParse(mSeason!) != null) {
|
|
maxSeason = int.parse(mSeason);
|
|
}
|
|
}
|
|
|
|
List<TVShow> _tvshows =
|
|
await tvshowList.map<TVShow>((json) => TVShow.fromJson(json)).toList();
|
|
tvshowDataSource = TVShowDataSource(_tvshows, maxSeason);
|
|
|
|
List<EpisodeCount> _episodecount = await episodeList
|
|
.map<EpisodeCount>((json) => EpisodeCount.fromJson(json))
|
|
.toList();
|
|
|
|
episodeCount = _episodecount;
|
|
|
|
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<GridColumn> getColumns() {
|
|
return <GridColumn>[
|
|
GridColumn(
|
|
columnName: 'seriesID',
|
|
visible: false,
|
|
width: 70,
|
|
label: Container(
|
|
padding: const EdgeInsets.all(16.0),
|
|
alignment: Alignment.centerLeft,
|
|
child: const Text('seriesID'))),
|
|
GridColumn(
|
|
columnName: 'seriesState',
|
|
visible: false,
|
|
width: 70,
|
|
label: Container(
|
|
padding: const EdgeInsets.all(16.0),
|
|
alignment: Alignment.centerLeft,
|
|
child: const Text('seriesState'))),
|
|
GridColumn(
|
|
columnName: 'status',
|
|
visible: false,
|
|
width: 70,
|
|
label: Container(
|
|
padding: const EdgeInsets.all(16.0),
|
|
alignment: Alignment.centerLeft,
|
|
child: const Text('status'))),
|
|
GridColumn(
|
|
columnName: 'cliffhanger',
|
|
visible: false,
|
|
width: 70,
|
|
label: Container(
|
|
padding: const EdgeInsets.all(16.0),
|
|
alignment: Alignment.centerLeft,
|
|
child: const Text('cliffhanger'))),
|
|
GridColumn(
|
|
columnName: 'seriesName',
|
|
columnWidthMode: ColumnWidthMode.auto,
|
|
label: Container(
|
|
padding: const EdgeInsets.all(8.0),
|
|
alignment: Alignment.centerLeft,
|
|
child: const Text('Name'))),
|
|
GridColumn(
|
|
columnName: 'firstAired',
|
|
columnWidthMode: ColumnWidthMode.auto,
|
|
label: Container(
|
|
padding: const EdgeInsets.all(8.0),
|
|
alignment: Alignment.centerLeft,
|
|
child: const Text('First Aired'))),
|
|
GridColumn(
|
|
columnName: 'resolution',
|
|
columnWidthMode: ColumnWidthMode.auto,
|
|
label: Container(
|
|
padding: const EdgeInsets.all(8.0),
|
|
alignment: Alignment.centerLeft,
|
|
child: const Text('Resolution'))),
|
|
];
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_columns = getColumns();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: FutureBuilder<Object>(
|
|
future: generateTVShowList(),
|
|
builder: (context, data) {
|
|
return data.hasData
|
|
? SfDataGridTheme(
|
|
data: const SfDataGridThemeData(
|
|
headerColor: Color(0xff009889),
|
|
),
|
|
child: SfDataGrid(
|
|
source: tvshowDataSource,
|
|
selectionMode: SelectionMode.single,
|
|
navigationMode: GridNavigationMode.row,
|
|
columns: _columns,
|
|
controller: _dataGridController,
|
|
columnWidthMode: ColumnWidthMode.auto,
|
|
columnWidthCalculationRange: ColumnWidthCalculationRange.allRows,
|
|
),
|
|
)
|
|
: const Center(
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
value: 0.8,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class TVShowDataSource extends DataGridSource {
|
|
/// Creates the employee data source class with required details.
|
|
TVShowDataSource(this.tvshows, this.maxSeason) {
|
|
buildDataGridRow();
|
|
}
|
|
|
|
String convertState(String state) {
|
|
List<String> stateList = state.split(',');
|
|
int maxEpisode = 0;
|
|
Map stateListMap = {};
|
|
|
|
for (var x = 0; x < stateList.length; x++) {
|
|
List<String> tmp = stateList[x].split('|');
|
|
stateListMap[tmp[0]] = tmp[1];
|
|
|
|
int episode = int.parse(tmp[0]);
|
|
if (episode > maxEpisode) {
|
|
maxEpisode = episode;
|
|
}
|
|
}
|
|
|
|
String stateString = "";
|
|
|
|
for (int x = 0; x < maxEpisode; x++) {
|
|
String s = '0';
|
|
int key = x + 1;
|
|
|
|
if (stateListMap.containsKey('$key')) {
|
|
s = stateListMap['$key'];
|
|
}
|
|
|
|
stateString = stateString + s;
|
|
}
|
|
|
|
return stateString;
|
|
}
|
|
|
|
List<DataGridCell> setSeasonData(int maxSeason, List<Season> seasonList) {
|
|
List<DataGridCell> dataGridCell = [];
|
|
int length = seasonList.length;
|
|
|
|
for (int x = 1; x <= maxSeason; x++) {
|
|
String status = '';
|
|
|
|
if (x <= length) {
|
|
status = convertState(seasonList[x - 1].state);
|
|
}
|
|
dataGridCell.add(DataGridCell(columnName: 'season$x', value: status));
|
|
}
|
|
|
|
return dataGridCell;
|
|
}
|
|
|
|
void buildDataGridRow() {
|
|
_tvshowDataGridRows = tvshows
|
|
.map<DataGridRow>((e) => DataGridRow(cells: [
|
|
DataGridCell<int>(columnName: 'seriesID', value: e.seriesID),
|
|
DataGridCell<int>(
|
|
columnName: 'seriesState', value: e.seriesState),
|
|
DataGridCell<String>(columnName: 'status', value: e.status),
|
|
DataGridCell<int>(
|
|
columnName: 'cliffhanger', value: e.cliffhanger),
|
|
DataGridCell<String>(
|
|
columnName: 'seriesName', value: e.seriesName),
|
|
DataGridCell<String>(
|
|
columnName: 'firstAired',
|
|
value: DateFormat('yyyy-MM-dd').format(e.firstAired)),
|
|
DataGridCell<String>(
|
|
columnName: 'resolution', value: e.resolution),
|
|
...setSeasonData(maxSeason, e.seasonStatus),
|
|
]))
|
|
.toList();
|
|
}
|
|
|
|
List<TVShow> tvshows = [];
|
|
int maxSeason;
|
|
|
|
List<DataGridRow> _tvshowDataGridRows = [];
|
|
|
|
@override
|
|
List<DataGridRow> get rows => _tvshowDataGridRows;
|
|
|
|
void _drawEpisodeState(Canvas c, Paint p, double x, Color color) {
|
|
Offset p1 = Offset(x + _kStateWidth, 1);
|
|
Offset p2 = Offset(x + _kStateWidth, _kStateHeight - 1);
|
|
Rect rect = Rect.fromLTRB(x, 0, x + _kStateWidth, _kStateHeight);
|
|
p.style = PaintingStyle.fill;
|
|
p.color = color;
|
|
c.drawRect(rect, p);
|
|
p.color = Colors.black;
|
|
c.drawLine(p1, p2, p);
|
|
}
|
|
|
|
Row generateSeasonState(String state) {
|
|
if (state.length == 0) {
|
|
return const Row(
|
|
children: [Text('')],
|
|
);
|
|
}
|
|
|
|
List<Color> stateColor = [];
|
|
|
|
for (int x = 0; x < state.length; x++) {
|
|
Color c = _kColorNone;
|
|
|
|
if (state[x] == '1') {
|
|
c = _kColorInit;
|
|
} else if (state[x] == '2') {
|
|
c = _kColorProg;
|
|
} else if (state[x] == '3') {
|
|
c = _kColorDone;
|
|
}
|
|
|
|
stateColor.add(c);
|
|
}
|
|
|
|
return Row(children: <Widget>[
|
|
...stateColor.map((col) => Text(
|
|
'1',
|
|
style: TextStyle(
|
|
fontFamily: 'geometric',
|
|
fontSize: 30,
|
|
color: col,
|
|
),
|
|
)),
|
|
]);
|
|
}
|
|
|
|
@override
|
|
DataGridRowAdapter buildRow(DataGridRow row) {
|
|
return DataGridRowAdapter(
|
|
cells: row.getCells().map<Widget>((e) {
|
|
Color colorBG = Colors.white;
|
|
Color colorFG = Colors.black;
|
|
FontWeight fontWeight = FontWeight.normal;
|
|
FontStyle fontStyle = FontStyle.normal;
|
|
|
|
if (e.columnName == 'seriesName') {
|
|
int seriesState = row.getCells().toList()[1].value;
|
|
String status = row.getCells().toList()[2].value;
|
|
int cliffhanger = row.getCells().toList()[3].value;
|
|
|
|
if (seriesState == 1) {
|
|
colorBG = _kColorInit;
|
|
} else if (seriesState == 2) {
|
|
colorBG = _kColorProg;
|
|
colorFG = Colors.white;
|
|
} else {
|
|
colorBG = _kColorDone;
|
|
}
|
|
|
|
if (cliffhanger == 1) {
|
|
fontStyle = FontStyle.italic;
|
|
}
|
|
|
|
if (status == 'In Production' || status == 'Returning Series') {
|
|
fontWeight = FontWeight.bold;
|
|
}
|
|
}
|
|
|
|
if (e.columnName.startsWith('season')) {
|
|
Row row = generateSeasonState(e.value.toString());
|
|
|
|
return Container(
|
|
alignment: Alignment.center,
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
child: row,
|
|
);
|
|
}
|
|
|
|
return Container(
|
|
alignment: Alignment.centerLeft,
|
|
padding: const EdgeInsets.all(8.0),
|
|
color: colorBG,
|
|
child: Text(
|
|
e.value.toString(),
|
|
style: TextStyle(
|
|
color: colorFG,
|
|
fontWeight: fontWeight,
|
|
fontStyle: fontStyle,
|
|
),
|
|
),
|
|
);
|
|
}).toList());
|
|
}
|
|
|
|
void updateDataGrid() {
|
|
notifyListeners();
|
|
}
|
|
}
|