Files
tasq/lib/models/announcement.model.dart
T
2026-04-27 06:20:39 +08:00

96 lines
3.3 KiB
Dart

import 'package:brick_offline_first_with_supabase/brick_offline_first_with_supabase.dart';
import 'package:brick_sqlite/brick_sqlite.dart';
import 'package:brick_supabase/brick_supabase.dart';
import '../utils/app_time.dart';
@ConnectOfflineFirstWithSupabase(
supabaseConfig: SupabaseSerializable(tableName: 'announcements'),
)
class Announcement extends OfflineFirstWithSupabaseModel {
final String id;
final String authorId;
final String title;
final String body;
final List<String> visibleRoles;
final bool isTemplate;
final String? templateId;
final DateTime createdAt;
final DateTime updatedAt;
final bool bannerEnabled;
final DateTime? bannerShowAt;
final DateTime? bannerHideAt;
final int? pushIntervalMinutes;
@Supabase(ignore: true)
@Sqlite(ignore: true)
bool get isBannerActive {
if (!bannerEnabled) return false;
final now = AppTime.now();
if (bannerShowAt != null && now.isBefore(bannerShowAt!)) return false;
if (bannerHideAt != null && now.isAfter(bannerHideAt!)) return false;
return true;
}
Announcement({
required this.id,
required this.authorId,
required this.title,
required this.body,
required this.visibleRoles,
required this.isTemplate,
this.templateId,
required this.createdAt,
required this.updatedAt,
required this.bannerEnabled,
this.bannerShowAt,
this.bannerHideAt,
this.pushIntervalMinutes,
});
@Supabase(ignore: true)
@Sqlite(ignore: true)
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Announcement &&
runtimeType == other.runtimeType &&
id == other.id && authorId == other.authorId &&
title == other.title && body == other.body &&
isTemplate == other.isTemplate && templateId == other.templateId &&
bannerEnabled == other.bannerEnabled &&
bannerShowAt == other.bannerShowAt &&
bannerHideAt == other.bannerHideAt &&
pushIntervalMinutes == other.pushIntervalMinutes &&
createdAt == other.createdAt && updatedAt == other.updatedAt;
@Supabase(ignore: true)
@Sqlite(ignore: true)
@override
int get hashCode => Object.hash(id, authorId, title, body, isTemplate,
templateId, bannerEnabled, bannerShowAt, bannerHideAt,
pushIntervalMinutes, createdAt, updatedAt);
factory Announcement.fromMap(Map<String, dynamic> map) {
final rolesRaw = map['visible_roles'];
final roles = rolesRaw is List ? rolesRaw.cast<String>() : <String>[];
return Announcement(
id: map['id'] as String,
authorId: map['author_id'] as String,
title: map['title'] as String? ?? '',
body: map['body'] as String? ?? '',
visibleRoles: roles,
isTemplate: map['is_template'] as bool? ?? false,
templateId: map['template_id'] as String?,
createdAt: AppTime.parse(map['created_at'] as String),
updatedAt: AppTime.parse(map['updated_at'] as String),
bannerEnabled: map['banner_enabled'] as bool? ?? false,
bannerShowAt: map['banner_show_at'] != null
? AppTime.parse(map['banner_show_at'] as String) : null,
bannerHideAt: map['banner_hide_at'] != null
? AppTime.parse(map['banner_hide_at'] as String) : null,
pushIntervalMinutes: map['push_interval_minutes'] as int?,
);
}
}