Browser-Tab-Manager/lib/models/tab_data.dart

81 lines
2.4 KiB
Dart
Raw Normal View History

2025-10-23 03:49:28 +02:00
// Class creating objects "Class Declaration" The Blueprint
// ? means can be null or nothing "Operators"
// ?? means if null, use this instead
class TabData {
2025-10-23 03:49:28 +02:00
// FIELDS for storing data "Fields"
// What we will be uding in other parts of the code
String id;
String title;
String url;
String favicon;
DateTime lastAccessed;
bool isPinned;
String type;
int? visitCount;
2025-10-23 03:49:28 +02:00
String? folder;
2025-10-23 03:49:28 +02:00
// Constructor Declaration "Constructor Declaration" The Assembly Line
TabData({
2025-10-23 03:49:28 +02:00
// Parameters saved from the data coming in
// "this." current object
required this.id,
required this.title,
required this.url,
this.favicon = '',
DateTime? lastAccessed,
this.isPinned = false,
this.type = 'tab',
this.visitCount,
this.folder,
2025-10-23 03:49:28 +02:00
})
// initializer logic, AFTER parameters, BEFORE Constructor
// ?? IF left is null, use right
// left side is field, right side is parameter the new saved value
: lastAccessed = lastAccessed ?? DateTime.now();
2025-10-23 03:49:28 +02:00
// factory logic before constructor, from json so we don't have to extract later.
factory TabData.fromJson(Map<String, dynamic> json) => TabData(
2025-10-23 03:49:28 +02:00
// Extract data pass to regular constructor
id: json['id'].toString(),
title: json['title'] ?? 'Untitled',
url: json['url'] ?? '',
favicon: json['favicon'] ?? json['favIconUrl'] ?? '',
lastAccessed: json['lastAccessed'] != null
? DateTime.parse(json['lastAccessed'])
: (json['lastVisitTime'] != null
? DateTime.parse(json['lastVisitTime'])
: (json['dateAdded'] != null
? DateTime.parse(json['dateAdded'])
: (json['timestamp'] != null
? DateTime.parse(json['timestamp'])
: DateTime.now()))),
isPinned: json['isPinned'] ?? false,
type: json['type'] ?? 'tab',
visitCount: json['visitCount'],
folder: json['folder'],
);
}
2025-10-23 03:49:28 +02:00
// Data model single tab, bookmark, or history item.
//
2025-10-23 03:49:28 +02:00
// Blueprint storing browser item information.
//
2025-10-23 03:49:28 +02:00
// Every tab ,bookmark is stored as a TabData object.
//
2025-10-23 03:49:28 +02:00
// Constructor creates new TabData objects.
//
2025-10-23 03:49:28 +02:00
// Favicon defaults to empty string
//
// isPinned defaults to false.
//
2025-10-23 03:49:28 +02:00
// FromJson factory data browser API into TabData object.
//
2025-10-23 03:49:28 +02:00
// Missing data, default values ??.
//
2025-10-23 03:49:28 +02:00
// Foundation: managing/storing tabs.