Skip to content

Commit

Permalink
add in some network handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LegendAF committed Sep 11, 2024
1 parent 3b2869e commit b0602a1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
130 changes: 77 additions & 53 deletions lib/src/components/tab_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,66 +241,86 @@ class _TenorTabViewState extends State<TenorTabView>
}

Future<void> _loadCatagories() async {
final fromTenor = await client.categories();

final featuredGifResponse = await client.featured(limit: 1);
final featuredGif = featuredGifResponse?.results.first;
if (featuredGif != null) {
fromTenor.insert(
0,
TenorCategory(
name: 'Featured',
searchTerm: '📈 Featured',
image: featuredGif.media.tinygif?.url ?? '',
path: featuredCategoryPath,
),
);
}
try {
final fromTenor = await client.categories();
final featuredGifResponse = await client.featured(limit: 1);
final featuredGif = featuredGifResponse?.results.first;
if (featuredGif != null) {
fromTenor.insert(
0,
TenorCategory(
name: 'Featured',
searchTerm: '📈 Featured',
image: featuredGif.media.tinygif?.url ?? '',
path: featuredCategoryPath,
),
);
}

setState(() {
_categories = fromTenor;
});
setState(() {
_categories = fromTenor;
});
} catch (e) {
//
}
}

Future<void> _loadMore() async {
// return if is loading or no more gifs
if (_isLoading || _collection?.next == '') {
return;
}
try {
// return if loading
if (_isLoading) return;

_isLoading = true;
// failsafe if categories are empty when we load more (network issues)
if (widget.showCategories && _categories.isEmpty) {
_loadCatagories();
}

// Offset pagination for query
if (_collection == null) {
offset = null;
} else {
// _collection.next
offset = _collection!.next;
}
// return no more gifs
if (_collection?.next == '') {
return;
}

if (widget.onLoad != null) {
final response = await widget.onLoad?.call(
_appBarProvider.queryText,
offset,
_limit,
_appBarProvider.selectedCategory,
);
if (response != null) {
_collection = response;
_isLoading = true;

// Offset pagination for query
if (_collection == null) {
offset = null;
} else {
// _collection.next
offset = _collection!.next;
}
}

// Set result to list
if (_collection != null && _collection!.results.isNotEmpty && mounted) {
setState(() {
_list.addAll(_collection!.results);
_isLoading = false;
});
} else {
// so it refreshes on something like categories
setState(() {
_isLoading = false;
});
if (widget.onLoad != null) {
final response = await widget.onLoad?.call(
_appBarProvider.queryText,
offset,
_limit,
_appBarProvider.selectedCategory,
);
if (response != null) {
_collection = response;
}
}

// Set result to list
if (_collection != null && _collection!.results.isNotEmpty && mounted) {
setState(() {
_list.addAll(_collection!.results);
_isLoading = false;
});
} else {
// so it refreshes on something like categories
setState(() {
_isLoading = false;
});
}
} on TenorNetworkException {
_isLoading = false;
} on TenorApiException {
_isLoading = false;
} catch (e) {
_isLoading = false;
rethrow;
}
}

Expand All @@ -322,8 +342,12 @@ class _TenorTabViewState extends State<TenorTabView>

// Return selected gif
void _selectedGif(TenorResult gif) {
// https://developers.google.com/tenor/guides/endpoints#register-share
client.registerShare(gif.id, search: _appBarProvider.queryText);
try {
// https://developers.google.com/tenor/guides/endpoints#register-share
client.registerShare(gif.id, search: _appBarProvider.queryText);
} catch (e) {
// do nothing if it fails
}
// return result to the consumer
Navigator.pop(context, gif);
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/tenor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Tenor extends tenor_dart.Tenor {
super.contentFilter = TenorContentFilter.off,
super.country = 'US',
super.locale = 'en_US',
super.networkTimeout = const Duration(seconds: 5),
});

/// Shows a bottom sheet modal that allows you to select a Tenor media object for use.
Expand Down

0 comments on commit b0602a1

Please sign in to comment.