Skip to content

Commit

Permalink
Merge pull request #132 from canopas/Mayank/fix-analyzer-error
Browse files Browse the repository at this point in the history
Fix analyzer error
  • Loading branch information
cp-mayank authored Nov 8, 2024
2 parents e885497 + 7e06944 commit 23a576e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 37 deletions.
2 changes: 1 addition & 1 deletion data/.flutter-plugins-dependencies

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions data/lib/service/auth/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
import '../../errors/app_error.dart';
import '../../extensions/string_extensions.dart';
import '../../storage/app_preferences.dart';
import '../../storage/provider/preferences_provider.dart';
import '../user/user_service.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -25,8 +26,8 @@ class AuthService {
final FirebaseAuth _auth;
final UserService _userService;

final StateController<String?> _currentUserNotifier;
final StateController<String?> _userSessionNotifier;
final PreferenceNotifier<String?> _currentUserNotifier;
final PreferenceNotifier<String?> _userSessionNotifier;

AuthService(
this._auth,
Expand Down
63 changes: 41 additions & 22 deletions data/lib/storage/provider/preferences_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,49 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';

final sharedPreferencesProvider =
Provider<SharedPreferences>((ref) => throw UnimplementedError());
Provider<SharedPreferences>((ref) => throw UnimplementedError());

StateProvider<T> createPrefProvider<T>({
StateNotifierProvider<PreferenceNotifier<T>, T> createPrefProvider<T>({
required String prefKey,
required T defaultValue,
}) {
return StateProvider((ref) {
final prefs = ref.watch(sharedPreferencesProvider);
final currentValue = prefs.get(prefKey) as T? ?? defaultValue;
ref.listenSelf((prev, curr) {
if (curr == null) {
prefs.remove(prefKey);
} else if (curr is String) {
prefs.setString(prefKey, curr);
} else if (curr is bool) {
prefs.setBool(prefKey, curr);
} else if (curr is int) {
prefs.setInt(prefKey, curr);
} else if (curr is double) {
prefs.setDouble(prefKey, curr);
} else if (curr is List<String>) {
prefs.setStringList(prefKey, curr);
}
});
return currentValue;
});
return StateNotifierProvider<PreferenceNotifier<T>, T>(
(ref) => PreferenceNotifier<T>(
ref.watch(sharedPreferencesProvider).get(prefKey) as T? ?? defaultValue,
(curr) {
final prefs = ref.watch(sharedPreferencesProvider);
if (curr == null) {
prefs.remove(prefKey);
} else if (curr is String) {
prefs.setString(prefKey, curr);
} else if (curr is bool) {
prefs.setBool(prefKey, curr);
} else if (curr is int) {
prefs.setInt(prefKey, curr);
} else if (curr is double) {
prefs.setDouble(prefKey, curr);
} else if (curr is List<String>) {
prefs.setStringList(prefKey, curr);
}
},
),
);
}

class PreferenceNotifier<T> extends StateNotifier<T> {
Function(T curr)? onUpdate;

PreferenceNotifier(
super.value,
this.onUpdate,
);

@override
set state(T value) {
super.state = value;
onUpdate?.call(value);
}

@override
T get state => super.state;
}
4 changes: 2 additions & 2 deletions data/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ dependencies:
uuid: ^4.5.1

# state management
flutter_riverpod: 2.5.3
hooks_riverpod: ^2.5.4
flutter_riverpod: ^2.6.1
hooks_riverpod: ^2.6.1

dev_dependencies:
flutter_test:
Expand Down
3 changes: 2 additions & 1 deletion khelo/lib/ui/flow/main/main_screen_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:data/api/user/user_models.dart';
import 'package:data/service/auth/auth_service.dart';
import 'package:data/storage/app_preferences.dart';
import 'package:data/storage/provider/preferences_provider.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -20,7 +21,7 @@ final mainScreenStateNotifierProvider =
});

class MainScreenStateNotifier extends StateNotifier<MainScreenState> {
final StateController<String?> _lastNotificationPermissionPromptDate;
final PreferenceNotifier<String?> _lastNotificationPermissionPromptDate;
final UserModel? _currentUser;

final AuthService _authService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:data/service/match/match_service.dart';
import 'package:data/service/team/team_service.dart';
import 'package:data/service/user/user_service.dart';
import 'package:data/storage/app_preferences.dart';
import 'package:data/storage/provider/preferences_provider.dart';
import 'package:data/utils/constant/firebase_storage_constant.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -39,7 +40,7 @@ class EditProfileViewNotifier extends StateNotifier<EditProfileState> {
final AuthService _authService;
final TeamService _teamService;
final MatchService _matchService;
final StateController<String?> _userJsonController;
final PreferenceNotifier<String?> _userJsonController;

EditProfileViewNotifier(
this._fileUploadService,
Expand Down
12 changes: 6 additions & 6 deletions khelo/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,10 @@ packages:
dependency: "direct main"
description:
name: flutter_riverpod
sha256: "711d916456563f715bde1e139d7cfdca009f8264befab3ac9f8ded8b6ec26405"
sha256: "9532ee6db4a943a1ed8383072a2e3eeda041db5657cdf6d2acecf3c21ecbe7e1"
url: "https://pub.dev"
source: hosted
version: "2.5.3"
version: "2.6.1"
flutter_svg:
dependency: "direct main"
description:
Expand Down Expand Up @@ -781,10 +781,10 @@ packages:
dependency: "direct main"
description:
name: hooks_riverpod
sha256: "534ca8af331235f700f3916a74e4579cdfdd708e2fbed342fc8aa969d41a7af0"
sha256: "70bba33cfc5670c84b796e6929c54b8bc5be7d0fe15bb28c2560500b9ad06966"
url: "https://pub.dev"
source: hosted
version: "2.5.4"
version: "2.6.1"
hotreloader:
dependency: transitive
description:
Expand Down Expand Up @@ -1261,10 +1261,10 @@ packages:
dependency: transitive
description:
name: riverpod
sha256: c86fedfb45dd1da98ee6493dd9374325cdf494e7d523ebfb0c387eecc5f7b5c9
sha256: "59062512288d3056b2321804332a13ffdd1bf16df70dcc8e506e411280a72959"
url: "https://pub.dev"
source: hosted
version: "2.5.3"
version: "2.6.1"
riverpod_analyzer_utils:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions khelo/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ dependencies:
firebase_messaging: ^15.1.3

# state management
flutter_riverpod: 2.5.3
hooks_riverpod: 2.5.4
flutter_riverpod: ^2.6.1
hooks_riverpod: ^2.6.1

# navigation
go_router: ^14.3.0
Expand Down

0 comments on commit 23a576e

Please sign in to comment.