OSDN Git Service

CoreDataCoreのプロパティ名を簡略化
[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 CoreDataIntormation {
12     static let local = CoreDataIntormation("LocalData")
13 }
14 extension CoreDataCore {
15     static let local = CoreDataCore(.local)
16 }
17
18
19 class LocalDataStore: CoreDataAccessor, CoreDataManager {
20     static var `default` = LocalDataStore(type: .reader)
21     class func oneTimeEditor() -> LocalDataStore {
22         return LocalDataStore(type: .editor)
23     }
24     
25     required init(type: CoreDataManagerType) {
26         managedObjectContext =
27             type == .reader ? core.parentContext : core.editorContext()
28     }
29     deinit {
30         saveActionCore()
31     }
32     
33     let core = CoreDataCore.local
34     let managedObjectContext: NSManagedObjectContext
35 }
36
37 extension LocalDataStore {
38     func unmarkedDropShipHistories(befor days: Int) -> [DropShipHistory] {
39         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
40         let predicate01 = NSPredicate(format: "date < %@", date as NSDate)
41         let predicate02 = NSPredicate(format: "mark = 0 || mark = nil")
42         let predicate03 = NSPredicate(format: "mapArea IN %@", ["1", "2", "3", "4", "5", "6", "7", "8", "9"])
43         let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate01, predicate02, predicate03])
44         guard let dropHistories = try? objects(with: DropShipHistory.entity, predicate: predicate)
45             else { return [] }
46         return dropHistories
47     }
48     
49     func createDropShipHistory() -> DropShipHistory? {
50         return insertNewObject(for: DropShipHistory.entity)
51     }
52     
53     func kaihatuHistories() -> [KaihatuHistory] {
54         guard let kaihatuHistories = try? objects(with: KaihatuHistory.entity)
55             else { return [] }
56         return kaihatuHistories
57     }
58     func unmarkedKaihatuHistories(befor days: Int) -> [KaihatuHistory] {
59         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
60         let predicate01 = NSPredicate(format: "date < %@", date as NSDate)
61         let predicate02 = NSPredicate(format: "mark = 0 || mark = nil")
62         let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate01, predicate02])
63         guard let kaihatuHistories = try? objects(with: KaihatuHistory.entity, predicate: predicate)
64             else { return [] }
65         return kaihatuHistories
66     }
67     func createKaihatuHistory() -> KaihatuHistory? {
68         return insertNewObject(for: KaihatuHistory.entity)
69     }
70     
71     func kenzoMark(byDockId dockId: Int) -> KenzoMark? {
72         let predicate = NSPredicate(format: "kDockId = %ld", dockId)
73         guard let kenzoMarks = try? objects(with: KenzoMark.entity, predicate: predicate)
74             else { return nil }
75         return kenzoMarks.first
76     }
77     // swiftlint:disable function_parameter_count
78     func kenzoMark(fuel: Int,
79                    bull: Int,
80                    steel: Int,
81                    bauxite: Int,
82                    kaihatusizai: Int,
83                    kDockId: Int,
84                    shipId: Int) -> KenzoMark? {
85         // swiftlint:disable:next line_length
86         let predicate = NSPredicate(format: "fuel = %ld AND bull = %ld AND steel = %ld AND bauxite = %ld AND kaihatusizai = %ld AND kDockId = %ld AND created_ship_id = %ld",
87                                     fuel, bull, steel, bauxite, kaihatusizai, kDockId, shipId)
88         guard let kenzoMarks = try? objects(with: KenzoMark.entity, predicate: predicate)
89             else { return nil }
90         return kenzoMarks.first
91     }
92     func createKenzoMark() -> KenzoMark? {
93         return insertNewObject(for: KenzoMark.entity)
94     }
95     
96     func unmarkedKenzoHistories(befor days: Int) -> [KenzoHistory] {
97         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
98         let predicate01 = NSPredicate(format: "date < %@", date as NSDate)
99         let predicate02 = NSPredicate(format: "mark = 0 || mark = nil")
100         let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate01, predicate02])
101         guard let kenzoHistories = try? objects(with: KenzoHistory.entity, predicate: predicate)
102             else { return [] }
103         return kenzoHistories
104     }
105     func createKenzoHistory() -> KenzoHistory? {
106         return insertNewObject(for: KenzoHistory.entity)
107     }
108     
109     func hiddenDropShipHistories() -> [HiddenDropShipHistory] {
110         guard let dropShipHistories = try? objects(with: HiddenDropShipHistory.entity)
111             else { return [] }
112         return dropShipHistories
113     }
114     func createHiddenDropShipHistory() -> HiddenDropShipHistory? {
115         return insertNewObject(for: HiddenDropShipHistory.entity)
116     }
117 }