Skip to content

Commit

Permalink
remove gogoanime api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sunder-kirei committed Jan 25, 2023
1 parent 9eeffee commit ed5ddc7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 77 deletions.
4 changes: 3 additions & 1 deletion lib/helpers/db_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DBHelper {
"CREATE TABLE $tableName (id TEXT PRIMARY KEY, romaji TEXT, image TEXT)",
);
await db.execute(
"CREATE TABLE $historyTable (id TEXT PRIMARY KEY, image TEXT, episode INTEGER, episodeImage TEXT, details TEXT, position INTEGER)",
"CREATE TABLE $historyTable (id TEXT PRIMARY KEY, image TEXT, episode INTEGER, episodeImage TEXT, details TEXT, position INTEGER, title TEXT)",
);
},
version: 1,
Expand Down Expand Up @@ -85,6 +85,7 @@ class DBHelper {
required int episode,
required String details,
required int position,
required String title,
}) async {
final sql = await openDB();
final id = await sql.insert(
Expand All @@ -96,6 +97,7 @@ class DBHelper {
"episodeImage": episodeImage,
"details": details,
"position": position,
"title": title,
},
conflictAlgorithm: ConflictAlgorithm.replace,
);
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Watchlist with ChangeNotifier {
required int episode,
required String details,
required int position,
required String title,
}) async {
await DBHelper.insertHistory(
itemId: itemId,
Expand All @@ -108,6 +109,7 @@ class Watchlist with ChangeNotifier {
episode: episode,
details: details,
position: position,
title: title,
);
await fetchHistory();
return;
Expand Down
10 changes: 5 additions & 5 deletions lib/screens/details_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class _DetailsScreenState extends State<DetailsScreen> {
as Map<String, dynamic>)["id"],
)),
//Uncomment this is using video_player_screen_animepahe.dart file"
// provider: Stream.animepahe,
provider: Stream.animepahe,
).then(
(value) {
setState(() {
Expand Down Expand Up @@ -92,8 +92,8 @@ class _DetailsScreenState extends State<DetailsScreen> {
image: fetchedData!["image"],
id: fetchedData!["id"],
//Uncomment this is using video_player_screen_animepahe.dart file" also above in didChangeDependencies()
// title: fetchedData!["title"]
// ["romaji"],
title: fetchedData!["title"]
["romaji"],
),
),
);
Expand Down Expand Up @@ -216,11 +216,11 @@ class _DetailsScreenState extends State<DetailsScreen> {
CustomRoute(
builder: (context) => VideoPlayerScreen(
details: fetchedData!["episodes"],
episode: data["number"],
episode: index,
image: fetchedData!["image"],
id: fetchedData!["id"],
//Uncomment this is using video_player_screen_animepahe.dart file"
// title: fetchedData!["title"]["romaji"],
title: fetchedData!["title"]["romaji"],
),
),
),
Expand Down
6 changes: 4 additions & 2 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class HomeScreen extends StatelessWidget {
.getHistory
.reversed
.toList()[index];
final details = json.decode(data["details"]);
return ClipRRect(
borderRadius: BorderRadius.circular(5),
child: AspectRatio(
Expand All @@ -73,7 +74,7 @@ class HomeScreen extends StatelessWidget {
footer: Container(
color: Colors.black,
child: Text(
"Episode ${data["episode"]}",
"Episode ${details[data["episode"]]["number"]}",
textAlign: TextAlign.center,
),
),
Expand All @@ -94,8 +95,9 @@ class HomeScreen extends StatelessWidget {
id: data["id"],
image: data["image"],
episode: data["episode"],
details: json.decode(data["details"]),
details: details,
position: data["position"],
title: data["title"],
//Uncomment this is using video_player_screen_animepahe.dart file"
// title: data["title"]["romaji"],
),
Expand Down
136 changes: 67 additions & 69 deletions lib/screens/video_player_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class VideoPlayerScreen extends StatefulWidget {
final int episode;
final List<dynamic> details;
final int position;
final String title;
const VideoPlayerScreen({
super.key,
required this.id,
required this.image,
required this.episode,
required this.details,
this.position = 0,
required this.title,
});
static const routeName = "/watch";

Expand All @@ -31,7 +33,7 @@ class VideoPlayerScreen extends StatefulWidget {

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
final ScrollController _controller = ScrollController();
Map<String, dynamic>? fetchedData;
// Map<String, dynamic>? fetchedData;
List<dynamic>? localDetail;
List<dynamic>? videoSources;
int? currentEpisode;
Expand All @@ -40,41 +42,27 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
void fetchDetails() {
HttpHelper.getInfo(
malID: int.parse(widget.id),
provider: Stream.gogoanime,
provider: Stream.animepahe,
).then((value) {
setState(() {
localDetail = value["episodes"];
});
});
}

Future<void> getStreamInfo({required Stream provider}) async {
final response = await HttpHelper.getInfo(
malID: int.parse(widget.id),
provider: provider,
);
setState(() {
fetchedData = response;
});

if (fetchedData!["episodes"].length != widget.details) fetchDetails();

await getEpisode(
episode: currentEpisode!,
);
}

Future<void> getEpisode({int episode = 1}) async {
Future<void> getEpisode({required int episode}) async {
print(episode);
Provider.of<Watchlist>(
context,
listen: false,
).addToHistory(
episode: episode,
image: widget.image,
episodeImage: widget.details[episode - 1]["image"],
episodeImage: widget.details[episode]["image"],
itemId: widget.id,
details: json.encode(widget.details),
position: 0,
title: widget.title,
);
setState(() {
isLoading = true;
Expand All @@ -87,7 +75,7 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
);
}
final response = await HttpHelper.getVideo(
episodeID: fetchedData!["episodes"][episode - 1]["id"],
episodeID: localDetail![episode]["id"],
);
setState(() {
videoSources = response["sources"];
Expand All @@ -96,10 +84,10 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
}

void scrollToEpisode({
int episode = 1,
int episode = 0,
Duration duration = Duration.zero,
}) {
double position = 100 * (episode - 1);
double position = (100 * (episode)).toDouble();
if (episode * 60 > 3000) {
_controller.jumpTo(min(position, _controller.position.maxScrollExtent));
}
Expand All @@ -115,22 +103,28 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
}

Future<void> callback({required int? position}) async {
int episode = currentEpisode ?? 1;
int episode = currentEpisode ?? 0;
await Provider.of<Watchlist>(
context,
listen: false,
).addToHistory(
episode: episode,
image: widget.image,
episodeImage: widget.details[episode - 1]["image"],
episodeImage: widget.details[episode]["image"],
itemId: widget.id,
details: localDetail == null
? json.encode(widget.details)
: json.encode(localDetail),
position: position ?? 0,
title: widget.title,
);
}

Future<void> initPlayer() async {
await getEpisode(episode: currentEpisode!);
fetchDetails();
}

@override
void initState() {
super.initState();
Expand All @@ -140,18 +134,21 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
void didChangeDependencies() {
setState(() {
currentEpisode = widget.episode;
localDetail = widget.details;
});

WidgetsBinding.instance.addPostFrameCallback((_) {
scrollToEpisode(episode: currentEpisode!);
});
getStreamInfo(provider: Stream.animepahe);
initPlayer();
super.didChangeDependencies();
}

@override
Widget build(BuildContext context) {
int currentLength =
localDetail == null ? widget.details.length : localDetail!.length;
int currentLength = localDetail == null
? widget.details.length - 1
: localDetail!.length - 1;
return Scaffold(
body: SafeArea(
child: Flex(
Expand All @@ -172,7 +169,7 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
nextEpisode: () {
getEpisode(
episode: currentEpisode == currentLength
? currentLength + 1
? currentLength
: currentEpisode! + 1,
);
},
Expand Down Expand Up @@ -200,44 +197,45 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
const SizedBox(
width: 10,
),
if (fetchedData != null)
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 5,
),
Text(
"Currently Watching",
style: Theme.of(context).textTheme.caption,
),
const SizedBox(
height: 3,
),
Text(
fetchedData!["title"]["romaji"],
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(
height: 10,
),
Text(
"Episode",
style: Theme.of(context).textTheme.caption,
),
const SizedBox(
height: 3,
),
Text(
currentEpisode.toString(),
style: Theme.of(context).textTheme.titleLarge,
),
],
),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 5,
),
Text(
"Currently Watching",
style: Theme.of(context).textTheme.caption,
),
const SizedBox(
height: 3,
),
Text(
widget.title,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(
height: 10,
),
Text(
"Episode",
style: Theme.of(context).textTheme.caption,
),
const SizedBox(
height: 3,
),
Text(
// currentEpisode.toString(),
localDetail![currentEpisode!]["number"]
.toString(),
style: Theme.of(context).textTheme.titleLarge,
),
],
),
),
],
),
),
Expand All @@ -263,13 +261,13 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
: localDetail![index];
return InkWell(
onTap: () {
if (data["number"] == currentEpisode) return;
if (index == currentEpisode) return;
getEpisode(
episode: data["number"],
episode: index,
);
},
child: Container(
color: data["number"] == currentEpisode
color: index == currentEpisode
? Theme.of(context).colorScheme.background
: null,
width: MediaQuery.of(context).size.width,
Expand Down

0 comments on commit ed5ddc7

Please sign in to comment.