Prechádzať zdrojové kódy

date extension prepared

nayooti 5 rokov pred
rodič
commit
6920193a8e

+ 0 - 4
deltachat-ios.xcodeproj/project.pbxproj

@@ -142,7 +142,6 @@
 		AE1988AB23EB3C7600B4CD5F /* Assets in Resources */ = {isa = PBXBuildFile; fileRef = AE1988AA23EB3C7600B4CD5F /* Assets */; };
 		AE25F09022807AD800CDEA66 /* AvatarSelectionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE25F08F22807AD800CDEA66 /* AvatarSelectionCell.swift */; };
 		AE38B31822672DFC00EC37A1 /* ActionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE38B31722672DFC00EC37A1 /* ActionCell.swift */; };
-		AE3FA04424793B8B004B5F30 /* TimeBucket.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE3FA04324793B8B004B5F30 /* TimeBucket.swift */; };
 		AE406EF0240FF8FF005F7022 /* ProfileCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE406EEF240FF8FF005F7022 /* ProfileCell.swift */; };
 		AE4AEE3522B1030D000AA495 /* PreviewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE4AEE3422B1030D000AA495 /* PreviewController.swift */; };
 		AE52EA19229EB53C00C586C9 /* ContactDetailHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE52EA18229EB53C00C586C9 /* ContactDetailHeader.swift */; };
@@ -427,7 +426,6 @@
 		AE1988AA23EB3C7600B4CD5F /* Assets */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Assets; sourceTree = "<group>"; };
 		AE25F08F22807AD800CDEA66 /* AvatarSelectionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarSelectionCell.swift; sourceTree = "<group>"; };
 		AE38B31722672DFC00EC37A1 /* ActionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionCell.swift; sourceTree = "<group>"; };
-		AE3FA04324793B8B004B5F30 /* TimeBucket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeBucket.swift; sourceTree = "<group>"; };
 		AE406EEF240FF8FF005F7022 /* ProfileCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileCell.swift; sourceTree = "<group>"; };
 		AE4AEE3422B1030D000AA495 /* PreviewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewController.swift; sourceTree = "<group>"; };
 		AE52EA18229EB53C00C586C9 /* ContactDetailHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactDetailHeader.swift; sourceTree = "<group>"; };
@@ -942,7 +940,6 @@
 				AEFBE23023FF09B20045327A /* TypeAlias.swift */,
 				307D822D241669C7006D2490 /* LocationManager.swift */,
 				AE0AA9552478191900D42A7F /* GridCollectionViewFlowLayout.swift */,
-				AE3FA04324793B8B004B5F30 /* TimeBucket.swift */,
 			);
 			path = Helper;
 			sourceTree = "<group>";
@@ -1482,7 +1479,6 @@
 				AE38B31822672DFC00EC37A1 /* ActionCell.swift in Sources */,
 				AE9DAF0D22C1215D004C9591 /* EditContactController.swift in Sources */,
 				305961FC2346125100C80F33 /* MessageContainerView.swift in Sources */,
-				AE3FA04424793B8B004B5F30 /* TimeBucket.swift in Sources */,
 				305961D42346125100C80F33 /* MessagesViewController.swift in Sources */,
 				785BE16821E247F1003BE98C /* MessageInfoViewController.swift in Sources */,
 				AE851AC5227C755A00ED86F0 /* Protocols.swift in Sources */,

+ 42 - 16
deltachat-ios/Extensions/Date+Extension.swift

@@ -1,7 +1,37 @@
 import Foundation
 
