Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/features/sks_people_live/data/models/sks_user_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ part "sks_user_data.g.dart";
@freezed
class SksUserData with _$SksUserData {
const factory SksUserData({
required int id,
required int activeUsers,
required int movingAverage21,
required DateTime externalTimestamp,
required DateTime createdAt,
required DateTime updatedAt,
required Trend trend,
required bool isResultRecent,
required DateTime nextUpdateTimestamp,
}) = _SksUserData;

factory SksUserData.fromJson(Map<String, dynamic> json) =>
Expand Down
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);
}
}

Copy link
Member

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.

Copy link
Contributor Author

@thesun901 thesun901 Nov 20, 2024

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.

Copy link
Member

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch Tomuś

@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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion, please check DateTimeUtils. In mentioned file Simon created handy extension now. Maybe it'll suit you here as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh this can stays as it is (imo)

Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

@mikolaj-jalocha mikolaj-jalocha Nov 20, 2024

Choose a reason for hiding this comment

The 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;
}