-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add interval refresh on the sks people counter (#415)
* feat: add interval refresh on the sks people counter * refactor: add separate files for refresh on ref and consts add files: sks_people_live_consts.dart ref_extensions.dart change files: home_viev.dart: add SksAppBar to AppBar parkings_repository.dart, latest_sks_user_data_repository.dart: refactor code * refactor: delete unused imports
- Loading branch information
1 parent
526c1a1
commit 6ec30da
Showing
6 changed files
with
36 additions
and
15 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 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
18 changes: 16 additions & 2 deletions
18
lib/features/sks_people_live/data/repository/latest_sks_user_data_repo.dart
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 |
---|---|---|
@@ -1,14 +1,28 @@ | ||
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 "../../../../utils/ref_extensions.dart"; | ||
import "../models/sks_user_data.dart"; | ||
import "../sks_people_live_consts.dart"; | ||
|
||
part "latest_sks_user_data_repo.g.dart"; | ||
|
||
@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(); | ||
final sksRefreshInterval = sksData.isResultRecent | ||
? sksData.nextUpdateTimestamp.difference(currentTime) | ||
: SksPeopleRepositoryConfig.defaultSksRefreshTime; | ||
|
||
ref.setRefresh(sksRefreshInterval); | ||
|
||
return sksData; | ||
} |
3 changes: 3 additions & 0 deletions
3
lib/features/sks_people_live/data/sks_people_live_consts.dart
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,3 @@ | ||
abstract class SksPeopleRepositoryConfig { | ||
static const defaultSksRefreshTime = Duration(minutes: 1); | ||
} |
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,12 @@ | ||
import "dart:async"; | ||
import "package:flutter_riverpod/flutter_riverpod.dart"; | ||
|
||
extension RefIntervalRefreshX on Ref { | ||
void setRefresh(Duration interval) { | ||
final timer = Timer.periodic( | ||
interval, | ||
(t) => invalidateSelf(), | ||
); | ||
onDispose(timer.cancel); | ||
} | ||
} |