save new movie
This commit is contained in:
+183
-34
@@ -1,6 +1,44 @@
|
|||||||
import 'package:logger/logger.dart';
|
import 'package:logger/logger.dart';
|
||||||
import 'package:multimedia/log_printer.dart';
|
import 'package:multimedia/log_printer.dart';
|
||||||
|
|
||||||
|
int getInt(Map<dynamic, dynamic> json, String key) {
|
||||||
|
if (json.containsKey(key)) {
|
||||||
|
return json[key] + 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getBool(Map<dynamic, dynamic> json, String key) {
|
||||||
|
if (json.containsKey(key)) {
|
||||||
|
return json[key];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
double getDouble(Map<dynamic, dynamic> json, String key) {
|
||||||
|
if (json.containsKey(key)) {
|
||||||
|
return json[key] + 0.0;
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getString(Map<dynamic, dynamic> json, String key) {
|
||||||
|
if (json.containsKey(key)) {
|
||||||
|
if (json[key] == null) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return json[key];
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime getDateTime(Map<dynamic, dynamic> json, String key) {
|
||||||
|
if (json.containsKey(key)) {
|
||||||
|
return DateTime.parse(json[key].toString());
|
||||||
|
}
|
||||||
|
return DateTime(1900);
|
||||||
|
}
|
||||||
|
|
||||||
class Movie {
|
class Movie {
|
||||||
int movieID;
|
int movieID;
|
||||||
String movieTitle;
|
String movieTitle;
|
||||||
@@ -130,42 +168,153 @@ class Movie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
factory Movie.fromJsonDynamic(Map<dynamic, dynamic> json) {
|
factory Movie.fromJsonDynamic(Map<dynamic, dynamic> json) {
|
||||||
// dynamic languages = json['languages'];
|
int movieID = getInt(json, 'id');
|
||||||
// dynamic productionCountries = json['productionCountries'];
|
String movieTitle = getString(json, 'title');
|
||||||
// dynamic productionCompanies = json['productionCompanies'];
|
String originalTitle = getString(json, 'original_title');
|
||||||
// dynamic cast = json['cast'];
|
String overview = getString(json, 'overview');
|
||||||
// dynamic crew = json['crew'];
|
DateTime releaseDate = getDateTime(json, 'release_date');
|
||||||
// dynamic genre = json['genre'];
|
int state = 1;
|
||||||
|
String resolution = '';
|
||||||
|
String backdropPath = getString(json, 'backdrop_path');
|
||||||
|
String posterPath = getString(json, 'poster_path');
|
||||||
|
double voteAverage = getDouble(json, 'vote_average');
|
||||||
|
int voteCount = getInt(json, 'vote_count');
|
||||||
|
String localPath = '';
|
||||||
|
dynamic externalIDs = json['external_ids'];
|
||||||
|
String imdbID = getString(externalIDs, 'imdb_id');
|
||||||
|
double popularity = getDouble(json, 'popularity');
|
||||||
|
String originalLanguage = getString(json, 'original_language');
|
||||||
|
int adult = getBool(json, 'adult') == true ? 1 : 0;
|
||||||
|
double budget = getDouble(json, 'budget');
|
||||||
|
String homepage = getString(json, 'homepage');
|
||||||
|
double revenue = getDouble(json, 'revenue');
|
||||||
|
int runtime = getInt(json, 'runtime');
|
||||||
|
String tagline = getString(json, 'tagline');
|
||||||
|
|
||||||
|
dynamic languages = json['spoken_languages'];
|
||||||
|
dynamic productionCountries = json['production_countries'];
|
||||||
|
dynamic productionCompanies = json['production_companies'];
|
||||||
|
dynamic credits = json['credits'];
|
||||||
|
dynamic genres = json['genres'];
|
||||||
|
dynamic casts = credits['cast'];
|
||||||
|
dynamic crews = credits['crew'];
|
||||||
|
String belongsToCollection = '';
|
||||||
|
|
||||||
|
String languageList = '';
|
||||||
|
String castList = '';
|
||||||
|
String crewList = '';
|
||||||
|
String genreList = '';
|
||||||
|
String productionCompaniesList = '';
|
||||||
|
String productionCountriesList = '';
|
||||||
|
bool first;
|
||||||
|
|
||||||
|
if (json.containsKey('belongsToCollection')) {
|
||||||
|
belongsToCollection = json['belongsToCollection'];
|
||||||
|
}
|
||||||
|
|
||||||
|
first = true;
|
||||||
|
for (var i = 0; i < languages.length; i++) {
|
||||||
|
dynamic language = languages[i];
|
||||||
|
String languageName = language['name'];
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
languageList = languageList + ',';
|
||||||
|
}
|
||||||
|
languageList = languageName;
|
||||||
|
}
|
||||||
|
|
||||||
|
first = true;
|
||||||
|
for (var i = 0; i < casts.length; i++) {
|
||||||
|
dynamic cast = casts[i];
|
||||||
|
String castName = cast['name'];
|
||||||
|
String castChar = cast['character'];
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
castList = castList + '|';
|
||||||
|
}
|
||||||
|
castList = castList + castName + ',' + castChar;
|
||||||
|
}
|
||||||
|
|
||||||
|
first = true;
|
||||||
|
for (var i = 0; i < crews.length; i++) {
|
||||||
|
dynamic crew = crews[i];
|
||||||
|
String crewName = crew['name'];
|
||||||
|
String crewDepartment = crew['department'];
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
crewList = crewList + '|';
|
||||||
|
}
|
||||||
|
crewList = crewList + crewName + ',' + crewDepartment;
|
||||||
|
}
|
||||||
|
|
||||||
|
first = true;
|
||||||
|
for (var i = 0; i < productionCountries.length; i++) {
|
||||||
|
dynamic productionCountry = productionCountries[i];
|
||||||
|
String name = productionCountry['name'];
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
productionCountriesList = productionCountriesList + ',';
|
||||||
|
}
|
||||||
|
productionCountriesList = productionCountriesList + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
first = true;
|
||||||
|
for (var i = 0; i < productionCompanies.length; i++) {
|
||||||
|
dynamic productionCompany = productionCompanies[i];
|
||||||
|
String name = productionCompany['name'];
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
productionCountriesList = productionCompaniesList + ',';
|
||||||
|
}
|
||||||
|
productionCompaniesList = productionCompaniesList + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
first = true;
|
||||||
|
for (var i = 0; i < genres.length; i++) {
|
||||||
|
dynamic genre = genres[i];
|
||||||
|
String genreName = genre['name'];
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
genreList = genreList + ',';
|
||||||
|
}
|
||||||
|
genreList = genreList + genreName;
|
||||||
|
}
|
||||||
|
|
||||||
return Movie(
|
return Movie(
|
||||||
movieID: json['id'],
|
movieID: movieID,
|
||||||
movieTitle: json['title'],
|
movieTitle: movieTitle,
|
||||||
originalTitle: json['original_title'],
|
originalTitle: originalTitle,
|
||||||
overview: json['overview'],
|
overview: overview,
|
||||||
releaseDate: DateTime.parse(json['release_date'].toString()),
|
releaseDate: releaseDate,
|
||||||
state: int.tryParse(['status'].toString()) ?? 0,
|
state: state,
|
||||||
resolution: '',
|
resolution: resolution,
|
||||||
backdropPath: json['backdrop_path'],
|
backdropPath: backdropPath,
|
||||||
posterPath: '',
|
posterPath: posterPath,
|
||||||
languages: '',
|
languages: languageList,
|
||||||
productionCountries: '',
|
productionCountries: productionCountriesList,
|
||||||
productionCompanies: '',
|
productionCompanies: productionCompaniesList,
|
||||||
voteAverage: double.tryParse(['vote_average'].toString()) ?? 0,
|
voteAverage: voteAverage,
|
||||||
voteCount: int.tryParse(['vote_count'].toString()) ?? 0,
|
voteCount: voteCount,
|
||||||
cast: '',
|
cast: castList,
|
||||||
crew: '',
|
crew: crewList,
|
||||||
genre: '',
|
genre: genreList,
|
||||||
localPath: '',
|
localPath: localPath,
|
||||||
imdbID: '',
|
imdbID: imdbID,
|
||||||
originalLanguage: '',
|
originalLanguage: originalLanguage,
|
||||||
popularity: 0,
|
popularity: popularity,
|
||||||
adult: false,
|
adult: adult == 1,
|
||||||
belongsToCollection: '',
|
belongsToCollection: belongsToCollection,
|
||||||
budget: 0,
|
budget: budget,
|
||||||
homepage: '',
|
homepage: homepage,
|
||||||
revenue: 0,
|
revenue: revenue,
|
||||||
runtime: 0,
|
runtime: runtime,
|
||||||
tagline: '',
|
tagline: tagline,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,12 @@ import 'package:tmdb_api/tmdb_api.dart' as TMDB;
|
|||||||
|
|
||||||
// ignore: must_be_immutable
|
// ignore: must_be_immutable
|
||||||
class AddMovieScreen extends StatefulWidget {
|
class AddMovieScreen extends StatefulWidget {
|
||||||
AddMovieScreen({
|
final Function(Movie) addMovieToList;
|
||||||
|
final Function() addMovieFinished;
|
||||||
|
|
||||||
|
AddMovieScreen(
|
||||||
|
this.addMovieToList,
|
||||||
|
this.addMovieFinished, {
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -35,6 +40,8 @@ class _AddMovieScreenState extends State<AddMovieScreen> {
|
|||||||
List<Movie> movieList = [];
|
List<Movie> movieList = [];
|
||||||
|
|
||||||
late MovieSearchList movieSearchList;
|
late MovieSearchList movieSearchList;
|
||||||
|
late Function(Movie) addMovieToList;
|
||||||
|
late Function() addMovieFinished;
|
||||||
|
|
||||||
static const headerStyle = TextStyle(
|
static const headerStyle = TextStyle(
|
||||||
color: Color(0xffffffff), fontSize: 18, fontWeight: FontWeight.bold);
|
color: Color(0xffffffff), fontSize: 18, fontWeight: FontWeight.bold);
|
||||||
@@ -42,6 +49,8 @@ class _AddMovieScreenState extends State<AddMovieScreen> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
addMovieToList = widget.addMovieToList;
|
||||||
|
addMovieFinished = widget.addMovieFinished;
|
||||||
movieSearchList = MovieSearchList(movieList: movieList);
|
movieSearchList = MovieSearchList(movieList: movieList);
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -376,15 +385,15 @@ class _AddMovieScreenState extends State<AddMovieScreen> {
|
|||||||
if (movie.state == 1) {
|
if (movie.state == 1) {
|
||||||
somethingFound = true;
|
somethingFound = true;
|
||||||
addMovie(movieList[i].movieID);
|
addMovie(movieList[i].movieID);
|
||||||
|
addMovieToList(movie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
addMovieFinished();
|
||||||
|
|
||||||
return somethingFound;
|
return somethingFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> addMovie(int movieID) async {
|
Future<bool> addMovie(int movieID) async {
|
||||||
logger.i('addMovie');
|
|
||||||
|
|
||||||
String lang = 'de';
|
String lang = 'de';
|
||||||
|
|
||||||
if (!searchGerman || searchEnglish) {
|
if (!searchGerman || searchEnglish) {
|
||||||
@@ -400,10 +409,8 @@ class _AddMovieScreenState extends State<AddMovieScreen> {
|
|||||||
appendToResponse: 'credits,external_ids',
|
appendToResponse: 'credits,external_ids',
|
||||||
);
|
);
|
||||||
|
|
||||||
// logger.i(response);
|
|
||||||
Movie movie = Movie.fromJsonDynamic(response);
|
Movie movie = Movie.fromJsonDynamic(response);
|
||||||
writeToDB(movie);
|
writeToDB(movie);
|
||||||
// logger.i('${movie.movieTitle} (${movie.movieID})');
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -435,8 +442,7 @@ class _AddMovieScreenState extends State<AddMovieScreen> {
|
|||||||
'movieTitle': movie.movieTitle,
|
'movieTitle': movie.movieTitle,
|
||||||
'originalTitle': movie.originalTitle,
|
'originalTitle': movie.originalTitle,
|
||||||
'overview': movie.overview,
|
'overview': movie.overview,
|
||||||
'releaseDate':
|
'releaseDate': DateFormat('yyyy-MM-dd').format(movie.releaseDate),
|
||||||
DateFormat('yyyy-MM-dd').format(movie.releaseDate),
|
|
||||||
'resolution': movie.resolution,
|
'resolution': movie.resolution,
|
||||||
'state': movie.state.toString(),
|
'state': movie.state.toString(),
|
||||||
'backdropPath': movie.backdropPath,
|
'backdropPath': movie.backdropPath,
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:multimedia/widgets/movies/add/add_screen.dart';
|
import 'package:multimedia/widgets/movies/add/add_screen.dart';
|
||||||
import 'package:multimedia/widgets/movies/details/details_screen.dart';
|
import 'package:multimedia/widgets/movies/details/details_screen.dart';
|
||||||
import 'package:multimedia/widgets/movies/filter_box.dart';
|
import 'package:multimedia/widgets/movies/filter_box.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
|
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
|
||||||
@@ -195,19 +195,6 @@ class _MoviesScreenState extends State<MoviesScreen> {
|
|||||||
movieDataSource.updateDataGridSource(
|
movieDataSource.updateDataGridSource(
|
||||||
rowColumnIndex: RowColumnIndex(realIndex, i));
|
rowColumnIndex: RowColumnIndex(realIndex, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// movieDataSource.updateDataGridSource(
|
|
||||||
// rowColumnIndex: RowColumnIndex(realIndex, 0));
|
|
||||||
// movieDataSource.updateDataGridSource(
|
|
||||||
// rowColumnIndex: RowColumnIndex(realIndex, 1));
|
|
||||||
// movieDataSource.updateDataGridSource(
|
|
||||||
// rowColumnIndex: RowColumnIndex(realIndex, 2));
|
|
||||||
// movieDataSource.updateDataGridSource(
|
|
||||||
// rowColumnIndex: RowColumnIndex(realIndex, 3));
|
|
||||||
// movieDataSource.updateDataGridSource(
|
|
||||||
// rowColumnIndex: RowColumnIndex(realIndex, 4));
|
|
||||||
// movieDataSource.updateDataGridSource(
|
|
||||||
// rowColumnIndex: RowColumnIndex(realIndex, 5));
|
|
||||||
} else {}
|
} else {}
|
||||||
filter();
|
filter();
|
||||||
}
|
}
|
||||||
@@ -261,6 +248,7 @@ class _MoviesScreenState extends State<MoviesScreen> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
late SfDataGrid dataGrid;
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -279,9 +267,6 @@ class _MoviesScreenState extends State<MoviesScreen> {
|
|||||||
tooltip: 'Add Movie',
|
tooltip: 'Add Movie',
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
addMovie();
|
addMovie();
|
||||||
// if (save()) {
|
|
||||||
// Navigator.pop(context, movieDetails);
|
|
||||||
// }
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
@@ -300,46 +285,48 @@ class _MoviesScreenState extends State<MoviesScreen> {
|
|||||||
child: FutureBuilder<Object>(
|
child: FutureBuilder<Object>(
|
||||||
future: generateMovieList(),
|
future: generateMovieList(),
|
||||||
builder: (context, data) {
|
builder: (context, data) {
|
||||||
return data.hasData
|
if (data.hasData) {
|
||||||
? SfDataGridTheme(
|
dataGrid = SfDataGrid(
|
||||||
data: const SfDataGridThemeData(
|
source: movieDataSource,
|
||||||
headerColor: Color(0xff009889),
|
selectionMode: SelectionMode.single,
|
||||||
),
|
navigationMode: GridNavigationMode.row,
|
||||||
child: SfDataGrid(
|
columns: _columns,
|
||||||
source: movieDataSource,
|
controller: _dataGridController,
|
||||||
selectionMode: SelectionMode.single,
|
columnWidthMode: ColumnWidthMode.auto,
|
||||||
navigationMode: GridNavigationMode.row,
|
columnWidthCalculationRange:
|
||||||
columns: _columns,
|
ColumnWidthCalculationRange.allRows,
|
||||||
controller: _dataGridController,
|
onCellTap: (DataGridCellTapDetails details) {
|
||||||
columnWidthMode: ColumnWidthMode.auto,
|
int index = details.rowColumnIndex.rowIndex - 1;
|
||||||
columnWidthCalculationRange:
|
if (index < 0) {
|
||||||
ColumnWidthCalculationRange.allRows,
|
return;
|
||||||
onCellTap: (DataGridCellTapDetails details) {
|
}
|
||||||
int index = details.rowColumnIndex.rowIndex - 1;
|
showMovieDetails(index);
|
||||||
if (index < 0) {
|
},
|
||||||
return;
|
onCellDoubleTap: (DataGridCellDoubleTapDetails details) {
|
||||||
}
|
print(details.rowColumnIndex);
|
||||||
showMovieDetails(index);
|
},
|
||||||
},
|
onCellLongPress: (DataGridCellLongPressDetails details) {
|
||||||
onCellDoubleTap:
|
print(details.rowColumnIndex);
|
||||||
(DataGridCellDoubleTapDetails details) {
|
},
|
||||||
print(details.rowColumnIndex);
|
onCellSecondaryTap: (DataGridCellTapDetails details) {
|
||||||
},
|
print(details.rowColumnIndex);
|
||||||
onCellLongPress:
|
},
|
||||||
(DataGridCellLongPressDetails details) {
|
);
|
||||||
print(details.rowColumnIndex);
|
|
||||||
},
|
return SfDataGridTheme(
|
||||||
onCellSecondaryTap: (DataGridCellTapDetails details) {
|
data: const SfDataGridThemeData(
|
||||||
print(details.rowColumnIndex);
|
headerColor: Color(0xff009889),
|
||||||
},
|
),
|
||||||
),
|
child: dataGrid,
|
||||||
)
|
);
|
||||||
: const Center(
|
} else {
|
||||||
child: CircularProgressIndicator(
|
return const Center(
|
||||||
strokeWidth: 2,
|
child: CircularProgressIndicator(
|
||||||
value: 0.8,
|
strokeWidth: 2,
|
||||||
),
|
value: 0.8,
|
||||||
);
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -347,9 +334,53 @@ class _MoviesScreenState extends State<MoviesScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addMovieToList(Movie movie) {
|
||||||
|
String movieTitle = movie.movieTitle.toUpperCase();
|
||||||
|
|
||||||
|
_moviesList.add(movie);
|
||||||
|
|
||||||
|
DataGridRow newRow = DataGridRow(cells: [
|
||||||
|
DataGridCell<int>(columnName: 'movieID', value: movie.movieID),
|
||||||
|
DataGridCell<int>(columnName: 'state', value: movie.state),
|
||||||
|
DataGridCell<String>(
|
||||||
|
columnName: 'backdropPath', value: movie.backdropPath),
|
||||||
|
DataGridCell<String>(columnName: 'movieTitle', value: movie.movieTitle),
|
||||||
|
DataGridCell<String>(
|
||||||
|
columnName: 'releaseDate',
|
||||||
|
value: DateFormat('yyyy-MM-dd').format(movie.releaseDate)),
|
||||||
|
DataGridCell<String>(columnName: 'resolution', value: movie.resolution),
|
||||||
|
DataGridCell<String>(columnName: 'overview', value: movie.overview),
|
||||||
|
]);
|
||||||
|
|
||||||
|
int rowNum = -1;
|
||||||
|
for (int i = 0; i < movieDataSource._movieDataGridRows.length; i++) {
|
||||||
|
DataGridRow tmpRow = movieDataSource._movieDataGridRows[i];
|
||||||
|
List<DataGridCell> tmpCells = tmpRow.getCells();
|
||||||
|
String tmpTitle = tmpCells[3].value.toString();
|
||||||
|
|
||||||
|
if (tmpTitle.toUpperCase().compareTo(movieTitle) >= 0) {
|
||||||
|
rowNum = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rowNum == -1) {
|
||||||
|
rowNum = movieDataSource._movieDataGridRows.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
movieDataSource._movieDataGridRows.insert(rowNum, newRow);
|
||||||
|
movieDataSource.updateDataGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
void addMovieFinished() {}
|
||||||
|
|
||||||
void addMovie() {
|
void addMovie() {
|
||||||
final m = Navigator.push(
|
final m = Navigator.push(
|
||||||
context, MaterialPageRoute(builder: (ctx) => AddMovieScreen()));
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (ctx) =>
|
||||||
|
AddMovieScreen(addMovieToList, addMovieFinished)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void searchMovie() {
|
void searchMovie() {
|
||||||
|
|||||||
Reference in New Issue
Block a user