Browse Source

add all-media UIPageViewController and UISegmentedControl

B. Petersen 2 years ago
parent
commit
d75f5ebda4

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

@@ -208,6 +208,7 @@
 		B26B3BC7236DC3DC008ED35A /* SwitchCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B26B3BC6236DC3DC008ED35A /* SwitchCell.swift */; };
 		B2C42570265C325C00B95377 /* MultilineLabelCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C4256F265C325C00B95377 /* MultilineLabelCell.swift */; };
 		B2D4B63B29C38D1900B47DA8 /* ChatsAndMediaViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2D4B63A29C38D1900B47DA8 /* ChatsAndMediaViewController.swift */; };
+		B2F899E129F96A67003797D5 /* AllMediaViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2F899E029F96A67003797D5 /* AllMediaViewController.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -573,6 +574,7 @@
 		B2D729E927C57B9000A4E0BE /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/InfoPlist.strings"; sourceTree = "<group>"; };
 		B2D729EA27C57B9000A4E0BE /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
 		B2D729EB27C57B9000A4E0BE /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.stringsdict"; sourceTree = "<group>"; };
+		B2F899E029F96A67003797D5 /* AllMediaViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AllMediaViewController.swift; sourceTree = "<group>"; };
 		C1B60449B860342EE5F2AD54 /* Pods-DcShare.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DcShare.release.xcconfig"; path = "Pods/Target Support Files/Pods-DcShare/Pods-DcShare.release.xcconfig"; sourceTree = "<group>"; };
 		FECB35E2B04CD5F5D02C157A /* Pods-deltachat-iosTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-iosTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-iosTests/Pods-deltachat-iosTests.release.xcconfig"; sourceTree = "<group>"; };
 /* End PBXFileReference section */
@@ -949,6 +951,7 @@
 			children = (
 				B28D25882913CE8600B9067F /* Settings */,
 				AE18F28B228C17630007B1BE /* AccountSetup */,
+				B2F899E029F96A67003797D5 /* AllMediaViewController.swift */,
 				B259D64229B771D5008FB706 /* BackupTransferViewController.swift */,
 				30DDCBEA28FCA21800465D22 /* AccountSwitchViewController.swift */,
 				30DDCBE828FCA1F900465D22 /* PartialScreenPresentationController.swift */,
@@ -1516,6 +1519,7 @@
 				AE19887523EB264000B4CD5F /* HelpViewController.swift in Sources */,
 				AE0D26FD1FB1FE88002FAFCE /* ChatListController.swift in Sources */,
 				30C7D5EC28F47E620078D24C /* InitialsBadge.swift in Sources */,
+				B2F899E129F96A67003797D5 /* AllMediaViewController.swift in Sources */,
 				302D5450268B6B2300A8B271 /* MessageUtils.swift in Sources */,
 				30149D9322F21129003C12B5 /* QrViewController.swift in Sources */,
 				AEE56D80225504DB007DC082 /* Extensions.swift in Sources */,

+ 100 - 0
deltachat-ios/Controller/AllMediaViewController.swift

@@ -0,0 +1,100 @@
+import UIKit
+import DcCore
+
+class AllMediaViewController: UIPageViewController {
+    private let dcContext: DcContext
+
+    private var selectedIndex: Int = 0
+
+    private lazy var segmentControl: UISegmentedControl = {
+        let control = UISegmentedControl(
+            items: [String.localized("images_and_videos"), String.localized("files")]
+        )
+        control.tintColor = DcColors.primary
+        control.addTarget(self, action: #selector(segmentControlChanged), for: .valueChanged)
+        control.selectedSegmentIndex = 0
+        return control
+    }()
+
+    init(dcAccounts: DcAccounts) {
+        self.dcContext = dcAccounts.getSelected()
+        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:])
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    // MARK: - lifecycle
+    override func viewDidLoad() {
+        super.viewDidLoad()
+        dataSource = self
+        delegate = self
+        navigationItem.titleView = segmentControl
+
+        setViewControllers(
+            [makeGalleryViewController()],
+            direction: .forward,
+            animated: true,
+            completion: nil
+        )
+    }
+
+    override func viewWillAppear(_ animated: Bool) {
+        // viewWillAppear() is on called on section change, not on main-tab change
+        super.viewWillAppear(animated)
+
+    }
+
+    override func viewWillDisappear(_ animated: Bool) {
+        // viewWillDisappear() is on called on section change, not on main-tab change
+        super.viewWillDisappear(animated)
+    }
+
+    // MARK: - actions
+    @objc private func segmentControlChanged(_ sender: UISegmentedControl) {
+        if sender.selectedSegmentIndex == 0 {
+            setViewControllers([makeGalleryViewController()], direction: .reverse, animated: true, completion: nil)
+        } else {
+            setViewControllers([makeFilesViewController()], direction: .forward, animated: true, completion: nil)
+        }
+    }
+
+    // MARK: - factory
+    private func makeGalleryViewController() -> UIViewController {
+        let allMedia = dcContext.getChatMedia(chatId: 0, messageType: DC_MSG_IMAGE, messageType2: DC_MSG_GIF, messageType3: DC_MSG_VIDEO)
+        return GalleryViewController(context: dcContext, chatId: 0, mediaMessageIds: allMedia.reversed())
+    }
+
+    private func makeFilesViewController() -> UIViewController {
+        let allMedia = dcContext.getChatMedia(chatId: 0, messageType: DC_MSG_FILE, messageType2: DC_MSG_AUDIO, messageType3: DC_MSG_WEBXDC)
+        return DocumentGalleryController(context: dcContext, chatId: 0, fileMessageIds: allMedia.reversed(), hasWebxdc: false)
+    }
+}
+
+// MARK: - UIPageViewControllerDataSource, UIPageViewControllerDelegate
+extension AllMediaViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {
+    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
+        if viewController is GalleryViewController {
+            return nil
+        }
+        return makeGalleryViewController()
+    }
+
+    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
+        if viewController is GalleryViewController {
+            return makeFilesViewController()
+        }
+        return nil
+    }
+
+    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
+        if completed {
+            if previousViewControllers.first is GalleryViewController {
+                segmentControl.selectedSegmentIndex = 1
+            } else {
+                segmentControl.selectedSegmentIndex = 0
+            }
+        }
+    }
+}

+ 1 - 1
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -43,7 +43,7 @@ class AppCoordinator {
     }
 
     private func createAllMediaNavigationController() -> UINavigationController {
-        let root = QrPageController(dcAccounts: dcAccounts)
+        let root = AllMediaViewController(dcAccounts: dcAccounts)
         let nav = UINavigationController(rootViewController: root)
         let settingsImage: UIImage?
         if #available(iOS 16.0, *) {