Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll ChatList to top if already visible (#2125) #2127

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions DcCore/DcCore/Extensions/UITableView+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import UIKit

extension UITableView {
public func scrollToTop() {
let numberOfSections = self.numberOfSections
if numberOfSections > 0 {
let numberOfRows = self.numberOfRows(inSection: 0)
if numberOfRows > 0 {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.top, animated: false)
}
}
public func scrollToTop(animated: Bool = false) {
let numberOfSections = self.numberOfSections
if numberOfSections > 0 {
let numberOfRows = self.numberOfRows(inSection: 0)
if numberOfRows > 0 {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.top, animated: animated)
}
}
}
}
38 changes: 28 additions & 10 deletions deltachat-ios/Coordinator/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MobileCoreServices
import DcCore

// MARK: - AppCoordinator
class AppCoordinator {
class AppCoordinator: NSObject {

private let window: UIWindow
private let dcAccounts: DcAccounts
Expand All @@ -30,7 +30,7 @@ class AppCoordinator {
let chatsNavController = createChatsNavigationController()
let settingsNavController = createSettingsNavigationController()
let tabBarController = UITabBarController()
tabBarController.delegate = appStateRestorer
tabBarController.delegate = self
tabBarController.viewControllers = [allMediaNavController, qrNavController, chatsNavController, settingsNavController]
tabBarController.tabBar.tintColor = DcColors.primary
return tabBarController
Expand All @@ -39,29 +39,29 @@ class AppCoordinator {
private func createQrNavigationController() -> UINavigationController {
let root = QrPageController(dcAccounts: dcAccounts)
let nav = UINavigationController(rootViewController: root)
let settingsImage: UIImage?
let qrCodeTabImage: UIImage?
if #available(iOS 13.0, *) {
settingsImage = UIImage(systemName: "qrcode")
qrCodeTabImage = UIImage(systemName: "qrcode")
} else {
settingsImage = UIImage(named: "qr_code")
qrCodeTabImage = UIImage(named: "qr_code")
}
nav.tabBarItem = UITabBarItem(title: String.localized("qr_code"), image: settingsImage, tag: qrTab)
nav.tabBarItem = UITabBarItem(title: String.localized("qr_code"), image: qrCodeTabImage, tag: qrTab)
return nav
}

private func createAllMediaNavigationController() -> UINavigationController {
let root = AllMediaViewController(dcContext: dcAccounts.getSelected())
let nav = UINavigationController(rootViewController: root)
let settingsImage = UIImage(named: "photo.on.rectangle")
nav.tabBarItem = UITabBarItem(title: String.localized("menu_all_media"), image: settingsImage, tag: chatsTab)
let allMediaTabImage = UIImage(named: "photo.on.rectangle")
nav.tabBarItem = UITabBarItem(title: String.localized("menu_all_media"), image: allMediaTabImage, tag: allMediaTab)
return nav
}

private func createChatsNavigationController() -> UINavigationController {
let root = ChatListViewController(dcContext: dcAccounts.getSelected(), dcAccounts: dcAccounts, isArchive: false)
let nav = UINavigationController(rootViewController: root)
let settingsImage = UIImage(named: "ic_chat")
nav.tabBarItem = UITabBarItem(title: String.localized("pref_chats"), image: settingsImage, tag: chatsTab)
let chatTabImage = UIImage(named: "ic_chat")
nav.tabBarItem = UITabBarItem(title: String.localized("pref_chats"), image: chatTabImage, tag: chatsTab)
return nav
}

Expand All @@ -83,6 +83,7 @@ class AppCoordinator {
self.window = window
self.dcAccounts = dcAccounts
let dcContext = dcAccounts.getSelected()
super.init()
initializeRootController()

let lastActiveTab = appStateRestorer.restoreLastActiveTab()
Expand Down Expand Up @@ -272,3 +273,20 @@ class AppCoordinator {
presentTabBarController()
}
}

extension AppCoordinator: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let navigationController = viewController as? UINavigationController,
zeitschlag marked this conversation as resolved.
Show resolved Hide resolved
let chatListViewController = navigationController.viewControllers.first as? ChatListViewController,
let chatsTab = tabBarController.selectedViewController as? UINavigationController,
chatsTab.topViewController == chatListViewController {
chatListViewController.tableView.scrollToTop(animated: true)
}

return true
}

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
appStateRestorer.tabBarController(tabBarController, didSelect: viewController)
}
}
4 changes: 1 addition & 3 deletions deltachat-ios/Handler/AppStateRestorer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

class AppStateRestorer: NSObject, UITabBarControllerDelegate {
class AppStateRestorer {

private let lastActiveTabKey = "last_active_tab2"
private let lastActiveChatId = "last_active_chat_id"
Expand All @@ -16,8 +16,6 @@ class AppStateRestorer: NSObject, UITabBarControllerDelegate {
case firstLaunch = 0
}

private override init() {}

static let shared: AppStateRestorer = AppStateRestorer()

func restoreLastActiveTab() -> Int {
Expand Down