Getting the Cache Instance
Copy
import 'package:zeus_app/cache/cache.dart';
// Get the singleton instance
final cache = CacheManager.instance;
Creating Entities
Create a Wallet
Copy
final wallet = await cache.createWallet(
name: 'Cash',
currency: 'USD',
initialBalance: 100.0,
);
print('Created wallet: ${wallet.id}');
Create a Category
Copy
final category = await cache.createCategory(
name: 'Food',
type: 'expense', // or 'income'
color: 0xFFFF5722,
icon: 'restaurant',
);
Create a Transaction
Copy
final transaction = await cache.createTransaction(
walletId: wallet.id,
categoryId: category.id,
amount: 25.50,
type: 'expense',
description: 'Lunch',
);
Querying Data
Get All Wallets
Copy
final wallets = await cache.wallets.findAll();
for (final wallet in wallets) {
print('${wallet.name}: ${wallet.balance}');
}
Get Transactions by Wallet
Copy
final transactions = await cache.transactions.findByWallet(walletId);
Get Transactions by Date Range
Copy
final now = DateTime.now();
final startOfMonth = DateTime(now.year, now.month, 1);
final monthlyTransactions = await cache.transactions.findByDateRange(
startOfMonth,
now,
);
Get Categories by Type
Copy
final expenseCategories = await cache.categories.findByType('expense');
final incomeCategories = await cache.categories.findByType('income');
Updating Entities
Copy
// Get existing entity
final wallet = await cache.wallets.findById(walletId);
// Modify
final updated = wallet!.copyWith(
name: 'Updated Name',
balance: 150.0,
);
// Save (automatically marks as pendingUpdate)
await cache.wallets.save(updated);
Deleting Entities
Soft Delete (Default)
Marks as deleted but keeps for sync:Copy
await cache.wallets.delete(walletId);
Hard Delete
Removes immediately:Copy
await cache.wallets.delete(walletId, hardDelete: true);
Generic Cache
For key-value storage:Copy
// Store
await cache.put('user_settings', '{"theme": "dark"}');
// With TTL
await cache.put('temp_token', token, ttl: Duration(hours: 1));
// Retrieve
final settings = await cache.get<String>('user_settings');
// Delete
await cache.delete('user_settings');
Sync Operations
Manual Sync
Copy
final result = await cache.sync();
if (result.success) {
print('Synced successfully');
} else {
print('Sync failed: ${result.error}');
}
Listen to Sync Progress
Copy
cache.syncProgress?.listen((progress) {
print('${progress.step}: ${progress.percent}%');
});
Check Sync Status
Copy
if (cache.isSyncing) {
print('Sync in progress...');
}
Complete Example
Copy
class TransactionService {
final CacheManager _cache;
TransactionService(this._cache);
Future<void> addExpense({
required String walletId,
required double amount,
required String categoryId,
String? description,
}) async {
// Create transaction
final transaction = await _cache.createTransaction(
walletId: walletId,
categoryId: categoryId,
amount: amount,
type: 'expense',
description: description,
);
// Update wallet balance
final wallet = await _cache.wallets.findById(walletId);
if (wallet != null) {
await _cache.wallets.save(
wallet.copyWith(
balance: wallet.balance - amount,
),
);
}
// Trigger sync
await _cache.sync();
}
Future<List<Transaction>> getMonthlyTransactions() async {
final now = DateTime.now();
final startOfMonth = DateTime(now.year, now.month, 1);
return _cache.transactions.findByDateRange(
startOfMonth,
now,
);
}
}

