summaryrefslogtreecommitdiffstats
path: root/lib/data/models/date_time.dart
blob: 62d774314bd91d805a864e8316786469569c0348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import 'dart:convert';

import 'package:flutter_ics_homescreen/export.dart';
import 'package:intl/intl.dart';

@immutable
class DateAndTime {
  final String date;
  final String time;
  const DateAndTime({
    required this.date,
    required this.time,
  });

  DateAndTime.initial()
      : date = DateFormat().add_yMMMMd().format(DateTime.now()),
        time = DateFormat('hh:mm a').format(DateTime.now());
  DateAndTime copyWith({
    String? date,
    String? time,
  }) {
    return DateAndTime(
      date: date ?? this.date,
      time: time ?? this.time,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'date': date,
      'time': time,
    };
  }

  factory DateAndTime.fromMap(Map<String, dynamic> map) {
    return DateAndTime(
      date: map['date'],
      time: map['time'],
    );
  }

  String toJson() => json.encode(toMap());

  factory DateAndTime.fromJson(String source) =>
      DateAndTime.fromMap(json.decode(source));

  @override
  String toString() => 'DateAndTime(date: $date, time: $time)';

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is DateAndTime && other.date == date && other.time == time;
  }

  @override
  int get hashCode => date.hashCode ^ time.hashCode;
}