Documentation Index
Fetch the complete documentation index at: https://docs-mstore.faisalaffan.com/llms.txt
Use this file to discover all available pages before exploring further.
Notification System
Push notifications dan in-app notifications
๐ฏ Overview
Notification adalah salah satu fitur utama dalam MStore Mobile yang menyediakan fungsionalitas untuk push notifications dan in-app notifications.
๐ Features
- โ
Push notifications (FCM)
- โ
In-app notifications
- โ
Low stock alerts
- โ
Transaction notifications
- โ
System announcements
- โ
Notification history
๐๏ธ Architecture
BLoC Implementation
BLoC: NotificationBloc, NotificationListBloc
// Events
abstract class NotificationEvent extends Equatable {}
class LoadNotification extends NotificationEvent {}
class CreateNotification extends NotificationEvent {}
class UpdateNotification extends NotificationEvent {}
class DeleteNotification extends NotificationEvent {}
// States
abstract class NotificationState extends Equatable {}
class NotificationInitial extends NotificationState {}
class NotificationLoading extends NotificationState {}
class NotificationLoaded extends NotificationState {}
class NotificationError extends NotificationState {}
// BLoC
class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
final NotificationRepository _repository;
NotificationBloc({required NotificationRepository repository})
: _repository = repository,
super(NotificationInitial()) {
on<LoadNotification>(_onLoad);
on<CreateNotification>(_onCreate);
on<UpdateNotification>(_onUpdate);
on<DeleteNotification>(_onDelete);
}
Future<void> _onLoad(
LoadNotification event,
Emitter<NotificationState> emit,
) async {
emit(NotificationLoading());
final result = await _repository.getNotifications();
result.fold(
(failure) => emit(NotificationError(failure.message)),
(data) => emit(NotificationLoaded(data)),
);
}
}
Repository Pattern
abstract class NotificationRepository {
Future<Either<Failure, List<Notification>>> getNotifications();
Future<Either<Failure, Notification>> getNotificationById(String id);
Future<Either<Failure, Notification>> createNotification(Notification data);
Future<Either<Failure, Notification>> updateNotification(String id, Notification data);
Future<Either<Failure, void>> deleteNotification(String id);
}
class NotificationRepositoryImpl implements NotificationRepository {
final NotificationApi _api;
final NotificationLocalRepository _localRepo;
@override
Future<Either<Failure, List<Notification>>> getNotifications() async {
try {
// Try local first (offline-first)
final local = await _localRepo.getNotifications();
// Sync with API in background
final result = await _api.getNotifications();
result.fold(
(failure) => null,
(data) => _localRepo.saveNotifications(data),
);
return Right(local.isNotEmpty ? local : result.getOrElse(() => []));
} catch (e) {
return Left(UnexpectedFailure(e.toString()));
}
}
}
๐ก API Integration
Endpoints
/api/v1/notifications/*
FCM
Request/Response Examples
Get List
GET /api/v1/notifications/*
Authorization: Bearer {access_token}
Response:
{
"success": true,
"data": [
{
"id": "123",
"name": "Example",
"created_at": "2024-10-14T10:00:00Z"
}
],
"pagination": {
"page": 1,
"limit": 20,
"total": 100
}
}
๐พ Local Database (Isar)
@collection
class NotificationLocal {
Id id = Isar.autoIncrement;
@Index()
String? notificationId;
String? name;
DateTime? createdAt;
DateTime? updatedAt;
DateTime? syncedAt;
bool? isSynced;
bool? isDeleted;
}
Queries
// Get all
final items = await isar.notificationLocals.where().findAll();
// Get by ID
final item = await isar.notificationLocals
.filter()
.notificationIdEqualTo(id)
.findFirst();
// Search
final results = await isar.notificationLocals
.filter()
.nameContains(query, caseSensitive: false)
.findAll();
// Get unsynced
final unsynced = await isar.notificationLocals
.filter()
.isSyncedEqualTo(false)
.findAll();
๐ Offline-First Strategy
Write Operations
- Save to local Isar immediately
- Show success to user
- Add to sync queue
- Background sync when online
- Update with server response
Read Operations
- Read from local Isar (fast)
- Show to user immediately
- Background fetch from API
- Update local cache if changed
- Notify UI if data updated
Conflict Resolution
- Strategy: Last-write-wins
- Timestamp: Server timestamp as source of truth
- Logging: All conflicts logged for audit
๐จ UI Components
Main Screen
class NotificationPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => getIt<NotificationBloc>()..add(LoadNotification()),
child: Scaffold(
appBar: AppBar(title: Text('Notification System')),
body: BlocBuilder<NotificationBloc, NotificationState>(
builder: (context, state) {
if (state is NotificationLoading) {
return Center(child: CircularProgressIndicator());
}
if (state is NotificationError) {
return ErrorWidget(message: state.message);
}
if (state is NotificationLoaded) {
return NotificationListView(items: state.items);
}
return SizedBox.shrink();
},
),
floatingActionButton: FloatingActionButton(
onPressed: () => _navigateToCreate(context),
child: Icon(Icons.add),
),
),
);
}
}
๐งช Testing
Unit Tests
void main() {
group('NotificationBloc', () {
late NotificationBloc bloc;
late MockNotificationRepository mockRepository;
setUp(() {
mockRepository = MockNotificationRepository();
bloc = NotificationBloc(repository: mockRepository);
});
tearDown(() {
bloc.close();
});
test('initial state is NotificationInitial', () {
expect(bloc.state, equals(NotificationInitial()));
});
blocTest<NotificationBloc, NotificationState>(
'emits [Loading, Loaded] when Load succeeds',
build: () {
when(() => mockRepository.getNotifications()).thenAnswer(
(_) async => Right([Notification(id: '1', name: 'Test')]),
);
return bloc;
},
act: (bloc) => bloc.add(LoadNotification()),
expect: () => [
NotificationLoading(),
isA<NotificationLoaded>(),
],
);
});
}
- Lazy Loading: Load data on demand
- Pagination: Implement pagination for large datasets
- Caching: Cache frequently accessed data
- Indexing: Use Isar indexes for fast queries
- Background Sync: Sync in background to avoid blocking UI
๐ Security
- Authorization: Check user permissions before operations
- Data Encryption: Sensitive data encrypted in Isar
- Input Validation: Validate all user inputs
- Audit Trail: Log all operations for audit
- Use Cupertino widgets
- Follow iOS HIG
- Handle safe area insets
Android
- Use Material widgets
- Follow Material Design
- Handle back button
Last Updated: October 14, 2024
Status: โ
Production Ready