Skip to content

Commit

Permalink
Added recent document container
Browse files Browse the repository at this point in the history
  • Loading branch information
vincode-io committed Feb 3, 2021
1 parent 3124eec commit 9113450
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Templeton/Sources/Templeton/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public final class Account: NSObject, Identifiable, Codable {
public var documentContainers: [DocumentContainer] {
var containers = [DocumentContainer]()
containers.append(AllDocuments(account: self))

containers.append(RecentDocuments(account: self)
)
for tagDocuments in tags?
.sorted(by: { $0.name < $1.name })
.compactMap({ TagDocuments(account: self, tag: $0) }) ?? [TagDocuments]() {
Expand Down Expand Up @@ -204,6 +205,8 @@ public final class Account: NSObject, Identifiable, Codable {
switch entityID {
case .allDocuments:
return AllDocuments(account: self)
case .recentDocuments:
return RecentDocuments(account: self)
case .tagDocuments(_, let tagID):
guard let tag = findTag(tagID: tagID) else { return nil }
return TagDocuments(account: self, tag: tag)
Expand Down
2 changes: 1 addition & 1 deletion Templeton/Sources/Templeton/AccountManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public final class AccountManager {
switch entityID {
case .search(let searchText):
return Search(searchText: searchText)
case .allDocuments(let accountID), .tagDocuments(let accountID, _):
case .allDocuments(let accountID), .recentDocuments(let accountID), .tagDocuments(let accountID, _):
return accountsDictionary[accountID]?.findDocumentContainer(entityID)
default:
fatalError()
Expand Down
29 changes: 27 additions & 2 deletions Templeton/Sources/Templeton/EntityID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
case account(Int)
case document(Int, String) // Account, Document
case allDocuments(Int) // Account
case recentDocuments(Int) // Account
case tagDocuments(Int, String) // Tag
case search(String) // Search String

Expand All @@ -22,6 +23,8 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
return accountID
case .allDocuments(let accountID):
return accountID
case .recentDocuments(let accountID):
return accountID
case .tagDocuments(let accountID, _):
return accountID
default:
Expand All @@ -39,6 +42,8 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
return "search:\(searchText)"
case .allDocuments(let id):
return "allDocuments:\(id)"
case .recentDocuments(let id):
return "recentDocuments:\(id)"
case .tagDocuments(let accountID, let tagID):
return "tagDocuments:\(accountID)_\(tagID)"
}
Expand Down Expand Up @@ -98,13 +103,19 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
self = .search(String(searchText))
return
} else if description.starts(with: "allDocuments:") {
let idString = description.suffix(from: description.index(description.startIndex, offsetBy: 18))
let idString = description.suffix(from: description.index(description.startIndex, offsetBy: 13))
if let accountID = Int(idString) {
self = .allDocuments(accountID)
return
}
} else if description.starts(with: "recentDocuments:") {
let idString = description.suffix(from: description.index(description.startIndex, offsetBy: 16))
if let accountID = Int(idString) {
self = .recentDocuments(accountID)
return
}
} else if description.starts(with: "tagDocuments:") {
let idString = description.suffix(from: description.index(description.startIndex, offsetBy: 18))
let idString = description.suffix(from: description.index(description.startIndex, offsetBy: 13))
let ids = idString.split(separator: "_")
if let accountID = Int(ids[0]) {
self = .tagDocuments(accountID, String(ids[1]))
Expand Down Expand Up @@ -132,6 +143,9 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
case "allDocuments":
let accountID = try container.decode(Int.self, forKey: .accountID)
self = .allDocuments(accountID)
case "recentDocuments":
let accountID = try container.decode(Int.self, forKey: .accountID)
self = .recentDocuments(accountID)
case "tagDocuments":
let accountID = try container.decode(Int.self, forKey: .accountID)
let tagID = try container.decode(String.self, forKey: .tagID)
Expand All @@ -158,6 +172,9 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
case "allDocuments":
guard let accountID = userInfo["accountID"] as? Int else { return nil }
self = .allDocuments(accountID)
case "recentDocuments":
guard let accountID = userInfo["accountID"] as? Int else { return nil }
self = .recentDocuments(accountID)
case "tagDocuments":
guard let accountID = userInfo["accountID"] as? Int else { return nil }
guard let tagID = userInfo["tagID"] as? String else { return nil }
Expand All @@ -184,6 +201,9 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
case .allDocuments(let accountID):
try container.encode("allDocuments", forKey: .type)
try container.encode(accountID, forKey: .accountID)
case .recentDocuments(let accountID):
try container.encode("recentDocuments", forKey: .type)
try container.encode(accountID, forKey: .accountID)
case .tagDocuments(let accountID, let tagID):
try container.encode("tagDocuments", forKey: .type)
try container.encode(accountID, forKey: .accountID)
Expand Down Expand Up @@ -214,6 +234,11 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
"type": "allDocuments",
"accountID": accountID
]
case .recentDocuments(let accountID):
return [
"type": "recentDocuments",
"accountID": accountID
]
case .tagDocuments(let accountID, let tagID):
return [
"type": "tagDocuments",
Expand Down
2 changes: 2 additions & 0 deletions Templeton/Sources/Templeton/L10n.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ internal enum L10n {
internal static let outdent = L10n.tr("Localizable", "Outdent")
/// Paste
internal static let paste = L10n.tr("Localizable", "Paste")
/// Recent
internal static let recent = L10n.tr("Localizable", "Recent")
/// Search
internal static let search = L10n.tr("Localizable", "Search")
/// Split Row
Expand Down
1 change: 1 addition & 0 deletions Templeton/Sources/Templeton/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
"Typing" = "Typing";
"Complete" = "Complete";
"Uncomplete" = "Uncomplete";
"Recent" = "Recent";
4 changes: 2 additions & 2 deletions Templeton/Sources/Templeton/Outline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public final class Outline: RowContainer, OPMLImporter, Identifiable, Equatable,
tagIDs = [String]()
}
tagIDs!.append(tag.id)
documentMetaDataDidChange()
self.updated = Date()

let inserted = tagIDs!.count - 1
let changes = OutlineElementChanges(section: .tags, inserts: Set([inserted]))
Expand All @@ -212,7 +212,7 @@ public final class Outline: RowContainer, OPMLImporter, Identifiable, Equatable,
public func deleteTag(_ tag: Tag) {
guard let index = tagIDs?.firstIndex(where: { $0 == tag.id }) else { return }
tagIDs?.remove(at: index)
documentMetaDataDidChange()
self.updated = Date()

let changes = OutlineElementChanges(section: .tags, deletes: Set([index]))
outlineElementsDidChange(changes)
Expand Down
30 changes: 30 additions & 0 deletions Templeton/Sources/Templeton/RecentDocuments.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// RecentDocuments.swift
//
//
// Created by Maurice Parker on 2/2/21.
//

import UIKit
import RSCore

public final class RecentDocuments: Identifiable, DocumentContainer {

public var id: EntityID
public var name: String? = L10n.recent
public var image: RSImage? = UIImage(systemName: "clock")!

public weak var account: Account?

public init(account: Account) {
self.id = .recentDocuments(account.id.accountID)
self.account = account
}

public func sortedDocuments(completion: @escaping (Result<[Document], Error>) -> Void) {
let sortedDocuments = Self.sortByUpdate(account?.documents ?? [Document]())
let suffix = Array(sortedDocuments.suffix(10))
completion(.success(suffix))
}

}

0 comments on commit 9113450

Please sign in to comment.