mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Move modules to Modules folder.
This commit is contained in:
5
Modules/SyncDatabase/.gitignore
vendored
Normal file
5
Modules/SyncDatabase/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.DS_Store
|
||||
/.build
|
||||
/Packages
|
||||
/*.xcodeproj
|
||||
xcuserdata/
|
||||
30
Modules/SyncDatabase/Package.swift
Normal file
30
Modules/SyncDatabase/Package.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
// swift-tools-version:5.10
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "SyncDatabase",
|
||||
platforms: [.macOS(.v14), .iOS(.v17)],
|
||||
products: [
|
||||
.library(
|
||||
name: "SyncDatabase",
|
||||
type: .dynamic,
|
||||
targets: ["SyncDatabase"]),
|
||||
],
|
||||
dependencies: [
|
||||
.package(path: "../RSCore"),
|
||||
.package(path: "../Articles"),
|
||||
.package(path: "../RSDatabase"),
|
||||
],
|
||||
targets: [
|
||||
.target(
|
||||
name: "SyncDatabase",
|
||||
dependencies: [
|
||||
"RSCore",
|
||||
"RSDatabase",
|
||||
"Articles",
|
||||
],
|
||||
swiftSettings: [.unsafeFlags(["-warnings-as-errors"])]
|
||||
),
|
||||
]
|
||||
)
|
||||
3
Modules/SyncDatabase/README.md
Normal file
3
Modules/SyncDatabase/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# SyncDatabase
|
||||
|
||||
A description of this package.
|
||||
25
Modules/SyncDatabase/Sources/SyncDatabase/Constants.swift
Normal file
25
Modules/SyncDatabase/Sources/SyncDatabase/Constants.swift
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Constants.swift
|
||||
// SyncDatabase
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct DatabaseTableName {
|
||||
|
||||
static let syncStatus = "syncStatus"
|
||||
|
||||
}
|
||||
|
||||
struct DatabaseKey {
|
||||
|
||||
// Sync Status
|
||||
static let articleID = "articleID"
|
||||
static let key = "key"
|
||||
static let flag = "flag"
|
||||
static let selected = "selected"
|
||||
|
||||
}
|
||||
88
Modules/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift
Normal file
88
Modules/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift
Normal file
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// SyncDatabase.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RSCore
|
||||
import RSDatabase
|
||||
|
||||
public typealias SyncStatusesResult = Result<Array<SyncStatus>, DatabaseError>
|
||||
public typealias SyncStatusesCompletionBlock = (SyncStatusesResult) -> Void
|
||||
|
||||
public typealias SyncStatusArticleIDsResult = Result<Set<String>, DatabaseError>
|
||||
public typealias SyncStatusArticleIDsCompletionBlock = (SyncStatusArticleIDsResult) -> Void
|
||||
|
||||
public struct SyncDatabase {
|
||||
|
||||
private let syncStatusTable: SyncStatusTable
|
||||
private let queue: DatabaseQueue
|
||||
|
||||
public init(databaseFilePath: String) {
|
||||
let queue = DatabaseQueue(databasePath: databaseFilePath)
|
||||
try! queue.runCreateStatements(SyncDatabase.tableCreationStatements)
|
||||
queue.vacuumIfNeeded(daysBetweenVacuums: 11)
|
||||
self.queue = queue
|
||||
|
||||
self.syncStatusTable = SyncStatusTable(queue: queue)
|
||||
}
|
||||
|
||||
// MARK: - API
|
||||
|
||||
public func insertStatuses(_ statuses: [SyncStatus], completion: @escaping DatabaseCompletionBlock) {
|
||||
syncStatusTable.insertStatuses(statuses, completion: completion)
|
||||
}
|
||||
|
||||
public func selectForProcessing(limit: Int? = nil, completion: @escaping SyncStatusesCompletionBlock) {
|
||||
return syncStatusTable.selectForProcessing(limit: limit, completion: completion)
|
||||
}
|
||||
|
||||
public func selectPendingCount(completion: @escaping DatabaseIntCompletionBlock) {
|
||||
syncStatusTable.selectPendingCount(completion)
|
||||
}
|
||||
|
||||
public func selectPendingReadStatusArticleIDs(completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
||||
syncStatusTable.selectPendingReadStatusArticleIDs(completion: completion)
|
||||
}
|
||||
|
||||
public func selectPendingStarredStatusArticleIDs(completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
||||
syncStatusTable.selectPendingStarredStatusArticleIDs(completion: completion)
|
||||
}
|
||||
|
||||
public func resetAllSelectedForProcessing(completion: DatabaseCompletionBlock? = nil) {
|
||||
syncStatusTable.resetAllSelectedForProcessing(completion: completion)
|
||||
}
|
||||
|
||||
public func resetSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) {
|
||||
syncStatusTable.resetSelectedForProcessing(articleIDs, completion: completion)
|
||||
}
|
||||
|
||||
public func deleteSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) {
|
||||
syncStatusTable.deleteSelectedForProcessing(articleIDs, completion: completion)
|
||||
}
|
||||
|
||||
// MARK: - Suspend and Resume (for iOS)
|
||||
|
||||
/// Close the database and stop running database calls.
|
||||
/// Any pending calls will complete first.
|
||||
public func suspend() {
|
||||
queue.suspend()
|
||||
}
|
||||
|
||||
/// Open the database and allow for running database calls again.
|
||||
public func resume() {
|
||||
queue.resume()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private extension SyncDatabase {
|
||||
|
||||
static let tableCreationStatements = """
|
||||
CREATE TABLE if not EXISTS syncStatus (articleID TEXT NOT NULL, key TEXT NOT NULL, flag BOOL NOT NULL DEFAULT 0, selected BOOL NOT NULL DEFAULT 0, PRIMARY KEY (articleID, key));
|
||||
"""
|
||||
}
|
||||
48
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatus.swift
Normal file
48
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatus.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// SyncStatus.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Articles
|
||||
import RSDatabase
|
||||
|
||||
public struct SyncStatus: Hashable, Equatable {
|
||||
|
||||
public enum Key: String {
|
||||
case read = "read"
|
||||
case starred = "starred"
|
||||
case deleted = "deleted"
|
||||
case new = "new"
|
||||
|
||||
public init(_ articleStatusKey: ArticleStatus.Key) {
|
||||
switch articleStatusKey {
|
||||
case .read:
|
||||
self = Self.read
|
||||
case .starred:
|
||||
self = Self.starred
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public let articleID: String
|
||||
public let key: SyncStatus.Key
|
||||
public let flag: Bool
|
||||
public let selected: Bool
|
||||
|
||||
public init(articleID: String, key: SyncStatus.Key, flag: Bool, selected: Bool = false) {
|
||||
self.articleID = articleID
|
||||
self.key = key
|
||||
self.flag = flag
|
||||
self.selected = selected
|
||||
}
|
||||
|
||||
public func databaseDictionary() -> DatabaseDictionary {
|
||||
return [DatabaseKey.articleID: articleID, DatabaseKey.key: key.rawValue, DatabaseKey.flag: flag, DatabaseKey.selected: selected]
|
||||
}
|
||||
|
||||
}
|
||||
241
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift
Normal file
241
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift
Normal file
@@ -0,0 +1,241 @@
|
||||
//
|
||||
// SyncStatusTable.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RSCore
|
||||
import Articles
|
||||
import RSDatabase
|
||||
import RSDatabaseObjC
|
||||
|
||||
struct SyncStatusTable: DatabaseTable {
|
||||
|
||||
let name = DatabaseTableName.syncStatus
|
||||
private let queue: DatabaseQueue
|
||||
|
||||
init(queue: DatabaseQueue) {
|
||||
self.queue = queue
|
||||
}
|
||||
|
||||
func selectForProcessing(limit: Int?, completion: @escaping SyncStatusesCompletionBlock) {
|
||||
queue.runInTransaction { databaseResult in
|
||||
var statuses = Set<SyncStatus>()
|
||||
var error: DatabaseError?
|
||||
|
||||
func makeDatabaseCall(_ database: FMDatabase) {
|
||||
let updateSQL = "update syncStatus set selected = true"
|
||||
database.executeUpdate(updateSQL, withArgumentsIn: nil)
|
||||
|
||||
var selectSQL = "select * from syncStatus where selected == true"
|
||||
if let limit = limit {
|
||||
selectSQL = "\(selectSQL) limit \(limit)"
|
||||
}
|
||||
if let resultSet = database.executeQuery(selectSQL, withArgumentsIn: nil) {
|
||||
statuses = resultSet.mapToSet(self.statusWithRow)
|
||||
}
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCall(database)
|
||||
case .failure(let databaseError):
|
||||
error = databaseError
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
if let error = error {
|
||||
completion(.failure(error))
|
||||
}
|
||||
else {
|
||||
completion(.success(Array(statuses)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func selectPendingCount(_ completion: @escaping DatabaseIntCompletionBlock) {
|
||||
queue.runInDatabase { databaseResult in
|
||||
var count: Int = 0
|
||||
var error: DatabaseError?
|
||||
|
||||
func makeDatabaseCall(_ database: FMDatabase) {
|
||||
let sql = "select count(*) from syncStatus"
|
||||
if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) {
|
||||
count = self.numberWithCountResultSet(resultSet)
|
||||
}
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCall(database)
|
||||
case .failure(let databaseError):
|
||||
error = databaseError
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
if let error = error {
|
||||
completion(.failure(error))
|
||||
}
|
||||
else {
|
||||
completion(.success(count))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func selectPendingReadStatusArticleIDs(completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
||||
selectPendingArticleIDsAsync(.read, completion)
|
||||
}
|
||||
|
||||
func selectPendingStarredStatusArticleIDs(completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
||||
selectPendingArticleIDsAsync(.starred, completion)
|
||||
}
|
||||
|
||||
func resetAllSelectedForProcessing(completion: DatabaseCompletionBlock? = nil) {
|
||||
queue.runInTransaction { databaseResult in
|
||||
|
||||
func makeDatabaseCall(_ database: FMDatabase) {
|
||||
let updateSQL = "update syncStatus set selected = false"
|
||||
database.executeUpdate(updateSQL, withArgumentsIn: nil)
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCall(database)
|
||||
callCompletion(completion, nil)
|
||||
case .failure(let databaseError):
|
||||
callCompletion(completion, databaseError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resetSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) {
|
||||
guard !articleIDs.isEmpty else {
|
||||
callCompletion(completion, nil)
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInTransaction { databaseResult in
|
||||
|
||||
func makeDatabaseCall(_ database: FMDatabase) {
|
||||
let parameters = articleIDs.map { $0 as AnyObject }
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))!
|
||||
let updateSQL = "update syncStatus set selected = false where articleID in \(placeholders)"
|
||||
database.executeUpdate(updateSQL, withArgumentsIn: parameters)
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCall(database)
|
||||
callCompletion(completion, nil)
|
||||
case .failure(let databaseError):
|
||||
callCompletion(completion, databaseError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func deleteSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) {
|
||||
guard !articleIDs.isEmpty else {
|
||||
callCompletion(completion, nil)
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInTransaction { databaseResult in
|
||||
|
||||
func makeDatabaseCall(_ database: FMDatabase) {
|
||||
let parameters = articleIDs.map { $0 as AnyObject }
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))!
|
||||
let deleteSQL = "delete from syncStatus where selected = true and articleID in \(placeholders)"
|
||||
database.executeUpdate(deleteSQL, withArgumentsIn: parameters)
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCall(database)
|
||||
callCompletion(completion, nil)
|
||||
case .failure(let databaseError):
|
||||
callCompletion(completion, databaseError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func insertStatuses(_ statuses: [SyncStatus], completion: @escaping DatabaseCompletionBlock) {
|
||||
queue.runInTransaction { databaseResult in
|
||||
|
||||
func makeDatabaseCall(_ database: FMDatabase) {
|
||||
let statusArray = statuses.map { $0.databaseDictionary() }
|
||||
self.insertRows(statusArray, insertType: .orReplace, in: database)
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCall(database)
|
||||
callCompletion(completion, nil)
|
||||
case .failure(let databaseError):
|
||||
callCompletion(completion, databaseError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private extension SyncStatusTable {
|
||||
|
||||
func statusWithRow(_ row: FMResultSet) -> SyncStatus? {
|
||||
guard let articleID = row.string(forColumn: DatabaseKey.articleID),
|
||||
let rawKey = row.string(forColumn: DatabaseKey.key),
|
||||
let key = SyncStatus.Key(rawValue: rawKey) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let flag = row.bool(forColumn: DatabaseKey.flag)
|
||||
let selected = row.bool(forColumn: DatabaseKey.selected)
|
||||
|
||||
return SyncStatus(articleID: articleID, key: key, flag: flag, selected: selected)
|
||||
}
|
||||
|
||||
func selectPendingArticleIDsAsync(_ statusKey: ArticleStatus.Key, _ completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
||||
|
||||
queue.runInDatabase { databaseResult in
|
||||
|
||||
func makeDatabaseCall(_ database: FMDatabase) {
|
||||
let sql = "select articleID from syncStatus where selected == false and key = \"\(statusKey.rawValue)\";"
|
||||
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(Set<String>()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let articleIDs = resultSet.mapToSet{ $0.string(forColumnIndex: 0) }
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(articleIDs))
|
||||
}
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCall(database)
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func callCompletion(_ completion: DatabaseCompletionBlock?, _ databaseError: DatabaseError?) {
|
||||
guard let completion = completion else {
|
||||
return
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
completion(databaseError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user