Skip to content

Commit

Permalink
save webView's interaction state
Browse files Browse the repository at this point in the history
  • Loading branch information
svojsu committed Nov 11, 2024
1 parent f27c0ab commit ead20fc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
12 changes: 12 additions & 0 deletions novawallet/Common/Extension/SettingsExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum SettingsKey: String {
case integrateNetworksBannerSeen
case assetListGroupStyle
case assetIconsAppearance
case webViewStates
}

extension SettingsManagerProtocol {
Expand Down Expand Up @@ -294,4 +295,15 @@ extension SettingsManagerProtocol {
)
}
}

var webViewStates: [String: Data]? {
get {
let anyValue = anyValue(for: SettingsKey.webViewStates.rawValue)
return anyValue as? [String: Data]
}

set {
set(anyValue: newValue, for: SettingsKey.webViewStates.rawValue)
}
}
}
20 changes: 19 additions & 1 deletion novawallet/Modules/DApp/DAppBrowser/DAppBrowserPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ final class DAppBrowserPresenter {
private(set) var favorites: [String: DAppFavorite]?
private(set) var browserPage: DAppBrowserPage?

private var currentTabId: UUID?

init(
interactor: DAppBrowserInteractorInputProtocol,
wireframe: DAppBrowserWireframeProtocol,
Expand Down Expand Up @@ -112,6 +114,18 @@ extension DAppBrowserPresenter: DAppBrowserPresenterProtocol {
return
}

if
let currentTabId,
let currentWebView = webViewPool.getWebView(for: currentTabId),
let url = currentWebView.url {
if #available(iOS 15.0, *) {
let currentWebViewState = currentWebView.interactionState
tabsManager.updateStateForTab(with: url, currentWebViewState)
} else {
// Fallback on earlier versions
}
}

let webView = webViewPool.setupWebView(for: tab.uuid)

let viewModel = DAppBrowserTabViewModel(
Expand All @@ -120,6 +134,8 @@ extension DAppBrowserPresenter: DAppBrowserPresenterProtocol {
webView: webView
)

currentTabId = tab.uuid

view?.didReceiveTab(viewModel: viewModel)
}
}
Expand Down Expand Up @@ -161,10 +177,12 @@ extension DAppBrowserPresenter: DAppBrowserInteractorOutputProtocol {

let viewModel = DAppBrowserTabViewModel(
tab: tab,
loadRequired: true,
loadRequired: tab.state == nil,
webView: webView
)

currentTabId = tab.uuid

view?.didReceiveTab(viewModel: viewModel)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ extension DAppBrowserViewController: DAppBrowserViewProtocol {
transports = viewModel.tab.transports

rootView.setWebView(viewModel.webView)
if #available(iOS 15.0, *) {
rootView.webView.interactionState = viewModel.tab.state
} else {
// Fallback on earlier versions
}

configure()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct DAppBrowserTabModel {
let url: URL
let isDesktop: Bool
let transports: [DAppTransportModel]
let state: Data?
}

struct DAppBrowserTabViewModel {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import Foundation
import WebKit
import SoraKeystore

protocol DAppBrowserTabsManagerProtocol {
func createTab(for dappBrowserModel: DAppBrowserModel) -> DAppBrowserTabModel
func fetchTab(for id: UUID) -> DAppBrowserTabModel?
func fetchAllTabs() -> [DAppBrowserTabModel]
func updateStateForTab(with url: URL, _ state: Any?)
}

class DAppBrowserTabsManager {
private var tabs: [UUID: DAppBrowserTabModel] = [:]

private let settingsManager: SettingsManagerProtocol = SettingsManager.shared

static let shared = DAppBrowserTabsManager()
}

extension DAppBrowserTabsManager: DAppBrowserTabsManagerProtocol {
func createTab(for dappBrowserModel: DAppBrowserModel) -> DAppBrowserTabModel {
let state = settingsManager.webViewStates?[dappBrowserModel.url.absoluteString]

let uuid = UUID()

let tab = DAppBrowserTabModel(
uuid: uuid,
url: dappBrowserModel.url,
isDesktop: dappBrowserModel.isDesktop,
transports: dappBrowserModel.transports
transports: dappBrowserModel.transports,
state: state
)

tabs[uuid] = tab
Expand All @@ -35,4 +43,31 @@ extension DAppBrowserTabsManager: DAppBrowserTabsManagerProtocol {
func fetchAllTabs() -> [DAppBrowserTabModel] {
Array(tabs.values)
}

func updateStateForTab(with url: URL, _ state: Any?) {
guard let nsData = state as? NSData else {
return
}

let key = url.absoluteString

var dict: [String: Data] = settingsManager.webViewStates ?? [:]

let data = Data(referencing: nsData)
dict[key] = data

settingsManager.webViewStates = dict

if let tab = tabs.values.first(where: { $0.url == url }) {
let updatedTab = DAppBrowserTabModel(
uuid: tab.uuid,
url: tab.url,
isDesktop: tab.isDesktop,
transports: tab.transports,
state: data
)

tabs[tab.uuid] = updatedTab
}
}
}

0 comments on commit ead20fc

Please sign in to comment.