-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add interval refresh on the sks people counter #415
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
import "dart:async"; | ||
import "package:flutter_riverpod/flutter_riverpod.dart"; | ||
import "package:riverpod_annotation/riverpod_annotation.dart"; | ||
import "../../../../shared_api_clients/sks_api_client.dart"; | ||
import "../models/sks_user_data.dart"; | ||
|
||
part "latest_sks_user_data_repo.g.dart"; | ||
|
||
extension RefIntervalRefreshX on Ref { | ||
void setRefresh(Duration interval) { | ||
final timer = Timer.periodic( | ||
interval, | ||
(t) => invalidateSelf(), | ||
); | ||
onDispose(timer.cancel); | ||
} | ||
} | ||
|
||
@riverpod | ||
Future<SksUserData> getLatestSksUserData(Ref ref) async { | ||
final dio = ref.read(sksClientProvider); | ||
const latestDataEndpoint = "sks-users/current/"; | ||
const latestDataEndpoint = "/sks-users/current/"; | ||
final response = await dio.get(latestDataEndpoint); | ||
return SksUserData.fromJson(response.data as Map<String, dynamic>); | ||
final sksData = SksUserData.fromJson(response.data as Map<String, dynamic>); | ||
|
||
final currentTime = DateTime.now(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a suggestion, please check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbh this can stays as it is (imo) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, placed it here as fun fact lets say |
||
final sksRefreshInterval = sksData.isResultRecent | ||
? sksData.nextUpdateTimestamp.difference(currentTime) | ||
: const Duration(minutes: 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please extract it to some const, eg. in config (duration time) |
||
|
||
ref.setRefresh(sksRefreshInterval); | ||
|
||
return sksData; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have this part of code in file parkings_repository.dart so my idea is to move this to separate file(like ref_extesions.dart) in utils folder and use it in 2 places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing! I can take care of refactorization. By the way - to test code it is nessesary to add SksAppBar somewhere. It wasn't placed anywhere before so I left it as it was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add it at app bar at main screen (for testing purposes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch Tomuś