Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Throw an exception if twist is down or if data is not cached correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
simrat39 committed Jan 14, 2021
1 parent 0ddb28d commit f86ec83
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/exceptions/TwistDownException.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class TwistDownException implements Exception {}
13 changes: 8 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import 'package:anime_twist_flut/animations/Transitions.dart';
import 'package:anime_twist_flut/animations/TwistLoadingWidget.dart';
import 'package:anime_twist_flut/exceptions/NoInternetException.dart';
import 'package:anime_twist_flut/exceptions/TwistDownException.dart';
import 'package:anime_twist_flut/pages/chat_page/ChatPage.dart';
import 'package:anime_twist_flut/pages/error_page/ErrorPage.dart';
import 'package:anime_twist_flut/pages/favourites_page/FavouritesPage.dart';
Expand Down Expand Up @@ -215,14 +216,16 @@ class _RootWindowState extends State<RootWindow>
},
error: (e, s) {
var message = 'Whoops! An error occured';
switch (e) {
case NoInternetException:
message =
"Looks like you are not connected to the internet . Please reconnect and try again";
break;
if (e is NoInternetException) {
message =
"Looks like you are not connected to the internet. Please reconnect and try again";
} else if (e is TwistDownException) {
message =
"Looks like twist.moe is down. Please try again later";
}
return ErrorPage(
message: message,
e: e,
stackTrace: s,
onRefresh: () => context.refresh(_initDataProvider),
);
Expand Down
1 change: 1 addition & 0 deletions lib/pages/anime_info_page/AnimeInfoPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class _AnimeInfoPageState extends State<AnimeInfoPage> {
loading: () => Center(child: RotatingPinLoadingAnimation()),
error: (e, s) => ErrorPage(
stackTrace: s,
e: e,
message:
"Whoops! An error occured. Looks like twist.moe is down, or your internet is not working. Please try again later.",
onRefresh: () => context.refresh(_initDataProvider),
Expand Down
16 changes: 9 additions & 7 deletions lib/pages/error_page/ErrorPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import 'package:anime_twist_flut/pages/homepage/AppbarText.dart';
import 'package:flutter/material.dart';

class ErrorPage extends StatefulWidget {
ErrorPage(
{Key key,
@required this.message,
@required this.onRefresh,
@required this.stackTrace})
: super(key: key);
ErrorPage({
Key key,
@required this.message,
@required this.onRefresh,
@required this.stackTrace,
@required this.e,
}) : super(key: key);

final String message;
final VoidCallback onRefresh;
final StackTrace stackTrace;
final Exception e;

@override
_ErrorPageState createState() => _ErrorPageState();
Expand Down Expand Up @@ -48,7 +50,7 @@ class _ErrorPageState extends State<ErrorPage> {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Stacktrace"),
title: Text(widget.e.toString()),
scrollable: true,
content: Text(
widget.stackTrace.toString(),
Expand Down
12 changes: 7 additions & 5 deletions lib/services/CacheService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ class CacheService {
bool shouldUpdateCache({String cachedData, DateTime cachedDateTime}) {
if (cachedData == null) return true;
if (cachedData.isEmpty) return true;
if (cachedDateTime == null) return true;
DateTime now = DateTime.now();
bool hasAnime =
(jsonDecode(cachedData.isEmpty ? "{[]}" : cachedData) as List<dynamic>)
.length >
0;
if (!hasAnime) return true;
try {
bool hasAnime = jsonDecode(cachedData).length > 0;
if (!hasAnime) return true;
} catch (e) {
throw Exception();
}
return now.difference(cachedDateTime).abs() > cacheUpdateInterval;
}

Expand Down
25 changes: 15 additions & 10 deletions lib/services/twist_service/AnimeApiService.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Dart imports:
import 'dart:convert';
import 'dart:io';

// Project imports:
import '../../cached_http_get/CachedHttpGet.dart';
import 'package:anime_twist_flut/exceptions/TwistDownException.dart';

import '../../models/TwistModel.dart';
import '../../secrets.dart';
import '../CacheService.dart';
import 'package:supercharged/supercharged.dart';
import 'package:http/http.dart';

class AnimeApiService {
static const String baseUrl = 'https://twist.moe/api/anime';
Expand All @@ -21,15 +24,17 @@ class AnimeApiService {
await cacheService.initialize();

String response = await cacheService.getDataAndCacheIfNeeded(
getData: () {
return CachedHttpGet.get(
Request(
url: baseUrl,
header: {
'x-access-token': x_access_token,
},
),
getData: () async {
var ret = await get(
baseUrl,
headers: {
'x-access-token': x_access_token,
},
);
if (ret.statusCode != HttpStatus.ok) {
throw TwistDownException();
} else
return ret.body;
},
onCache: () {
print("Data is not cached or very old");
Expand All @@ -41,7 +46,7 @@ class AnimeApiService {
);

if (response == null) {
throw Exception("An error occured while getting all the animes :)");
throw TwistDownException();
}

List<TwistModel> ret = [];
Expand Down

0 comments on commit f86ec83

Please sign in to comment.