From 290da8d1d2b8035c4d70311e947ae16edd20cad8 Mon Sep 17 00:00:00 2001 From: Dragos Neagu Date: Wed, 15 Jan 2020 13:43:18 +0000 Subject: [PATCH] Implement birthdayMatches(date:) method. --- KNContacts.podspec | 2 +- KNContacts/KNContact.swift | 24 +++++++++++++++++++----- KNContactsTests/KNContactTests.swift | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/KNContacts.podspec b/KNContacts.podspec index c1e974c..b60ea2e 100644 --- a/KNContacts.podspec +++ b/KNContacts.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "KNContacts" - s.version = "1.2.1" + s.version = "1.2.2" s.summary = "KNContacts is a wrapper framework for CNContacts for easier access, scheduling and ordering." s.description = <<-DESC diff --git a/KNContacts/KNContact.swift b/KNContacts/KNContact.swift index f97c9ff..783e153 100644 --- a/KNContacts/KNContact.swift +++ b/KNContacts/KNContact.swift @@ -14,7 +14,7 @@ KNContact class is a wrapper class which gives quick access to helper methods fo birthday and age information, first email address or phone number. - Author: dragosrobertn -- Version: 1.2.1 +- Version: 1.2.2 **/ public struct KNContact { @@ -165,12 +165,25 @@ public struct KNContact { Helper method to find out if current date matches the contact's birthday. - Author: dragosrobertn - - Returns: Returns a boolean value representing whether the today is the contact's birthday. + - Returns: Returns a boolean value representing whether current date is the contact's birthday. - Version: 1.0.0 */ public func isBirthdayToday() -> Bool { - let todayFormattedString = KNDatesUtils.string(from: Date(), format: .dayAndMonth) - return self.formatBirthday() == todayFormattedString + return self.birthdayMatches(date: Date()) + } + + /** + Helper method to find out if current date matches the contact's birthday. + + - Author: dragosrobertn + - Parameters: + - date: The date to which to compare the contact's birthday + - Returns: Returns a boolean value representing whether the passed date matches the contact's birthday + - Version: 1.2.2 + */ + public func birthdayMatches(date: Date) -> Bool { + let formattedDate = KNDatesUtils.string(from: date, format: .dayAndMonth) + return self.formatBirthday() == formattedDate } /** @@ -181,7 +194,8 @@ public struct KNContact { - in: The number of days as an integer representing the number of days to check if the birthday is upcoming - startingDate: The date from which to start checking if the birthday is upcoming. Default is today's date and the starting date will be excluded.. - - Returns: Returns a bool representing whether the today is the contact's birthday. False if the contact doesn't have birthday information available. + - Returns: Returns a bool representing whether the contact's birthday is in the interval between the starting date and the number of following days provided by the days param. + False if the contact doesn't have birthday information available. - Version: 1.2.1 */ public func isBirthdayComing(in days: Int, startingDate: Date = Date()) -> Bool { diff --git a/KNContactsTests/KNContactTests.swift b/KNContactsTests/KNContactTests.swift index f660f26..b7dc621 100644 --- a/KNContactsTests/KNContactTests.swift +++ b/KNContactsTests/KNContactTests.swift @@ -221,6 +221,32 @@ class KNContactTests: XCTestCase { XCTAssertTrue(contact.isBirthdayToday()) } + func testBirthdayMatchesCurrentDateIsTrue() { + let mutableContact = UnitTestsContactHelpers.getMutableContact() + + let today = Date() + let todayComponents = calendar.dateComponents([.day, .month], from: today) + mutableContact.birthday?.day = todayComponents.day + mutableContact.birthday?.month = todayComponents.month + + let contact = KNContact(for: mutableContact) + + XCTAssertTrue(contact.birthdayMatches(date: today)) + } + + func testBirthdayMatchesTomorrowsDateIsFalse() { + let mutableContact = UnitTestsContactHelpers.getMutableContact() + + let tomorrow = calendar.date(byAdding: .day, value: 1, to: Date())! + let todayComponents = calendar.dateComponents([.day, .month], from: Date()) + mutableContact.birthday?.day = todayComponents.day + mutableContact.birthday?.month = todayComponents.month + + let contact = KNContact(for: mutableContact) + + XCTAssertFalse(contact.birthdayMatches(date: tomorrow)) + } + func testGetFirstEmailAddress() { let contact = UnitTestsContactHelpers.getKNContact()