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