OSDN Git Service

CoreDataManagerプロトコルをCoreDataAccessorのサブタイプにした
[kcd/KCD.git] / KCD / LocalDataStore.swift
1 //
2 //  LocalDataStore.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/06.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 extension CoreDataConfiguration {
12     
13     static let local = CoreDataConfiguration("LocalData")
14 }
15
16 final class LocalDataStore: CoreDataManager {
17     
18     static let core = CoreDataCore(.local)
19     
20     static let `default` = LocalDataStore(type: .reader)
21     
22     class func oneTimeEditor() -> LocalDataStore {
23         
24         return LocalDataStore(type: .editor)
25     }
26     
27     required init(type: CoreDataManagerType) {
28         
29         context = LocalDataStore.context(for: type)
30     }
31     
32     deinit {
33         
34         save()
35     }
36     
37     let context: NSManagedObjectContext
38 }
39
40 extension LocalDataStore {
41     
42     func unmarkedDropShipHistories(befor days: Int) -> [DropShipHistory] {
43         
44         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
45         let predicate = NSPredicate
46             .and([
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"])
51                 ])
52         
53         guard let dropHistories = try? objects(of: DropShipHistory.entity, predicate: predicate) else { return [] }
54         
55         return dropHistories
56     }
57     
58     func createDropShipHistory() -> DropShipHistory? {
59         
60         return insertNewObject(for: DropShipHistory.entity)
61     }
62     
63     func kaihatuHistories() -> [KaihatuHistory] {
64         
65         guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity) else { return [] }
66         
67         return kaihatuHistories
68     }
69     
70     func unmarkedKaihatuHistories(befor days: Int) -> [KaihatuHistory] {
71         
72         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
73         let predicate = NSPredicate
74             .and([
75                 NSPredicate(#keyPath(KaihatuHistory.date), lessThan: date),
76                 NSPredicate(#keyPath(KaihatuHistory.mark), equal: 0)
77                     .or(.isNil(#keyPath(KaihatuHistory.mark)))
78                 ])
79         
80         guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity, predicate: predicate) else { return [] }
81         
82         return kaihatuHistories
83     }
84     
85     func createKaihatuHistory() -> KaihatuHistory? {
86         
87         return insertNewObject(for: KaihatuHistory.entity)
88     }
89     
90     func kenzoMark(byDockId dockId: Int) -> KenzoMark? {
91         
92         let predicate = NSPredicate(#keyPath(KenzoMark.kDockId), equal: dockId)
93         
94         guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
95         
96         return kenzoMarks.first
97     }
98     
99     // swiftlint:disable function_parameter_count
100     func kenzoMark(fuel: Int, bull: Int, steel: Int, bauxite: Int, kaihatusizai: Int, kDockId: Int, shipId: Int) -> KenzoMark? {
101         
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))
110         
111         guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
112         
113         return kenzoMarks.first
114     }
115     
116     func createKenzoMark() -> KenzoMark? {
117         
118         return insertNewObject(for: KenzoMark.entity)
119     }
120     
121     func unmarkedKenzoHistories(befor days: Int) -> [KenzoHistory] {
122         
123         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
124         let predicate = NSPredicate
125             .and([
126                 NSPredicate(#keyPath(KenzoHistory.date), lessThan: date),
127                 NSPredicate(#keyPath(KenzoHistory.mark), equal: 0)
128                     .or(.isNil(#keyPath(KenzoHistory.mark)))
129                 ])
130         
131         guard let kenzoHistories = try? objects(of: KenzoHistory.entity, predicate: predicate) else { return [] }
132         
133         return kenzoHistories
134     }
135     
136     func createKenzoHistory() -> KenzoHistory? {
137         
138         return insertNewObject(for: KenzoHistory.entity)
139     }
140     
141     func hiddenDropShipHistories() -> [HiddenDropShipHistory] {
142         
143         guard let dropShipHistories = try? objects(of: HiddenDropShipHistory.entity) else { return [] }
144         
145         return dropShipHistories
146     }
147     
148     func createHiddenDropShipHistory() -> HiddenDropShipHistory? {
149         
150         return insertNewObject(for: HiddenDropShipHistory.entity)
151     }
152 }