Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat/ add favorite parkings feature #193

Merged
merged 7 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 2 additions & 6 deletions lib/api_base/ttl/local_timestamp_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "package:riverpod_annotation/riverpod_annotation.dart";
import "package:shared_preferences/shared_preferences.dart";

import "../../config/env.dart";
import "../../config/shared_prefs.dart";
import "../../config/ttl_config.dart";
import "../../utils/timestamp.dart";
import "ttl_timestamp.dart";
Expand Down Expand Up @@ -30,16 +31,11 @@ class LocalTimestampRepository {
}
}

@Riverpod(keepAlive: true)
Future<SharedPreferences> _prefs(_PrefsRef ref) async {
return SharedPreferences.getInstance();
}

@riverpod
Future<LocalTimestampRepository> localTimestampRepository(
LocalTimestampRepositoryRef ref,
TtlKey key,
) async {
final prefs = await ref.watch(_prefsProvider.future);
final prefs = await ref.watch(sharedPreferencesSingletonProvider.future);
return LocalTimestampRepository(key, prefs);
}
11 changes: 11 additions & 0 deletions lib/config/shared_prefs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "package:riverpod_annotation/riverpod_annotation.dart";
import "package:shared_preferences/shared_preferences.dart";

part "shared_prefs.g.dart";

@Riverpod(keepAlive: true)
Future<SharedPreferences> sharedPreferencesSingleton(
SharedPreferencesSingletonRef ref,
) {
return SharedPreferences.getInstance();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import "package:riverpod_annotation/riverpod_annotation.dart";

import "../../../config/shared_prefs.dart";

part "local_fav_parking_repository.g.dart";

@riverpod
class LocalFavParkingsRepository extends _$LocalFavParkingsRepository {
static const _repoPrefix = "fav_parking_";

String get _key => _repoPrefix + parkingId;

@override
bool? build(String parkingId) {
final prefs = ref.watch(sharedPreferencesSingletonProvider);
return switch (prefs) {
AsyncError() => null,
AsyncValue(:final value) => value?.getBool(_key) ?? false,
};
}

Future<void> toggle() async {
final currState = state ?? false;
final newState = !currState;
state = newState;
final prefs = await ref.read(sharedPreferencesSingletonProvider.future);
await prefs.setBool(_key, newState);
}
}
47 changes: 47 additions & 0 deletions lib/features/parkings_view/widgets/parking_favourite.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import "package:flutter/material.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";

import "../../../theme/app_theme.dart";
import "../../../theme/iparking_theme.dart";
import "../models/parking.dart";
import "../repository/local_fav_parking_repository.dart";

class FavouriteParkingWidget extends ConsumerWidget {
const FavouriteParkingWidget(this.parking);
final Parking parking;
@override
Widget build(BuildContext context, WidgetRef ref) {
final favController =
ref.watch(localFavParkingsRepositoryProvider(parking.id).notifier);
final isFavorite =
ref.watch(localFavParkingsRepositoryProvider(parking.id));
return IconButton(
visualDensity: VisualDensity.compact,
onPressed: favController.toggle,
icon: isFavorite == null
? const FavouriteIcon(
icon: Icons.error,
)
: FavouriteIcon(
icon: isFavorite
? Icons.favorite_rounded
: Icons.favorite_border_outlined,
),
);
}
}

class FavouriteIcon extends StatelessWidget {
const FavouriteIcon({super.key, required this.icon});
final IconData icon;

@override
Widget build(BuildContext context) {
return Icon(
icon,
color: context.colorTheme.whiteSoap,
size: 22,
shadows: iparkingShadows,
);
}
}
22 changes: 7 additions & 15 deletions lib/features/parkings_view/widgets/parking_wide_tile_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "../../../theme/iparking_theme.dart";
import "../../../utils/context_extensions.dart";
import "../../parking_chart/parking_chart.dart";
import "../models/parking.dart";
import "parkings_icons.icons.dart";
import "parking_favourite.dart";

class ParkingWideTileCard extends StatelessWidget {
const ParkingWideTileCard({
Expand Down Expand Up @@ -52,6 +52,11 @@ class ParkingWideTileCard extends StatelessWidget {
padding: ParkingsConfig.padding,
child: _RightColumn(parking, isActive: isActive),
),
Positioned(
top: 1,
right: 2,
child: FavouriteParkingWidget(parking),
),
],
),
),
Expand Down Expand Up @@ -118,21 +123,8 @@ class _RightColumn extends StatelessWidget {
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisAlignment: MainAxisAlignment.end,
children: [
if (!isActive)
const Icon(
ParkingsIcons.ipark_info,
color: Colors.white,
size: 25,
shadows: iparkingShadows,
),
if (isActive)
const Icon(
ParkingsIcons.ip_close,
color: Colors.white,
size: 20,
),
Text(
parking.counterText,
style: isActive
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies:
enum_map: ^0.3.0
google_maps_flutter: ^2.6.1
url_launcher: ^6.2.4
shared_preferences: ^2.2.2
shared_preferences: ^2.3.2
dio: ^5.4.3+1
freezed_annotation: ^2.4.1
json_annotation: ^4.9.0
Expand Down