+
 extension Date {
 
+    var galleryLocalizedDescription: String {
+        if isInToday {
+            return String.localized("today")
+        }
+        if isInYesterday {
+            return String.localized("yesterday")
+        }
+        if isInThisWeek {
+            return String.localized("this_week")
+        }
+        if isInLastWeek {
+            return String.localized("last_week")
+        }
+        if isInThisMonth {
+            return String.localized("this_month")
+        }
+        if isInLastMonth {
+            return String.localized("last_month")
+        }
+
+        let monthName = DateFormatter().monthSymbols[month - 1]
+        let yearName: String = isInSameYear(as: Date()) ? "" : " \(year)"
+        return "\(monthName)\(yearName)"
+    }
+
+}
+
+private extension Date {
+
     func isEqual(
         to date: Date,
         toGranularity component: Calendar.Component,
@@ -10,14 +40,8 @@ extension Date {
         calendar.isDate(self, equalTo: date, toGranularity: component)
     }
 
-    func isInSameYear(as date: Date) -> Bool { isEqual(to: date, toGranularity: .year) }
-    func isInSameMonth(as date: Date) -> Bool { isEqual(to: date, toGranularity: .month) }
-    func isInSameWeek(as date: Date) -> Bool { isEqual(to: date, toGranularity: .weekOfYear) }
-
-    func isInSameDay(as date: Date) -> Bool { Calendar.current.isDate(self, inSameDayAs: date) }
-
-    var isInThisYear: Bool { isInSameYear(as: Date()) }
-    var isInThisMonth: Bool { isInSameMonth(as: Date()) }
+    var isInToday: Bool { Calendar.current.isDateInToday(self) }
+    var isInYesterday: Bool { Calendar.current.isDateInYesterday(self) }
     var isInThisWeek: Bool { isInSameWeek(as: Date()) }
     var isInLastWeek: Bool {
         guard let lastWeekDate = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: Date()) else {
@@ -25,7 +49,7 @@ extension Date {
         }
         return isEqual(to: lastWeekDate, toGranularity: .weekOfYear)
     }
-
+    var isInThisMonth: Bool { isInSameMonth(as: Date()) }
     var isInLastMonth: Bool {
         guard let lastMonthDate = Calendar.current.date(byAdding: .month, value: -1, to: Date()) else {
             return false
@@ -33,13 +57,15 @@ extension Date {
         return isEqual(to: lastMonthDate, toGranularity: .month)
     }
 
+    var month: Int {
+       return Calendar.current.component(.month, from: self)
+    }
 
-    var isInYesterday: Bool { Calendar.current.isDateInYesterday(self) }
-    var isInToday: Bool { Calendar.current.isDateInToday(self) }
-    var isInTomorrow: Bool { Calendar.current.isDateInTomorrow(self) }
-
-    var isInTheFuture: Bool { self > Date() }
-    var isInThePast: Bool { self < Date() }
-
+    var year: Int {
+        return Calendar.current.component(.year, from: self)
+    }
 
+    func isInSameMonth(as date: Date) -> Bool { isEqual(to: date, toGranularity: .month) }
+    func isInSameWeek(as date: Date) -> Bool { isEqual(to: date, toGranularity: .weekOfYear) }
+    func isInSameYear(as date: Date) -> Bool { isEqual(to: date, toGranularity: .year) }
 }

+ 0 - 59
deltachat-ios/Helper/TimeBucket.swift

@@ -1,59 +0,0 @@
-import Foundation
-
-enum TimeBucket: CaseIterable {
-    case today
-    case yesterday
-    case thisWeek
-    case lastWeek
-    case thisMonth
-    case lastMonth
-}
-
-extension TimeBucket {
-
-    static func bucket(for date: Date) -> TimeBucket {
-
-        if date.isInToday {
-            return .today
-        }
-
-        if date.isInYesterday {
-            return .yesterday
-        }
-        if date.isInThisWeek {
-            return .thisWeek
-        }
-
-        if date.isInLastWeek {
-            return .lastWeek
-        }
-
-        if date.isInThisMonth {
-            return .thisMonth
-        }
-
-        if date.isInLastMonth {
-            return .lastMonth
-        }
-
-        // TODO: handle months
-        return .lastMonth
-    }
-
-    var translationKey: String {
-        switch self {
-        case .today:
-            return "today"
-        case .yesterday:
-            return "yesterday"
-        case .thisWeek:
-            return "this_week"
-        case .lastWeek:
-            return "last_week"
-        case .thisMonth:
-            return "this_month"
-        case .lastMonth:
-            return "last_month"
-        }
-    }
-}