-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
534 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'activity_log_manager.dart'; | ||
export 'log_class.dart'; | ||
export 'log_enum.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:altme/activity_log/activity_log.dart'; | ||
import 'package:secure_storage/secure_storage.dart'; | ||
|
||
class ActivityLogManager { | ||
ActivityLogManager(SecureStorageProvider secureStorageProvider) | ||
: _secureStorageProvider = secureStorageProvider; | ||
|
||
final SecureStorageProvider _secureStorageProvider; | ||
|
||
static const int maxLogsPerBatch = 100; | ||
|
||
/// Get logs from single batch | ||
Future<List<LogData>> _getLogsForCurrentBatch(int index) async { | ||
final logDataJson = await _secureStorageProvider.get('log_batch_$index'); | ||
|
||
if (logDataJson != null) { | ||
final logStrings = logDataJson.split('\n'); | ||
return logStrings | ||
.map( | ||
(logString) => LogData.fromJson( | ||
jsonDecode(logString) as Map<String, dynamic>, | ||
), | ||
) | ||
.toList(); | ||
} | ||
return []; | ||
} | ||
|
||
/// Set log entry | ||
Future<void> writeLog(LogData log) async { | ||
final currentBatchIndex = await _getCurrentBatchIndex(); | ||
|
||
List<LogData> logs = await _getLogsForCurrentBatch(currentBatchIndex); | ||
|
||
// If the batch is full, increment the batch index and start a new batch | ||
if (logs.length >= maxLogsPerBatch) { | ||
await _incrementBatchIndex(); | ||
logs = []; | ||
} | ||
|
||
logs.add(log); | ||
|
||
final logJsonList = logs.map((log) => jsonEncode(log.toJson())).toList(); | ||
|
||
await _secureStorageProvider.set( | ||
'log_batch_$currentBatchIndex', | ||
logJsonList.join('\n'), | ||
); | ||
} | ||
|
||
/// Get logs from all batches | ||
Future<List<LogData>> readAllLogs() async { | ||
final currentBatchIndex = await _getCurrentBatchIndex(); | ||
final allLogs = <LogData>[]; | ||
|
||
for (int i = currentBatchIndex; i == 0; i--) { | ||
final logData = await _getLogsForCurrentBatch(i); | ||
allLogs.addAll(logData.reversed.toList()); | ||
} | ||
return allLogs; | ||
} | ||
|
||
// /// Get paginated logs | ||
// Future<List<LogData>> readLogs(int limit, int offset) async { | ||
// final allLogs = <LogData>[]; | ||
|
||
// final currentBatchIndex = await _getCurrentBatchIndex(); | ||
// // Retrieve logs from all batches | ||
// for (int i = 0; i <= currentBatchIndex; i++) { | ||
// final logData = await _secureStorageProvider.get('log_batch_$i'); | ||
// if (logData != null) { | ||
// final logStrings = logData.split('\n'); | ||
// final logs = logStrings | ||
// .map( | ||
// (logString) => LogData.fromJson( | ||
// jsonDecode(logString) as Map<String, dynamic>, | ||
// ), | ||
// ) | ||
// .toList(); | ||
// allLogs.addAll(logs); | ||
// } | ||
// } | ||
|
||
// // Apply pagination logic (limit and offset) | ||
// // but this logic needs to be improved.. it is paginating later.. | ||
// // we need to consider while fetching | ||
// return allLogs.skip(offset).take(limit).toList(); | ||
// } | ||
|
||
/// Clear all logs | ||
Future<void> clearLogs() async { | ||
final currentBatchIndex = await _getCurrentBatchIndex(); | ||
for (int i = 0; i <= currentBatchIndex; i++) { | ||
await _secureStorageProvider.delete('log_batch_$i'); | ||
} | ||
await _secureStorageProvider.delete('currentBatchIndex'); | ||
} | ||
|
||
/// get Current Batch | ||
Future<int> _getCurrentBatchIndex() async { | ||
final index = await _secureStorageProvider.get('currentBatchIndex'); | ||
if (index != null) { | ||
return int.parse(index); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
Future<void> _incrementBatchIndex() async { | ||
final currentBatchIndex = await _getCurrentBatchIndex(); | ||
final newBatchIndex = currentBatchIndex + 1; | ||
await _secureStorageProvider.set( | ||
'currentBatchIndex', | ||
newBatchIndex.toString(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:altme/activity_log/activity_log.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'log_class.g.dart'; | ||
|
||
@JsonSerializable() | ||
class LogData extends Equatable { | ||
const LogData({ | ||
required this.type, | ||
required this.timestamp, | ||
this.credentialId, | ||
this.data, | ||
}); | ||
|
||
factory LogData.fromJson(Map<String, dynamic> json) => | ||
_$LogDataFromJson(json); | ||
|
||
final LogType type; | ||
final DateTime timestamp; | ||
final String? credentialId; | ||
final String? data; | ||
|
||
Map<String, dynamic> toJson() => _$LogDataToJson(this); | ||
|
||
@override | ||
List<Object?> get props => [ | ||
type, | ||
timestamp, | ||
credentialId, | ||
data, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
enum LogType { | ||
walletInit, | ||
backupData, | ||
restoreWallet, | ||
addVC, | ||
deleteVC, | ||
presentVC, | ||
importKey, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'cubit/activity_log_cubit.dart'; | ||
export 'view/activity_log_page.dart'; |
28 changes: 28 additions & 0 deletions
28
lib/dashboard/drawer/activity_log/cubit/activity_log_cubit.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:altme/activity_log/activity_log.dart'; | ||
import 'package:altme/app/app.dart'; | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'activity_log_cubit.g.dart'; | ||
part 'activity_log_state.dart'; | ||
|
||
class ActivityLogCubit extends Cubit<ActivityLogState> { | ||
ActivityLogCubit({ | ||
required this.activityLogManager, | ||
}) : super(ActivityLogState()); | ||
|
||
final ActivityLogManager activityLogManager; | ||
|
||
final log = getLogger('ActivityLogCubit'); | ||
|
||
Future<void> getAllLogs() async { | ||
final logs = await activityLogManager.readAllLogs(); | ||
emit( | ||
state.copyWith( | ||
logDatas: logs, | ||
status: AppStatus.populate, | ||
), | ||
); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
lib/dashboard/drawer/activity_log/cubit/activity_log_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
part of 'activity_log_cubit.dart'; | ||
|
||
@JsonSerializable() | ||
class ActivityLogState extends Equatable { | ||
ActivityLogState({ | ||
this.status = AppStatus.init, | ||
List<LogData>? logDatas, | ||
}) : logDatas = logDatas ?? []; | ||
|
||
factory ActivityLogState.fromJson(Map<String, dynamic> json) => | ||
_$ActivityLogStateFromJson(json); | ||
|
||
final AppStatus status; | ||
final List<LogData> logDatas; | ||
|
||
ActivityLogState loading() { | ||
return copyWith(status: AppStatus.loading); | ||
} | ||
|
||
ActivityLogState copyWith({ | ||
AppStatus? status, | ||
List<LogData>? logDatas, | ||
}) { | ||
return ActivityLogState( | ||
status: status ?? this.status, | ||
logDatas: logDatas ?? this.logDatas, | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() => _$ActivityLogStateToJson(this); | ||
|
||
@override | ||
List<Object?> get props => [status, logDatas]; | ||
} |
Oops, something went wrong.