Browse Source

added time bucket classification

nayooti 5 năm trước cách đây
mục cha
commit
4e987b76a0

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

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

+ 5 - 36
deltachat-ios/Controller/GalleryViewController.swift

@@ -1,41 +1,6 @@
 import UIKit
 import UIKit
 import DcCore
 import DcCore
 
 
-enum TimeBucket: CaseIterable {
-    case today
-    case yesterday
-    case thisWeek
-    case lastWeek
-    case thisMonth
-    case lastMonth
-}
-
-extension TimeBucket {
-
-    static func bucket(for date: Date) -> TimeBucket {
-        // TODO: calculate here
-
-        return date.bucket()
-    }
-
-    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"
-        }
-    }
-}
-
 class GalleryViewController: UIViewController {
 class GalleryViewController: UIViewController {
 
 
     private struct GallerySection {
     private struct GallerySection {
@@ -152,7 +117,11 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
     }
     }
 
 
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-        let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier: GalleryCell.reuseIdentifier, for: indexPath) as! GalleryCell
+        guard let mediaCell = collectionView.dequeueReusableCell(
+            withReuseIdentifier: GalleryCell.reuseIdentifier,
+            for: indexPath) as? GalleryCell else {
+            return UICollectionViewCell()
+        }
         let msg = DcMsg(id: mediaMessageIds[indexPath.row])
         let msg = DcMsg(id: mediaMessageIds[indexPath.row])
         mediaCell.update(msg: msg)
         mediaCell.update(msg: msg)
         // cell update
         // cell update

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

@@ -2,57 +2,43 @@ import Foundation
 
 
 extension Date {
 extension Date {
 
 
-    func bucket() -> TimeBucket {
-        if Calendar.current.isDateInToday(self) {
-            return .today
-        }
-        if Calendar.current.isDateInYesterday(self) {
-            return .yesterday
-        }
-
-        let today = Date()
-        let earliest = self
-        let latest = today
-
-        let differenceComponents: DateComponents = Calendar.current.dateComponents (
-            [.day, .weekOfYear, .month, .year],
-            from: earliest,
-            to: latest
-        )
-
-        let todayComponents: DateComponents = Calendar.current.dateComponents(
-            [.day, .weekOfYear, .month, .year],
-            from: today
-        )
+    func isEqual(
+        to date: Date,
+        toGranularity component: Calendar.Component,
+        in calendar: Calendar = .current
+    ) -> Bool {
+        calendar.isDate(self, equalTo: date, toGranularity: component)
+    }
 
 
-        let dateComponents: DateComponents = Calendar.current.dateComponents(
-            [.day, .weekOfYear, .month, .year],
-            from: self
-        )
+    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) }
 
 
-        let year = differenceComponents.year ?? 0
-        let month = differenceComponents.month ?? 0
-        let week = differenceComponents.weekOfYear ?? 0
+    func isInSameDay(as date: Date) -> Bool { Calendar.current.isDate(self, inSameDayAs: date) }
 
 
-        if year > 1 || month > 1 || month == 1 {
-            return .lastMonth
+    var isInThisYear: Bool { isInSameYear(as: Date()) }
+    var isInThisMonth: Bool { isInSameMonth(as: Date()) }
+    var isInThisWeek: Bool { isInSameWeek(as: Date()) }
+    var isInLastWeek: Bool {
+        guard let lastWeekDate = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: Date()) else { return false
         }
         }
+        return isEqual(to: lastWeekDate, toGranularity: .weekOfYear)
+    }
 
 
-        if week > 1 {
-            return .thisMonth
+    var isInLastMonth: Bool {
+        guard let lastMonthDate = Calendar.current.date(byAdding: .month, value: -1, to: Date()) else {
+            return false
         }
         }
+        return isEqual(to: lastMonthDate, toGranularity: .month)
+    }
 
 
 
 
+    var isInYesterday: Bool { Calendar.current.isDateInYesterday(self) }
+    var isInToday: Bool { Calendar.current.isDateInToday(self) }
+    var isInTomorrow: Bool { Calendar.current.isDateInTomorrow(self) }
 
 
-        if month == 1 && dateComponents.month == todayComponents.month {
-            return .thisMonth
-        }
-
-        if week > 1 {
-            return .lastWeek
-        }
-
+    var isInTheFuture: Bool { self > Date() }
+    var isInThePast: Bool { self < Date() }
 
 
 
 
-    }
 }
 }

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

@@ -0,0 +1,59 @@
+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"
+        }
+    }
+}