-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8d3353
commit 9bd3a9d
Showing
21 changed files
with
406 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:toastification/toastification.dart'; | ||
import 'package:visibility_detector/visibility_detector.dart'; | ||
|
||
class APIController<T> extends ChangeNotifier { | ||
void Function()? _refresh; | ||
|
||
// Internal method to bind the refresh function from the state. | ||
void _attachRefresh(void Function() refreshCallback) { | ||
_refresh = refreshCallback; | ||
} | ||
|
||
// Public method to trigger a refresh. | ||
void refresh() { | ||
_refresh?.call(); | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
class APIBuilder<T> extends StatefulWidget { | ||
const APIBuilder( | ||
{super.key, | ||
this.interval, | ||
required this.onLoad, | ||
required this.onData, | ||
required this.api, | ||
this.controller}); | ||
|
||
final Duration? interval; | ||
final Widget Function() onLoad; | ||
final Widget Function(T data) onData; | ||
final Future<T> Function() api; | ||
final APIController<T>? controller; | ||
|
||
@override | ||
State<APIBuilder<T>> createState() => _APIBuilderState<T>(); | ||
} | ||
|
||
class _APIBuilderState<T> extends State<APIBuilder<T>> { | ||
late Future<T> _futureData; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_futureData = widget.api(); | ||
|
||
// Attach the refresh callback to the controller. | ||
widget.controller?._attachRefresh(_refreshData); | ||
} | ||
|
||
// Method to refresh data. | ||
void _refreshData() { | ||
setState(() { | ||
_futureData = widget.api(); | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return VisibilityDetector( | ||
key: widget.key ?? Key(hashCode.toString()), | ||
onVisibilityChanged: (VisibilityInfo info) { | ||
if (info.visibleFraction > 0) { | ||
_refreshData(); | ||
} | ||
}, | ||
child: FutureBuilder<T>( | ||
future: _futureData, | ||
builder: (context, snapshot) { | ||
if (snapshot.hasError) { | ||
print(snapshot.error); | ||
WidgetsBinding.instance | ||
.addPostFrameCallback((_) => toastification.show( | ||
title: Text('API Request failed! ${snapshot.error}'), | ||
autoCloseDuration: const Duration(seconds: 5), | ||
type: ToastificationType.error, | ||
)); | ||
} | ||
if (snapshot.hasData) { | ||
return widget.onData(snapshot.data!); | ||
} else { | ||
return widget.onLoad(); | ||
} | ||
}, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.