From f8eb356124cba8f5ef2faa53f113ba9248699429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=C2=A0Buczek?= Date: Mon, 7 Dec 2020 18:56:56 +0100 Subject: [PATCH] Fix #3113: Fetch adblock data every 6 hours. --- Client/Application/Delegates/AppDelegate.swift | 2 ++ .../AdblockDebugMenuTableViewController.swift | 14 ++++++++++++++ Client/WebFilters/AdblockResourceDownloader.swift | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Client/Application/Delegates/AppDelegate.swift b/Client/Application/Delegates/AppDelegate.swift index d5dbd5a4482..e241c24f1c8 100644 --- a/Client/Application/Delegates/AppDelegate.swift +++ b/Client/Application/Delegates/AppDelegate.swift @@ -478,6 +478,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati if let authInfo = KeychainWrapper.sharedAppContainerKeychain.authenticationInfo(), authInfo.isPasscodeRequiredImmediately { authenticator?.willEnterForeground() } + + AdblockResourceDownloader.shared.startLoading() } func applicationWillResignActive(_ application: UIApplication) { diff --git a/Client/Frontend/Settings/AdblockDebugMenuTableViewController.swift b/Client/Frontend/Settings/AdblockDebugMenuTableViewController.swift index d0a80c8fb72..98362cea80d 100644 --- a/Client/Frontend/Settings/AdblockDebugMenuTableViewController.swift +++ b/Client/Frontend/Settings/AdblockDebugMenuTableViewController.swift @@ -22,6 +22,7 @@ class AdblockDebugMenuTableViewController: TableViewController { if listNames.isEmpty { return } self.dataSource.sections = [self.actionsSection, + self.fetchSection, self.datesSection, self.bundledListsSection(names: listNames), self.downloadedListsSection(names: listNames)] @@ -44,6 +45,19 @@ class AdblockDebugMenuTableViewController: TableViewController { return section } + private var fetchSection: Section { + var section = Section(footer: "Last time we pinged the server for new data. If adblock list hasn't changed `Last Time Updated` section does not update.") + let dateFormatter = DateFormatter().then { + $0.dateStyle = .short + $0.timeStyle = .short + } + + section.rows = [.init(text: "Last fetch time", detailText: + dateFormatter.string(from: AdblockResourceDownloader.shared.lastFetchDate))] + + return section + } + private var datesSection: Section { var section = Section(header: "Last time updated", footer: "When the lists were last time updated on the device") diff --git a/Client/WebFilters/AdblockResourceDownloader.swift b/Client/WebFilters/AdblockResourceDownloader.swift index 954d8becde9..5d75c68dda4 100644 --- a/Client/WebFilters/AdblockResourceDownloader.swift +++ b/Client/WebFilters/AdblockResourceDownloader.swift @@ -34,9 +34,19 @@ class AdblockResourceDownloader { Preferences.Shields.useRegionAdBlock.observe(from: self) } + /// Initialized with year 1970 to force adblock fetch at first launch. + private(set) var lastFetchDate = Date(timeIntervalSince1970: 0) + func startLoading() { - AdblockResourceDownloader.shared.regionalAdblockResourcesSetup() - AdblockResourceDownloader.shared.generalAdblockResourcesSetup() + let now = Date() + let fetchInterval = AppConstants.buildChannel.isPublic ? 6.hours : 10.minutes + + if now.timeIntervalSince(lastFetchDate) >= fetchInterval { + lastFetchDate = now + + AdblockResourceDownloader.shared.regionalAdblockResourcesSetup() + AdblockResourceDownloader.shared.generalAdblockResourcesSetup() + } } func regionalAdblockResourcesSetup() {