499 lines
11 KiB
Dart
499 lines
11 KiB
Dart
import 'package:sqflite/sqflite.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'local_database.dart';
|
|
|
|
class LocalPreference {
|
|
|
|
static Future<void> setSelectedCityId(int value) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.insert(
|
|
'selected_city',
|
|
{
|
|
'id': 1,
|
|
'city_id': value,
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
static Future<int> getSelectedCityId() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'selected_city',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return result.first['city_id'] as int;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/// Insert default onboarding row (call once in splash)
|
|
static Future<void> initOnboarding() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query('onboarding_state');
|
|
|
|
if (result.isEmpty) {
|
|
await db.insert(
|
|
'onboarding_state',
|
|
{
|
|
'id': 1,
|
|
'is_first_time': 1, // true
|
|
'page': 0,
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Get onboarding page
|
|
static Future<int> getOnboardingPage() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'onboarding_state',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return result.first['page'] as int;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/// Get isFirstTime value
|
|
static Future<bool> isFirstTimeUser() async {
|
|
final page = await getOnboardingPage();
|
|
return page < 3;
|
|
}
|
|
|
|
/// Move to next onboarding page
|
|
static Future<void> updateOnboardingPage(int page) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.update(
|
|
'onboarding_state',
|
|
{
|
|
'page': page,
|
|
'is_first_time': page < 3 ? 1 : 0,
|
|
},
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
}
|
|
|
|
/// Reset onboarding (for logout / testing)
|
|
static Future<void> resetOnboarding() async {
|
|
await updateOnboardingPage(0);
|
|
}
|
|
|
|
/// Set login state
|
|
static Future<void> setLogin(bool value) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.insert(
|
|
'login_state',
|
|
{
|
|
'id': 1,
|
|
'is_logged_in': value ? 1 : 0,
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
/// Get login state
|
|
static Future<bool> getLogin() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'login_state',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return result.first['is_logged_in'] == 1;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static Future<void> clearLogin() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.update(
|
|
'login_state',
|
|
{'is_logged_in': 0},
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
}
|
|
|
|
|
|
/// Set user tokens
|
|
static Future<void> setTokens({
|
|
required String accessToken,
|
|
required String refreshToken,
|
|
required int refreshTokenMaxAge,
|
|
}) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.insert(
|
|
'user_tokens',
|
|
{
|
|
'id': 1,
|
|
'access_token': accessToken,
|
|
'refresh_token': refreshToken,
|
|
'refresh_token_max_age': refreshTokenMaxAge,
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
/// Update only access token (for refresh token flow)
|
|
static Future<void> setAccessToken(String accessToken) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.update(
|
|
'user_tokens',
|
|
{'access_token': accessToken},
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
}
|
|
|
|
/// Get access token
|
|
static Future<String?> getAccessToken() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'user_tokens',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return result.first['access_token'] as String?;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Get refresh token
|
|
static Future<String?> getRefreshToken() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'user_tokens',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return result.first['refresh_token'] as String?;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Clear tokens (for logout)
|
|
static Future<void> clearTokens() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.delete(
|
|
'user_tokens',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
}
|
|
|
|
/// Set user details
|
|
static Future<void> setUserDetails({
|
|
required int userId,
|
|
required String firstName,
|
|
required String lastName,
|
|
required String fullName,
|
|
required String emailAddress,
|
|
required String role,
|
|
required int roleId,
|
|
String? profileImage, // Added optional profileImage parameter
|
|
}) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.insert(
|
|
'user_details',
|
|
{
|
|
'id': 1,
|
|
'user_id': userId,
|
|
'first_name': firstName,
|
|
'last_name': lastName,
|
|
'full_name': fullName,
|
|
'email_address': emailAddress,
|
|
'role': role,
|
|
'role_id': roleId,
|
|
'profile_image': profileImage, // Include profile image
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
|
|
/// Get userId
|
|
static Future<int?> getUserId() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'user_details',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return result.first['user_id'] as int;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Set profile image with error handling
|
|
static Future<void> setProfileImage(String imageUrl) async {
|
|
try {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.update(
|
|
'user_details',
|
|
{'profile_image': imageUrl},
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (kDebugMode) {
|
|
print('✅ [LOCAL_PREF] Profile image saved: $imageUrl');
|
|
print('📊 [LOCAL_PREF] Rows affected: $result');
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('❌ [LOCAL_PREF] Error saving profile image: $e');
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
/// Get profile image
|
|
static Future<String?> getProfileImage() async {
|
|
try {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'user_details',
|
|
columns: ['profile_image'],
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
final imageUrl = result.first['profile_image'] as String?;
|
|
if (kDebugMode && imageUrl != null) {
|
|
print('✅ [LOCAL_PREF] Retrieved profile image: $imageUrl');
|
|
}
|
|
return imageUrl;
|
|
}
|
|
return null;
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('❌ [LOCAL_PREF] Error getting profile image: $e');
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Set pass cart data
|
|
static Future<void> setPassCart({
|
|
required String cityName,
|
|
required String heroImage,
|
|
required String cardTypeName,
|
|
required String cardDisplayName,
|
|
required int themeColor,
|
|
required int adultCount,
|
|
required int childCount,
|
|
required double adultPrice,
|
|
required double childPrice,
|
|
required int validityDuration,
|
|
required double totalPrice,
|
|
String? description,
|
|
}) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.insert(
|
|
'pass_cart',
|
|
{
|
|
'id': 1,
|
|
'city_name': cityName,
|
|
'hero_image': heroImage,
|
|
'card_type_name': cardTypeName,
|
|
'card_display_name': cardDisplayName,
|
|
'theme_color': themeColor,
|
|
'adult_count': adultCount,
|
|
'child_count': childCount,
|
|
'adult_price': adultPrice,
|
|
'child_price': childPrice,
|
|
'validity_duration': validityDuration,
|
|
'total_price': totalPrice,
|
|
'description': description,
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
|
|
if (kDebugMode) {
|
|
print('✅ [LOCAL_PREF] Pass cart saved: $cardDisplayName for $cityName');
|
|
}
|
|
}
|
|
|
|
/// Get pass cart data
|
|
static Future<Map<String, dynamic>?> getPassCart() async {
|
|
try {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'pass_cart',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
if (kDebugMode) {
|
|
print('✅ [LOCAL_PREF] Retrieved pass cart data');
|
|
}
|
|
return result.first;
|
|
}
|
|
return null;
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('❌ [LOCAL_PREF] Error getting pass cart: $e');
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<void> clearPassCart() async {
|
|
try {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.delete(
|
|
'pass_cart',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (kDebugMode) {
|
|
print('✅ [LOCAL_PREF] Pass cart cleared');
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('❌ [LOCAL_PREF] Error clearing pass cart: $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
static Future<void> setSelectedCityLogo(String logoUrl) async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.update(
|
|
'selected_city',
|
|
{'city_logo': logoUrl},
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
}
|
|
|
|
static Future<String?> getSelectedCityLogo() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.query(
|
|
'selected_city',
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return result.first['city_logo'] as String?;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static Future<void> clearUserDetails() async {
|
|
final db = await LocalDatabase().database;
|
|
|
|
await db.update(
|
|
'user_details',
|
|
{
|
|
'user_id': null,
|
|
'first_name': '',
|
|
'last_name': '',
|
|
'full_name': '',
|
|
'email_address': '',
|
|
'role': '',
|
|
'role_id': 0,
|
|
'profile_image': null,
|
|
},
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
}
|
|
|
|
|
|
static Future<void> clearProfileImage() async {
|
|
try {
|
|
final db = await LocalDatabase().database;
|
|
|
|
final result = await db.update(
|
|
'user_details',
|
|
{'profile_image': null},
|
|
where: 'id = ?',
|
|
whereArgs: [1],
|
|
);
|
|
|
|
if (kDebugMode) {
|
|
print('🧹 [LOCAL_PREF] Profile image cleared');
|
|
print('📊 [LOCAL_PREF] Rows affected: $result');
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('❌ [LOCAL_PREF] Error clearing profile image: $e');
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
static Future<void> resetAppData() async {
|
|
await clearLogin();
|
|
await clearTokens();
|
|
await clearUserDetails();
|
|
await clearPassCart();// optional
|
|
await clearProfileImage();// optional
|
|
await clearPassCart();// optional
|
|
}
|
|
|
|
static Future<void> clearAllData() async {
|
|
try {
|
|
final db = await LocalDatabase().database;
|
|
|
|
// Clear all tables
|
|
await db.delete('selected_city');
|
|
await db.delete('login_state');
|
|
await db.delete('user_tokens');
|
|
await db.delete('user_details');
|
|
await db.delete('pass_cart');
|
|
|
|
if (kDebugMode) {
|
|
print('🧹 [LOCAL_PREF] All local data cleared successfully');
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('❌ [LOCAL_PREF] Error clearing all local data: $e');
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
} |