Skip to content

Commit

Permalink
feat: add interval refresh on the sks people counter (#415)
Browse files Browse the repository at this point in the history
* 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
thesun901 authored and simon-the-shark committed Nov 21, 2024
1 parent 526c1a1 commit 6ec30da
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/features/home_view/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import "package:flutter/material.dart";
import "../../config/ui_config.dart";
import "../../theme/app_theme.dart";
import "../academic_calendar/widgets/academic_calendar_consumer.dart";
import "../sks_people_live/presentation/sks_app_bar.dart";
import "keep_alive_home_view_providers.dart";
import "widgets/buildings_section/buildings_section.dart";
import "widgets/departments_section/departments_section.dart";
import "widgets/logo_app_bar.dart";
import "widgets/news_section.dart";
import "widgets/parkings_section.dart";
import "widgets/science_clubs_section.dart";
Expand Down Expand Up @@ -38,7 +38,7 @@ class HomeView extends StatelessWidget {
child: SafeArea(
child: Padding(
padding: EdgeInsets.only(left: horizontalPadding),
child: LogoAppBar(context),
child: SksAppBar(context),
),
),
),
Expand Down
11 changes: 1 addition & 10 deletions lib/features/parkings_view/repository/parkings_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,14 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:riverpod_annotation/riverpod_annotation.dart";

import "../../../utils/ref_extensions.dart";
import "../api_client/iparking_client.dart";
import "../api_client/iparking_commands.dart";
import "../models/parking.dart";
import "local_fav_parking_repository.dart";

part "parkings_repository.g.dart";

extension RefIntervalRefreshX on Ref {
void setRefresh(Duration interval) {
final timer = Timer.periodic(
interval,
(t) => invalidateSelf(),
);
onDispose(timer.cancel);
}
}

@riverpod
Future<IList<Parking>> parkingsRepository(Ref ref) async {
ref.setRefresh(ParkingsConfig.parkingsRefreshInterval);
Expand Down
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,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 lib/features/sks_people_live/data/sks_people_live_consts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class SksPeopleRepositoryConfig {
static const defaultSksRefreshTime = Duration(minutes: 1);
}
12 changes: 12 additions & 0 deletions lib/utils/ref_extensions.dart
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);
}
}

0 comments on commit 6ec30da

Please sign in to comment.