import 'package:test_sa/modules/notification_setting/models/notification_channel_model.dart'; class NotificationPreferenceResponse { final List? data; final int? totalRows; final int? count; final String? message; final String? title; final String? innerMessage; final int? responseCode; final bool? isSuccess; NotificationPreferenceResponse({ this.data, this.totalRows, this.count, this.message, this.title, this.innerMessage, this.responseCode, this.isSuccess, }); factory NotificationPreferenceResponse.fromJson(Map json) { return NotificationPreferenceResponse( data: json['data'] != null ? (json['data'] as List).map((e) => NotificationTemplate.fromJson(e)).toList() : null, totalRows: json['totalRows'], count: json['count'], message: json['message'], title: json['title'], innerMessage: json['innerMessage'], responseCode: json['responseCode'], isSuccess: json['isSuccess'], ); } Map toJson() { return { 'data': data?.map((e) => e.toJson()).toList(), 'totalRows': totalRows, 'count': count, 'message': message, 'title': title, 'innerMessage': innerMessage, 'responseCode': responseCode, 'isSuccess': isSuccess, }; } } class NotificationTemplate { final int? notificationTemplateId; final String? templateName; final String? moduleName; final String? subModuleName; final bool? mandatory; final bool? isDisabled; final String? channelNames; final bool? isCustom; final List? templateChannels; final List? preferenceChannels; // Local state for UI tracking bool isModified; List selectedChannelIds; NotificationTemplate({ this.notificationTemplateId, this.templateName, this.moduleName, this.subModuleName, this.mandatory, this.isDisabled, this.channelNames, this.isCustom, this.templateChannels, this.preferenceChannels, this.isModified = false, List? selectedChannelIds, }) : selectedChannelIds = selectedChannelIds ?? []; factory NotificationTemplate.fromJson(Map json) { final List? templateChannelsList = json['templateChannels'] != null ? (json['templateChannels'] as List).map((e) => NotificationChannel.fromJson(e)).toList() : null; final List? preferenceChannelsList = json['preferenceChannels'] != null ? (json['preferenceChannels'] as List).map((e) => NotificationChannel.fromJson(e)).toList() : null; // Initialize selectedChannelIds from preferenceChannels if available, otherwise from templateChannels List initialSelectedIds = []; if (preferenceChannelsList != null && preferenceChannelsList.isNotEmpty) { initialSelectedIds = preferenceChannelsList .where((e) => e.id != null) .map((e) => e.id!) .toList(); } else if (templateChannelsList != null && templateChannelsList.isNotEmpty) { initialSelectedIds = templateChannelsList .where((e) => e.id != null) .map((e) => e.id!) .toList(); } return NotificationTemplate( notificationTemplateId: json['notificationTemplateId'], templateName: json['templateName'], moduleName: json['moduleName'], subModuleName: json['subModuleName'], mandatory: json['mandatory'], isDisabled: json['isDisabled'], channelNames: json['channelNames'], isCustom: json['isCustom'], templateChannels: templateChannelsList, preferenceChannels: preferenceChannelsList, selectedChannelIds: initialSelectedIds, ); } Map toJson() { return { 'notificationTemplateId': notificationTemplateId, 'templateName': templateName, 'moduleName': moduleName, 'subModuleName': subModuleName, 'mandatory': mandatory, 'isDisabled': isDisabled, 'channelNames': channelNames, 'isCustom': isCustom, 'templateChannels': templateChannels?.map((e) => e.toJson()).toList(), 'preferenceChannels': preferenceChannels?.map((e) => e.toJson()).toList(), }; } // Create a copy with modified values NotificationTemplate copyWith({ int? notificationTemplateId, String? templateName, String? moduleName, String? subModuleName, bool? mandatory, bool? isDisabled, String? channelNames, bool? isCustom, List? templateChannels, List? preferenceChannels, bool? isModified, List? selectedChannelIds, }) { return NotificationTemplate( notificationTemplateId: notificationTemplateId ?? this.notificationTemplateId, templateName: templateName ?? this.templateName, moduleName: moduleName ?? this.moduleName, subModuleName: subModuleName ?? this.subModuleName, mandatory: mandatory ?? this.mandatory, isDisabled: isDisabled ?? this.isDisabled, channelNames: channelNames ?? this.channelNames, isCustom: isCustom ?? this.isCustom, templateChannels: templateChannels ?? this.templateChannels, preferenceChannels: preferenceChannels ?? this.preferenceChannels, isModified: isModified ?? this.isModified, selectedChannelIds: selectedChannelIds ?? this.selectedChannelIds, ); } }