244 lines
5.2 KiB
Dart
244 lines
5.2 KiB
Dart
import 'package:sqflite/sqflite.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;
|
|
}
|
|
|
|
/// 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,
|
|
}) 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,
|
|
},
|
|
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;
|
|
}
|
|
|
|
|
|
} |