From a4e8569f85e85b7b14d2f6878e7a578939d36249 Mon Sep 17 00:00:00 2001 From: masakih Date: Wed, 4 Jan 2017 11:09:40 +0900 Subject: [PATCH] =?utf8?q?HMPeriodicNotifier=E3=82=92Swift=E3=81=AB?= =?utf8?q?=E5=A4=89=E6=8F=9B=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- KCD.xcodeproj/project.pbxproj | 10 ++-- KCD/HMAppDelegate.m | 6 +-- KCD/HMPeriodicNotifier.h | 20 -------- KCD/HMPeriodicNotifier.m | 87 -------------------------------- KCD/HMPeriodicNotifier.swift | 65 ++++++++++++++++++++++++ KCD/HMResourceHistoryManager.m | 8 +-- KCD/HMStrengthenListViewController.swift | 4 +- KCD/KCD-Bridging-Header.h | 1 - 8 files changed, 77 insertions(+), 124 deletions(-) delete mode 100644 KCD/HMPeriodicNotifier.h delete mode 100644 KCD/HMPeriodicNotifier.m create mode 100644 KCD/HMPeriodicNotifier.swift diff --git a/KCD.xcodeproj/project.pbxproj b/KCD.xcodeproj/project.pbxproj index 0c0777d8..b9705386 100644 --- a/KCD.xcodeproj/project.pbxproj +++ b/KCD.xcodeproj/project.pbxproj @@ -69,6 +69,7 @@ F4513EF318B991F800073552 /* HMMemberBasicCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4513EF218B991F800073552 /* HMMemberBasicCommand.m */; }; F4513EF618B9F29800073552 /* HMMemberShipCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4513EF518B9F29800073552 /* HMMemberShipCommand.m */; }; F4513EFA18BB699700073552 /* HMShipWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = F4513EF818BB699700073552 /* HMShipWindowController.m */; }; + F45771231E1BCC7C008A9215 /* HMPeriodicNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = F45771221E1BCC7C008A9215 /* HMPeriodicNotifier.swift */; }; F45F3BE11E067A870009434E /* HMHistoryItemCleaner.swift in Sources */ = {isa = PBXBuildFile; fileRef = F45F3BE01E067A870009434E /* HMHistoryItemCleaner.swift */; }; F45F3BE31E06A60A0009434E /* HMUpgradableShipsWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F45F3BE21E06A60A0009434E /* HMUpgradableShipsWindowController.swift */; }; F45FBB6F1E0FDB33000E72B9 /* HMStrengthenListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F45FBB6E1E0FDB33000E72B9 /* HMStrengthenListViewController.swift */; }; @@ -266,7 +267,6 @@ F4ECA36F1C6102FC0098AB6A /* HMMillisecondTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = F4ECA36E1C6102FC0098AB6A /* HMMillisecondTransformer.m */; }; F4ED5D2D1B7C677E00D1BDEF /* HMGuardShelterCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4ED5D2C1B7C677E00D1BDEF /* HMGuardShelterCommand.m */; }; F4F479261C074E9900D0BF69 /* HMCombinedCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F479251C074E9900D0BF69 /* HMCombinedCommand.m */; }; - F4F6E80C1B2BC81C0008B66E /* HMPeriodicNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F6E80B1B2BC81C0008B66E /* HMPeriodicNotifier.m */; }; F4F6E8291B3045970008B66E /* CacheStoragePolicy.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F6E8221B3045970008B66E /* CacheStoragePolicy.m */; }; F4F6E82A1B3045970008B66E /* CanonicalRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F6E8241B3045970008B66E /* CanonicalRequest.m */; }; F4F6E82B1B3045970008B66E /* CustomHTTPProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F6E8261B3045970008B66E /* CustomHTTPProtocol.m */; }; @@ -435,6 +435,7 @@ F4513EF518B9F29800073552 /* HMMemberShipCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMMemberShipCommand.m; sourceTree = ""; }; F4513EF718BB699700073552 /* HMShipWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMShipWindowController.h; sourceTree = ""; }; F4513EF818BB699700073552 /* HMShipWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMShipWindowController.m; sourceTree = ""; }; + F45771221E1BCC7C008A9215 /* HMPeriodicNotifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMPeriodicNotifier.swift; sourceTree = ""; }; F45F3BDD1E0679D80009434E /* KCD-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "KCD-Bridging-Header.h"; sourceTree = ""; }; F45F3BE01E067A870009434E /* HMHistoryItemCleaner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMHistoryItemCleaner.swift; sourceTree = ""; }; F45F3BE21E06A60A0009434E /* HMUpgradableShipsWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMUpgradableShipsWindowController.swift; sourceTree = ""; }; @@ -725,8 +726,6 @@ F4ED5D2C1B7C677E00D1BDEF /* HMGuardShelterCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMGuardShelterCommand.m; sourceTree = ""; }; F4F479241C074E9900D0BF69 /* HMCombinedCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMCombinedCommand.h; sourceTree = ""; }; F4F479251C074E9900D0BF69 /* HMCombinedCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMCombinedCommand.m; sourceTree = ""; }; - F4F6E80A1B2BC81C0008B66E /* HMPeriodicNotifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMPeriodicNotifier.h; sourceTree = ""; }; - F4F6E80B1B2BC81C0008B66E /* HMPeriodicNotifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMPeriodicNotifier.m; sourceTree = ""; }; F4F6E8211B3045970008B66E /* CacheStoragePolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CacheStoragePolicy.h; sourceTree = ""; }; F4F6E8221B3045970008B66E /* CacheStoragePolicy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CacheStoragePolicy.m; sourceTree = ""; }; F4F6E8231B3045970008B66E /* CanonicalRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CanonicalRequest.h; sourceTree = ""; }; @@ -1155,8 +1154,7 @@ F49546251B1355EC00E0D8D0 /* HMBookmarkItem.m */, F49546291B14CBA600E0D8D0 /* HMBookmarkManager.h */, F495462A1B14CBA600E0D8D0 /* HMBookmarkManager.m */, - F4F6E80A1B2BC81C0008B66E /* HMPeriodicNotifier.h */, - F4F6E80B1B2BC81C0008B66E /* HMPeriodicNotifier.m */, + F45771221E1BCC7C008A9215 /* HMPeriodicNotifier.swift */, F4F9F8A71C796C2E00D0EE76 /* HMChangeHenseiNotification.h */, F4F9F8A81C796C2E00D0EE76 /* HMChangeHenseiNotification.m */, F4F9F8A41C78596900D0EE76 /* HMAnchorageRepairManager.h */, @@ -1842,6 +1840,7 @@ F48A70671DE5D62C005DFC0B /* HMOrderedSetToArrayTransformer.m in Sources */, F44AB23E19E824D700F217AF /* HMKCSlotItemObject.m in Sources */, F4D6BCB01AA2A97200742FEB /* HMLengTransformer.m in Sources */, + F45771231E1BCC7C008A9215 /* HMPeriodicNotifier.swift in Sources */, F4B414F518B1888800468DAF /* HMMasterFurnitureCommand.m in Sources */, F4AA0601192E1FE900844688 /* HMBattleCommand.m in Sources */, F495462B1B14CBA600E0D8D0 /* HMBookmarkManager.m in Sources */, @@ -1965,7 +1964,6 @@ F43BA4A4197BE6C200CFF788 /* HMRealDestroyShipCommand.m in Sources */, F4F9F8711C6A306A00D0EE76 /* HMKCBattle+CoreDataProperties.m in Sources */, F4B37EF619FBE801002F371A /* HMRemodelSlotItemCommand.m in Sources */, - F4F6E80C1B2BC81C0008B66E /* HMPeriodicNotifier.m in Sources */, F4B414EC18B0A7D600468DAF /* HMMasterUseItemCommand.m in Sources */, F45FBB731E116F53000E72B9 /* HMMainTabVIewItemViewController.swift in Sources */, F4F9F8A61C78596900D0EE76 /* HMAnchorageRepairManager.m in Sources */, diff --git a/KCD/HMAppDelegate.m b/KCD/HMAppDelegate.m index 13321901..f35060ff 100644 --- a/KCD/HMAppDelegate.m +++ b/KCD/HMAppDelegate.m @@ -16,8 +16,6 @@ #import "HMFleetManager.h" -#import "HMPeriodicNotifier.h" - #import "HMTSVSupport.h" #import "CustomHTTPProtocol.h" @@ -148,8 +146,8 @@ } NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; - _historyCleanNotifer = [HMPeriodicNotifier periodicNotifierWithHour:0 minutes:7]; - [nc addObserverForName:HMPeriodicNotification + _historyCleanNotifer = [[HMPeriodicNotifier alloc] initWithHour:0 minutes:7]; + [nc addObserverForName:[HMPeriodicNotifier notificationName] object:_historyCleanNotifer queue:nil usingBlock:^(NSNotification * _Nonnull note) { diff --git a/KCD/HMPeriodicNotifier.h b/KCD/HMPeriodicNotifier.h deleted file mode 100644 index 634ea6a8..00000000 --- a/KCD/HMPeriodicNotifier.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// HMPeriodicNotifier.h -// KCD -// -// Created by Hori,Masaki on 2015/06/13. -// Copyright (c) 2015年 Hori,Masaki. All rights reserved. -// - -#import - -@interface HMPeriodicNotifier : NSObject - -// 24-hour clock -- (instancetype)initWithHour:(NSUInteger)hour minutes:(NSUInteger)minutes; -+ (instancetype)periodicNotifierWithHour:(NSUInteger)hour minutes:(NSUInteger)minutes; - -@end - - -extern NSString *HMPeriodicNotification; diff --git a/KCD/HMPeriodicNotifier.m b/KCD/HMPeriodicNotifier.m deleted file mode 100644 index ee5543fb..00000000 --- a/KCD/HMPeriodicNotifier.m +++ /dev/null @@ -1,87 +0,0 @@ -// -// HMPeriodicNotifier.m -// KCD -// -// Created by Hori,Masaki on 2015/06/13. -// Copyright (c) 2015年 Hori,Masaki. All rights reserved. -// - -#import "HMPeriodicNotifier.h" - - -NSString *HMPeriodicNotification = @"HMPeriodicNotification"; - -@interface HMPeriodicNotifier () -@property NSUInteger hour; -@property NSUInteger minutes; - -@property (nonatomic, strong) NSDateComponents *currentDay; -@property (nonatomic, strong) NSTimer *timer; -@end - -@implementation HMPeriodicNotifier - -+ (instancetype)periodicNotifierWithHour:(NSUInteger)hour minutes:(NSUInteger)minutes -{ - return [[self alloc] initWithHour:hour minutes:minutes]; -} -- (instancetype)initWithHour:(NSUInteger)hour minutes:(NSUInteger)minutes -{ - self = [super init]; - if(self) { - _hour = hour; - _minutes = minutes; - [self registerNotifications]; - [self notifyIfNeeded:nil]; - } - return self; -} - -- (void)registerNotifications -{ - NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; - - [nc addObserver:self - selector:@selector(notifyIfNeeded:) - name:NSSystemTimeZoneDidChangeNotification - object:nil]; - [nc addObserver:self - selector:@selector(notifyIfNeeded:) - name:NSSystemClockDidChangeNotification - object:nil]; - [nc addObserver:self - selector:@selector(notifyIfNeeded:) - name:NSWorkspaceDidWakeNotification - object:nil]; -} - -- (void)notifyIfNeeded:(id)timer -{ - NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; - NSCalendarUnit unit = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; - NSDateComponents *currentDay = [[NSCalendar currentCalendar] components:unit fromDate:now]; - currentDay.hour = self.hour; - currentDay.minute = self.minutes; - NSDate *notifyDate = [[NSCalendar currentCalendar] dateFromComponents:currentDay]; - - if([now compare:notifyDate] == NSOrderedDescending) { - currentDay.day += 1; - [[NSNotificationCenter defaultCenter] postNotificationName:HMPeriodicNotification - object:self]; - } - - if(self.timer.valid) { - [self.timer invalidate]; - } - - NSDate *nextNotifyDate = [[NSCalendar currentCalendar] dateFromComponents:currentDay]; - NSTimeInterval nextNotifyTime = [nextNotifyDate timeIntervalSinceNow]; - - self.timer = [NSTimer scheduledTimerWithTimeInterval:nextNotifyTime + 0.1 - target:self - selector:_cmd - userInfo:nil - repeats:NO]; -} - -@end diff --git a/KCD/HMPeriodicNotifier.swift b/KCD/HMPeriodicNotifier.swift new file mode 100644 index 00000000..804d388f --- /dev/null +++ b/KCD/HMPeriodicNotifier.swift @@ -0,0 +1,65 @@ +// +// HMPeriodicNotifier.swift +// KCD +// +// Created by Hori,Masaki on 2017/01/03. +// Copyright © 2017年 Hori,Masaki. All rights reserved. +// + +import Cocoa + +extension Notification.Name { + static let Periodic = Notification.Name("com.masakih.KCD.Notification.Periodic") +} + +fileprivate extension Selector { + static let notifyIfNeeded = #selector(HMPeriodicNotifier.notifyIfNeeded(_:)) +} + +class HMPeriodicNotifier: NSObject { + init(hour: Int, minutes: Int) { + self.hour = hour + self.minutes = minutes + super.init() + + let nc = NotificationCenter.default + nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: notify) + nc.addObserver(forName: .NSSystemClockDidChange, object: nil, queue: nil, using: notify) + nc.addObserver(forName: .NSWorkspaceDidWake, object: nil, queue: nil, using: notify) + + notifyIfNeeded(nil) + } + + static var notificationName: String { + get { + return Notification.Name.Periodic.rawValue + } + } + + private let hour: Int + private let minutes: Int + + private func notify(_ notification: Notification) { + notifyIfNeeded(nil) + } + + @objc fileprivate func notifyIfNeeded(_ timer: Timer?) { + let now = Date(timeIntervalSinceNow: 0.0) + let unit: Set = [.era, .year, .month, .day] + var currentDay = Calendar.current.dateComponents(unit, from: now) + currentDay.hour = hour + currentDay.minute = minutes + if let notifyDate = Calendar.current.date(from: currentDay) { + if now.compare(notifyDate) == .orderedDescending { + currentDay.day? += 1 + NotificationCenter.default.post(name: .Periodic, object: self) + } + } + if let v = timer?.isValid, v { + timer?.invalidate() + } + guard let nextNotifyDate = Calendar.current.date(from: currentDay) else { fatalError("Can not create time of notify") } + let nextNotifyTime = nextNotifyDate.timeIntervalSinceNow + 0.1 + Timer.scheduledTimer(timeInterval: nextNotifyTime, target: self, selector: .notifyIfNeeded, userInfo: nil, repeats: false) + } +} diff --git a/KCD/HMResourceHistoryManager.m b/KCD/HMResourceHistoryManager.m index fc051615..8eedc08a 100644 --- a/KCD/HMResourceHistoryManager.m +++ b/KCD/HMResourceHistoryManager.m @@ -8,11 +8,11 @@ #import "HMResourceHistoryManager.h" +#import "KCD-Swift.h" + #import "HMServerDataStore.h" #import "HMResourceHistoryDataStore.h" -#import "HMPeriodicNotifier.h" - #import "HMKCMaterial.h" #import "HMKCBasic.h" #import "HMKCResource.h" @@ -38,10 +38,10 @@ static HMResourceHistoryManager *sInstance; - (void)run { - self.periodicNotification = [HMPeriodicNotifier periodicNotifierWithHour:23 minutes:3]; + self.periodicNotification = [[HMPeriodicNotifier alloc] initWithHour:23 minutes:3]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reduce:) - name:HMPeriodicNotification + name:[HMPeriodicNotifier notificationName] object:self.periodicNotification]; [self notifyIfNeeded:nil]; diff --git a/KCD/HMStrengthenListViewController.swift b/KCD/HMStrengthenListViewController.swift index 63aa09cc..283d0741 100644 --- a/KCD/HMStrengthenListViewController.swift +++ b/KCD/HMStrengthenListViewController.swift @@ -49,8 +49,8 @@ class HMStrengthenListViewController: HMMainTabVIewItemViewController { } let nc = NotificationCenter.default - nc.addObserver(self, selector: .buildList, name: .HMPeriodic, object: notifier) - nc.addObserver(self, selector: .downloadPList, name: .HMPeriodic, object: plistDownloadNotifier) + nc.addObserver(self, selector: .buildList, name: .Periodic, object: notifier) + nc.addObserver(self, selector: .downloadPList, name: .Periodic, object: plistDownloadNotifier) } fileprivate static let groupNameKey = "group" diff --git a/KCD/KCD-Bridging-Header.h b/KCD/KCD-Bridging-Header.h index 9d8237da..8421a340 100644 --- a/KCD/KCD-Bridging-Header.h +++ b/KCD/KCD-Bridging-Header.h @@ -11,7 +11,6 @@ // Other #import "HMEnhancementListItem.h" -#import "HMPeriodicNotifier.h" #import "HMMissionStatus.h" -- 2.11.0