Skip to content

Commit

Permalink
WIP: Timer for reloading AI Chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Bunn committed Nov 28, 2024
1 parent 531f766 commit 4e07beb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
19 changes: 19 additions & 0 deletions DuckDuckGo/AIChat/AIChatModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,30 @@
//

import WebKit
import Combine

final class AIChatModel {
private var cleanupTimerCancellable: AnyCancellable?

let webViewConfiguration: WKWebViewConfiguration
let cleanupPublisher = PassthroughSubject<Void, Never>()

init(webViewConfiguration: WKWebViewConfiguration) {
self.webViewConfiguration = webViewConfiguration
}

func cancelTimer() {
cleanupTimerCancellable?.cancel()
}

func startCleanupTimer() {
print("Start timer")
cancelTimer()

cleanupTimerCancellable = Just(())
.delay(for: .seconds(5), scheduler: RunLoop.main)
.sink { [weak self] in
self?.cleanupPublisher.send()
}
}
}
61 changes: 51 additions & 10 deletions DuckDuckGo/AIChat/AIChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
//

import UIKit
import Combine

final class AIChatViewController: UIViewController {
private let chatModel: AIChatModel
private lazy var webViewController = AIChatWebViewController(chatModel: chatModel)
private var webViewController: AIChatWebViewController?
private var cleanupCancellable: AnyCancellable?

init(chatModel: AIChatModel) {
self.chatModel = chatModel
Expand All @@ -36,9 +38,19 @@ final class AIChatViewController: UIViewController {
super.viewDidLoad()

setupNavigationBar()
subscribeToCleanupPublisher()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
addWebViewController()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
chatModel.cancelTimer()
}

private func setupNavigationBar() {
let imageView = UIImageView(image: UIImage(named: "Logo"))
imageView.contentMode = .scaleAspectFit
Expand Down Expand Up @@ -72,23 +84,52 @@ final class AIChatViewController: UIViewController {
closeButton.tintColor = .label
navigationItem.rightBarButtonItem = closeButton
}

private func addWebViewController() {
addChild(webViewController)
view.addSubview(webViewController.view)
webViewController.view.translatesAutoresizingMaskIntoConstraints = false
guard webViewController == nil else {
print("WebViewController already exists, returning")
return
}

let newWebViewController = AIChatWebViewController(chatModel: chatModel)
webViewController = newWebViewController

addChild(newWebViewController)
view.addSubview(newWebViewController.view)
newWebViewController.view.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
webViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
webViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
webViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
webViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
newWebViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
newWebViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
newWebViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
newWebViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])

webViewController.didMove(toParent: self)
newWebViewController.didMove(toParent: self)
}


private func removeWebViewController() {
webViewController?.removeFromParent()
webViewController?.view.removeFromSuperview()
webViewController = nil
}

private func subscribeToCleanupPublisher() {
cleanupCancellable = chatModel.cleanupPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] in
self?.webViewController?.reload()
}
}

@objc private func closeButtonTapped() {
chatModel.startCleanupTimer()
dismiss(animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
chatModel.cancelTimer()
removeWebViewController()
}
}
4 changes: 4 additions & 0 deletions DuckDuckGo/AIChat/AIChatWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ final class AIChatWebViewController: UIViewController {
])
}

func reload() {
loadWebsite()
}

private func loadWebsite() {
if let url = URL(string: "https://duck.ai") {
let request = URLRequest(url: url)
Expand Down
11 changes: 7 additions & 4 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ class MainViewController: UIViewController {

var appDidFinishLaunchingStartTime: CFAbsoluteTime?

private lazy var aiChatNavigationController: UINavigationController = {
let chatModel = AIChatModel(webViewConfiguration: WKWebViewConfiguration.persistent())
return UINavigationController(rootViewController: AIChatViewController(chatModel: chatModel))
}()

init(
bookmarksDatabase: CoreDataDatabase,
bookmarksDatabaseCleaner: BookmarkDatabaseCleaner,
Expand Down Expand Up @@ -2342,10 +2347,8 @@ extension MainViewController: TabDelegate {
}

func tabDidRequestAIChat(tab: TabViewController) {
let navigationController = UINavigationController(rootViewController: AIChatViewController())

navigationController.modalPresentationStyle = .fullScreen
tab.present(navigationController, animated: true, completion: nil)
aiChatNavigationController.modalPresentationStyle = .fullScreen
tab.present(aiChatNavigationController, animated: true, completion: nil)
}

func tabDidRequestBookmarks(tab: TabViewController) {
Expand Down

0 comments on commit 4e07beb

Please sign in to comment.