2 // LocalDataStore.swift
5 // Created by Hori,Masaki on 2017/01/06.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class LocalDataStore: CoreDataManager {
13 static let core = CoreDataCore(CoreDataConfiguration("LocalData"))
15 static let `default` = LocalDataStore(type: .reader)
17 class func oneTimeEditor() -> LocalDataStore {
19 return LocalDataStore(type: .editor)
22 required init(type: CoreDataManagerType) {
24 context = LocalDataStore.context(for: type)
32 let context: NSManagedObjectContext
35 extension LocalDataStore {
37 func unmarkedDropShipHistories(befor days: Int) -> [DropShipHistory] {
39 let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
40 let predicate = NSPredicate
42 NSPredicate(#keyPath(DropShipHistory.date), lessThan: date),
43 NSPredicate(#keyPath(DropShipHistory.mark), equal: 0)
44 .or(.isNil(#keyPath(DropShipHistory.mark))),
45 NSPredicate(#keyPath(DropShipHistory.mapArea), valuesIn: ["1", "2", "3", "4", "5", "6", "7", "8", "9"])
48 guard let dropHistories = try? objects(of: DropShipHistory.entity, predicate: predicate) else { return [] }
53 func createDropShipHistory() -> DropShipHistory? {
55 return insertNewObject(for: DropShipHistory.entity)
58 func kaihatuHistories() -> [KaihatuHistory] {
60 guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity) else { return [] }
62 return kaihatuHistories
65 func unmarkedKaihatuHistories(befor days: Int) -> [KaihatuHistory] {
67 let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
68 let predicate = NSPredicate
70 NSPredicate(#keyPath(KaihatuHistory.date), lessThan: date),
71 NSPredicate(#keyPath(KaihatuHistory.mark), equal: 0)
72 .or(.isNil(#keyPath(KaihatuHistory.mark)))
75 guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity, predicate: predicate) else { return [] }
77 return kaihatuHistories
80 func createKaihatuHistory() -> KaihatuHistory? {
82 return insertNewObject(for: KaihatuHistory.entity)
85 func kenzoMark(byDockId dockId: Int) -> KenzoMark? {
87 let predicate = NSPredicate(#keyPath(KenzoMark.kDockId), equal: dockId)
89 guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
91 return kenzoMarks.first
94 // swiftlint:disable function_parameter_count
95 func kenzoMark(fuel: Int, bull: Int, steel: Int, bauxite: Int, kaihatusizai: Int, kDockId: Int, shipId: Int) -> KenzoMark? {
97 let predicate = NSPredicate.empty
98 .and(NSPredicate(#keyPath(KenzoMark.fuel), equal: fuel))
99 .and(NSPredicate(#keyPath(KenzoMark.bull), equal: bull))
100 .and(NSPredicate(#keyPath(KenzoMark.steel), equal: steel))
101 .and(NSPredicate(#keyPath(KenzoMark.bauxite), equal: bauxite))
102 .and(NSPredicate(#keyPath(KenzoMark.kaihatusizai), equal: kaihatusizai))
103 .and(NSPredicate(#keyPath(KenzoMark.kDockId), equal: kDockId))
104 .and(NSPredicate(#keyPath(KenzoMark.created_ship_id), equal: shipId))
106 guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
108 return kenzoMarks.first
111 func createKenzoMark() -> KenzoMark? {
113 return insertNewObject(for: KenzoMark.entity)
116 func unmarkedKenzoHistories(befor days: Int) -> [KenzoHistory] {
118 let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
119 let predicate = NSPredicate
121 NSPredicate(#keyPath(KenzoHistory.date), lessThan: date),
122 NSPredicate(#keyPath(KenzoHistory.mark), equal: 0)
123 .or(.isNil(#keyPath(KenzoHistory.mark)))
126 guard let kenzoHistories = try? objects(of: KenzoHistory.entity, predicate: predicate) else { return [] }
128 return kenzoHistories
131 func createKenzoHistory() -> KenzoHistory? {
133 return insertNewObject(for: KenzoHistory.entity)
136 func hiddenDropShipHistories() -> [HiddenDropShipHistory] {
138 guard let dropShipHistories = try? objects(of: HiddenDropShipHistory.entity) else { return [] }
140 return dropShipHistories
143 func createHiddenDropShipHistory() -> HiddenDropShipHistory? {
145 return insertNewObject(for: HiddenDropShipHistory.entity)