feat: make core pages local-first offline
This commit is contained in:
@@ -244,6 +244,20 @@ class TxApi {
|
||||
static bool get _queueOfflineChanges =>
|
||||
SessionStore.instance.isAccount && SessionStore.instance.cloudSyncEnabled;
|
||||
|
||||
static MonthSummary monthLocal(int year, int month) => MonthSummary.fromJson(
|
||||
LocalDatabase.instance.monthSummary(year, month, _ledgerId),
|
||||
);
|
||||
|
||||
static Future<MonthSummary> monthRemote(int year, int month) async {
|
||||
final response = await _dio.get(
|
||||
'/api/transactions/month',
|
||||
queryParameters: {'year': year, 'month': month, 'ledgerId': _ledgerId},
|
||||
);
|
||||
final json = response.data as Map<String, dynamic>;
|
||||
LocalDatabase.instance.cacheMonthSummary(json);
|
||||
return MonthSummary.fromJson(json);
|
||||
}
|
||||
|
||||
static Future<MonthSummary> month(int year, int month) async {
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly) {
|
||||
@@ -330,6 +344,29 @@ class TxApi {
|
||||
}
|
||||
}
|
||||
|
||||
static PeriodStats periodStatsLocal(String period, DateTime anchor) =>
|
||||
PeriodStats.fromJson(
|
||||
LocalDatabase.instance.periodStats(period, anchor, _ledgerId),
|
||||
);
|
||||
|
||||
static Future<PeriodStats> periodStatsRemote(
|
||||
String period,
|
||||
DateTime anchor,
|
||||
) async {
|
||||
final response = await _dio.get(
|
||||
'/api/transactions/stats/period',
|
||||
queryParameters: {
|
||||
'period': period,
|
||||
'anchor':
|
||||
'${anchor.year.toString().padLeft(4, '0')}-'
|
||||
'${anchor.month.toString().padLeft(2, '0')}-'
|
||||
'${anchor.day.toString().padLeft(2, '0')}',
|
||||
'ledgerId': _ledgerId,
|
||||
},
|
||||
);
|
||||
return PeriodStats.fromJson(response.data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
static Future<List<CategoryItem>> categories(String type) async {
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly) {
|
||||
@@ -357,6 +394,24 @@ class TxApi {
|
||||
}
|
||||
}
|
||||
|
||||
static List<CategoryItem> categoriesLocal(String type) => LocalDatabase
|
||||
.instance
|
||||
.categories(type)
|
||||
.map(CategoryItem.fromJson)
|
||||
.toList();
|
||||
|
||||
static Future<List<CategoryItem>> categoriesRemote(String type) async {
|
||||
final response = await _dio.get(
|
||||
'/api/categories',
|
||||
queryParameters: {'type': type},
|
||||
);
|
||||
final values = response.data as List;
|
||||
LocalDatabase.instance.cacheCategories(values, replaceType: type);
|
||||
return values
|
||||
.map((item) => CategoryItem.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
static Future<TxItem> create({
|
||||
required int categoryId,
|
||||
required String type,
|
||||
@@ -532,7 +587,9 @@ class TxApi {
|
||||
includeDeleted: true,
|
||||
)?['updatedAt'];
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly || id < 0) {
|
||||
if (session.shouldUseLocalOnly ||
|
||||
ApiClient.availability.value == BackendAvailability.offline ||
|
||||
id < 0) {
|
||||
LocalDatabase.instance.softDeleteTransaction(id);
|
||||
if (_queueOfflineChanges) {
|
||||
LocalDatabase.instance.enqueueSync('transaction', id, 'delete', {
|
||||
@@ -613,7 +670,8 @@ class TxApi {
|
||||
|
||||
static Future<List<TxItem>> recycleBin() async {
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly) {
|
||||
if (session.shouldUseLocalOnly ||
|
||||
ApiClient.availability.value == BackendAvailability.offline) {
|
||||
return LocalDatabase.instance
|
||||
.recycleBin(_ledgerId)
|
||||
.map(TxItem.fromJson)
|
||||
@@ -638,13 +696,32 @@ class TxApi {
|
||||
}
|
||||
}
|
||||
|
||||
static List<TxItem> recycleBinLocal() => LocalDatabase.instance
|
||||
.recycleBin(_ledgerId)
|
||||
.map(TxItem.fromJson)
|
||||
.toList();
|
||||
|
||||
static Future<List<TxItem>> recycleBinRemote() async {
|
||||
final response = await _dio.get(
|
||||
'/api/transactions/recycle-bin',
|
||||
queryParameters: {'ledgerId': _ledgerId},
|
||||
);
|
||||
final values = response.data as List;
|
||||
LocalDatabase.instance.cacheTransactions(values);
|
||||
return values
|
||||
.map((item) => TxItem.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
static Future<TxItem> restore(int id) async {
|
||||
final baseUpdatedAt = LocalDatabase.instance.transaction(
|
||||
id,
|
||||
includeDeleted: true,
|
||||
)?['updatedAt'];
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly || id < 0) {
|
||||
if (session.shouldUseLocalOnly ||
|
||||
ApiClient.availability.value == BackendAvailability.offline ||
|
||||
id < 0) {
|
||||
final local = LocalDatabase.instance.restoreTransaction(id);
|
||||
if (_queueOfflineChanges) {
|
||||
LocalDatabase.instance.enqueueSync('transaction', id, 'restore', {
|
||||
@@ -984,6 +1061,18 @@ class BudgetApi {
|
||||
static final _dio = ApiClient.instance.dio;
|
||||
static int get _ledgerId => CurrentLedgerStore.instance.currentId ?? 1;
|
||||
|
||||
static BudgetsData getLocal(int year, int month) => BudgetsData.fromJson(
|
||||
LocalDatabase.instance.budgets(year, month, _ledgerId),
|
||||
);
|
||||
|
||||
static Future<BudgetsData> getRemote(int year, int month) async {
|
||||
final response = await _dio.get(
|
||||
'/api/budgets',
|
||||
queryParameters: {'year': year, 'month': month, 'ledgerId': _ledgerId},
|
||||
);
|
||||
return BudgetsData.fromJson(response.data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
static Future<BudgetsData> get(int year, int month) async {
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly) {
|
||||
@@ -1294,6 +1383,29 @@ class ReportApi {
|
||||
static final _dio = ApiClient.instance.dio;
|
||||
static int get _ledgerId => CurrentLedgerStore.instance.currentId ?? 1;
|
||||
|
||||
static PeriodReport local(String period, DateTime anchor) =>
|
||||
_localPeriod(period, anchor);
|
||||
|
||||
static Future<PeriodReport> remote(String period, DateTime anchor) async {
|
||||
if (period == 'month') {
|
||||
return PeriodReport.fromMonthly(await monthly(anchor.year, anchor.month));
|
||||
}
|
||||
final path = period == 'week'
|
||||
? '/api/reports/weekly'
|
||||
: '/api/reports/yearly';
|
||||
final query = period == 'week'
|
||||
? {
|
||||
'date':
|
||||
'${anchor.year.toString().padLeft(4, '0')}-'
|
||||
'${anchor.month.toString().padLeft(2, '0')}-'
|
||||
'${anchor.day.toString().padLeft(2, '0')}',
|
||||
'ledgerId': _ledgerId,
|
||||
}
|
||||
: {'year': anchor.year, 'ledgerId': _ledgerId};
|
||||
final response = await _dio.get(path, queryParameters: query);
|
||||
return PeriodReport.fromJson(response.data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
static Future<PeriodReport> weekly(DateTime date) async {
|
||||
if (SessionStore.instance.shouldUseLocalOnly) {
|
||||
return _localPeriod('week', date);
|
||||
@@ -1595,7 +1707,8 @@ class CategoryApi {
|
||||
'type': type,
|
||||
};
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly) {
|
||||
if (session.shouldUseLocalOnly ||
|
||||
ApiClient.availability.value == BackendAvailability.offline) {
|
||||
final local = LocalDatabase.instance.createCategory(
|
||||
name,
|
||||
iconKey,
|
||||
@@ -1649,7 +1762,9 @@ class CategoryApi {
|
||||
'sortOrder': sortOrder,
|
||||
};
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly || id < 0) {
|
||||
if (session.shouldUseLocalOnly ||
|
||||
ApiClient.availability.value == BackendAvailability.offline ||
|
||||
id < 0) {
|
||||
final local = LocalDatabase.instance.updateCategory(
|
||||
id,
|
||||
name,
|
||||
@@ -1662,16 +1777,31 @@ class CategoryApi {
|
||||
}
|
||||
return CategoryItem.fromJson(local);
|
||||
}
|
||||
final response = await _dio.put('/api/categories/$id', data: payload);
|
||||
final json = response.data as Map<String, dynamic>;
|
||||
LocalDatabase.instance.cacheCategories([json]);
|
||||
return CategoryItem.fromJson(json);
|
||||
try {
|
||||
final response = await _dio.put('/api/categories/$id', data: payload);
|
||||
final json = response.data as Map<String, dynamic>;
|
||||
LocalDatabase.instance.cacheCategories([json]);
|
||||
return CategoryItem.fromJson(json);
|
||||
} catch (error) {
|
||||
if (!isConnectivityError(error) || !session.isAccount) rethrow;
|
||||
final local = LocalDatabase.instance.updateCategory(
|
||||
id,
|
||||
name,
|
||||
iconKey,
|
||||
colorKey,
|
||||
sortOrder,
|
||||
);
|
||||
LocalDatabase.instance.enqueueSync('category', id, 'update', payload);
|
||||
return CategoryItem.fromJson(local);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> reorder(String type, List<int> categoryIds) async {
|
||||
LocalDatabase.instance.reorderCategories(type, categoryIds);
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly || categoryIds.any((id) => id < 0)) {
|
||||
if (session.shouldUseLocalOnly ||
|
||||
ApiClient.availability.value == BackendAvailability.offline ||
|
||||
categoryIds.any((id) => id < 0)) {
|
||||
if (session.isAccount && session.cloudSyncEnabled) {
|
||||
LocalDatabase.instance.enqueueSync('category', 0, 'reorder', {
|
||||
'type': type,
|
||||
@@ -1680,15 +1810,25 @@ class CategoryApi {
|
||||
}
|
||||
return;
|
||||
}
|
||||
await _dio.put(
|
||||
'/api/categories/reorder',
|
||||
data: {'type': type, 'categoryIds': categoryIds},
|
||||
);
|
||||
try {
|
||||
await _dio.put(
|
||||
'/api/categories/reorder',
|
||||
data: {'type': type, 'categoryIds': categoryIds},
|
||||
);
|
||||
} catch (error) {
|
||||
if (!isConnectivityError(error) || !session.isAccount) rethrow;
|
||||
LocalDatabase.instance.enqueueSync('category', 0, 'reorder', {
|
||||
'type': type,
|
||||
'categoryIds': categoryIds,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> delete(int id) async {
|
||||
final session = SessionStore.instance;
|
||||
if (session.shouldUseLocalOnly || id < 0) {
|
||||
if (session.shouldUseLocalOnly ||
|
||||
ApiClient.availability.value == BackendAvailability.offline ||
|
||||
id < 0) {
|
||||
LocalDatabase.instance.deleteCategory(id);
|
||||
if (session.isAccount && session.cloudSyncEnabled) {
|
||||
LocalDatabase.instance.enqueueSync('category', id, 'delete', {
|
||||
@@ -1697,7 +1837,13 @@ class CategoryApi {
|
||||
}
|
||||
return;
|
||||
}
|
||||
await _dio.delete('/api/categories/$id');
|
||||
LocalDatabase.instance.deleteCategory(id);
|
||||
try {
|
||||
await _dio.delete('/api/categories/$id');
|
||||
LocalDatabase.instance.deleteCategory(id);
|
||||
} catch (error) {
|
||||
if (!isConnectivityError(error) || !session.isAccount) rethrow;
|
||||
LocalDatabase.instance.deleteCategory(id);
|
||||
LocalDatabase.instance.enqueueSync('category', id, 'delete', {'id': id});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user