瀏覽代碼

struggling with buckets

nayooti 5 年之前
父節點
當前提交
10b6052904

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

@@ -133,6 +133,7 @@
 		AE0AA952247800E700D42A7F /* GalleryCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0AA951247800E700D42A7F /* GalleryCell.swift */; };
 		AE0AA9542478010D00D42A7F /* GallerySectionHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0AA9532478010D00D42A7F /* GallerySectionHeader.swift */; };
 		AE0AA9562478191900D42A7F /* GridCollectionViewFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0AA9552478191900D42A7F /* GridCollectionViewFlowLayout.swift */; };
+		AE0AA958247834A400D42A7F /* Date+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0AA957247834A400D42A7F /* Date+Extension.swift */; };
 		AE0D26FD1FB1FE88002FAFCE /* ChatListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */; };
 		AE18F294228C602A0007B1BE /* SecuritySettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */; };
 		AE19887523EB264000B4CD5F /* HelpViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE19887423EB264000B4CD5F /* HelpViewController.swift */; };
@@ -416,6 +417,7 @@
 		AE0AA951247800E700D42A7F /* GalleryCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryCell.swift; sourceTree = "<group>"; };
 		AE0AA9532478010D00D42A7F /* GallerySectionHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GallerySectionHeader.swift; sourceTree = "<group>"; };
 		AE0AA9552478191900D42A7F /* GridCollectionViewFlowLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GridCollectionViewFlowLayout.swift; sourceTree = "<group>"; };
+		AE0AA957247834A400D42A7F /* Date+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extension.swift"; sourceTree = "<group>"; };
 		AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = ChatListController.swift; sourceTree = "<group>"; tabWidth = 4; };
 		AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecuritySettingsController.swift; sourceTree = "<group>"; };
 		AE19887423EB264000B4CD5F /* HelpViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpViewController.swift; sourceTree = "<group>"; };
@@ -596,6 +598,7 @@
 				3059620D234614E700C80F33 /* DcContact+Extension.swift */,
 				3059620F2346154D00C80F33 /* String+Extension.swift */,
 				302B84CD2397F6CD001C261F /* URL+Extension.swift */,
+				AE0AA957247834A400D42A7F /* Date+Extension.swift */,
 			);
 			path = Extensions;
 			sourceTree = "<group>";
@@ -1465,6 +1468,7 @@
 				300C50A1234BDAB800F8AE22 /* TextMediaMessageSizeCalculator.swift in Sources */,
 				30F9B9EC235F2116006E7ACF /* MessageCounter.swift in Sources */,
 				AE0AA952247800E700D42A7F /* GalleryCell.swift in Sources */,
+				AE0AA958247834A400D42A7F /* Date+Extension.swift in Sources */,
 				307D822E241669C7006D2490 /* LocationManager.swift in Sources */,
 				305961F12346125100C80F33 /* ContactMessageCell.swift in Sources */,
 				AE851AD0227DF50900ED86F0 /* GroupChatDetailViewController.swift in Sources */,

+ 61 - 3
deltachat-ios/Controller/GalleryViewController.swift

@@ -1,10 +1,48 @@
 import UIKit
 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 {
 
     private struct GallerySection {
-        let headerTitle: String?
+        var headerTitle: String {
+            return String.localized(timeBucket.translationKey)
+        }
+        let timeBucket: TimeBucket
         let msgIds: [Int]
     }
 
@@ -77,8 +115,28 @@ class GalleryViewController: UIViewController {
 
     // MARK: - data processing + update
     private func processData(msgIds: [Int]) -> [GallerySection] {
-        let section = GallerySection(headerTitle: String.localized("today"), msgIds: msgIds)
-        return [section]
+
+        var buckets: [TimeBucket: [Int]] = [:]
+
+        msgIds.forEach { msgId in
+            let msg = DcMsg(id: msgId)
+            let date: Date = msg.sentDate
+            let msgBucket = TimeBucket.bucket(for: date)
+
+            if buckets[msgBucket] != nil {
+                buckets[msgBucket]?.append(msgId)
+            } else {
+                buckets[msgBucket] = [msgId]
+            }
+        }
+
+        var sections: [GallerySection] = []
+        TimeBucket.allCases.forEach { bucket in
+            if let msgIds = buckets[bucket] {
+                sections.append(GallerySection(timeBucket: bucket, msgIds: msgIds))
+            }
+        }
+        return sections
     }
 }
 

+ 58 - 0
deltachat-ios/Extensions/Date+Extension.swift

@@ -0,0 +1,58 @@
+import Foundation
+
+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
+        )
+
+        let dateComponents: DateComponents = Calendar.current.dateComponents(
+            [.day, .weekOfYear, .month, .year],
+            from: self
+        )
+
+        let year = differenceComponents.year ?? 0
+        let month = differenceComponents.month ?? 0
+        let week = differenceComponents.weekOfYear ?? 0
+
+        if year > 1 || month > 1 || month == 1 {
+            return .lastMonth
+        }
+
+        if week > 1 {
+            return .thisMonth
+        }
+
+
+
+        if month == 1 && dateComponents.month == todayComponents.month {
+            return .thisMonth
+        }
+
+        if week > 1 {
+            return .lastWeek
+        }
+
+
+
+    }
+}

+ 2 - 0
deltachat-ios/Helper/Utils.swift

@@ -65,3 +65,5 @@ struct Utils {
         return String(lang)
     }
 }
+
+