Skip to content

Commit

Permalink
Merge pull request #60 from BIDMCDigitalPsychiatry/feature/streakwidget
Browse files Browse the repository at this point in the history
Irefresh the widget from main app
  • Loading branch information
jijopulikkottil authored Jun 12, 2024
2 parents 7244db1 + 34ec406 commit 95f4750
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 78 deletions.
18 changes: 9 additions & 9 deletions StreakWidget/StreakWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ struct StreakWidgetEntryView : View {
.aspectRatio(contentMode: .fit)
.frame(width: 40)

// let daysText1: String = entry.currentStreak == 1 ? "day" : "days"
// Text("Current streak: \(entry.currentStreak) \(daysText1)")
// .font(.system(size: 12))
// .fontWeight(.black)
//
// let daysText: String = entry.longestStreak == 1 ? "day" : "days"
// Text("Longest Streak: \(entry.longestStreak) \(daysText)")
// .font(.system(size: 12))
// .fontWeight(.black)
let daysText1: String = entry.currentStreak == 1 ? "day" : "days"
Text("Current streak: \(entry.currentStreak) \(daysText1)")
.font(.system(size: 12))
.fontWeight(.black)

let daysText: String = entry.longestStreak == 1 ? "day" : "days"
Text("Longest Streak: \(entry.longestStreak) \(daysText)")
.font(.system(size: 12))
.fontWeight(.black)

Text("Way To Go!")
.font(.system(size: 14))
Expand Down
63 changes: 33 additions & 30 deletions StreakWidgetHelper/StreakWidgetHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import Foundation
//import LAMP
import WidgetKit

public class StreakWidgetHelper {

Expand Down Expand Up @@ -35,8 +36,6 @@ public class StreakWidgetHelper {
let task = URLSession.shared.dataTask(with: urlRequest(participantId: participantId)) { [weak self] data, response, error in

if let data, let urlResponse = response as? HTTPURLResponse {

print("data jj = \(String(data: data, encoding: .utf8))")
let decoder = JSONDecoder()
let formatter = ISO8601DateFormatter()
decoder.dateDecodingStrategy = .formatted(formatter)
Expand All @@ -50,7 +49,7 @@ public class StreakWidgetHelper {
if let reslut {
StreakWidgetHelper.cachedEntry = reslut
}

WidgetCenter.shared.reloadAllTimelines()
completion?(responseData.dates)


Expand Down Expand Up @@ -119,6 +118,30 @@ public class StreakWidgetHelper {
return false
}


// Function to extract unique days from an array of dates
func uniqueDays(from dates: [Date]) -> [Date] {
var uniqueDaysSet = Set<String>()
var uniqueDaysArray = [Date]()

let dateFormatter = DateFormatter()
dateFormatter.timeZone = .current
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"

for date in dates {
print("date = \(date)")
let dayString = dateFormatter.string(from: date)
print("dayString = \(dayString)")
if !uniqueDaysSet.contains(dayString) {
uniqueDaysSet.insert(dayString)
uniqueDaysArray.append(dateFormatter.date(from: dayString)!)
}
}

return uniqueDaysArray
}

public func findCurrentStreak(from dates: [Date]) -> Int {

var currentStreak = 0
Expand Down Expand Up @@ -148,34 +171,14 @@ public class StreakWidgetHelper {
return currentStreak

}

// Function to extract unique days from an array of dates
func uniqueDays(from dates: [Date]) -> [Date] {
var uniqueDaysSet = Set<String>()
var uniqueDaysArray = [Date]()

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"

for date in dates {
let dayString = dateFormatter.string(from: date)
if !uniqueDaysSet.contains(dayString) {
uniqueDaysSet.insert(dayString)
uniqueDaysArray.append(dateFormatter.date(from: dayString)!)
}
}

return uniqueDaysArray
}


public func findCurentAndLongestStreak(dates: [Date]) -> (Int, Int) {

guard dates.count > 0 else {
return (0, 0)
}
let sortedDates = dates.sorted()
let uniqueDays = uniqueDays(from: dates)
let sortedDatesDesc = uniqueDays.sorted()

//filter duplicates?

Expand All @@ -187,20 +190,20 @@ public class StreakWidgetHelper {
let today = Date()

// Iterate through the sorted dates
for i in 1..<sortedDates.count {
if areDatesConsecutive(sortedDates[i-1], sortedDates[i]) {
for i in 1..<sortedDatesDesc.count {
if areDatesConsecutive(sortedDatesDesc[i-1], sortedDatesDesc[i]) {
tempStreak += 1
} else {
tempStreak = 1
}
maxStreak = max(maxStreak, tempStreak)
}

let j = sortedDates.count - 1
let j = sortedDatesDesc.count - 1
let calendar = Calendar.current
let previousDay = calendar.date(byAdding: .day, value: -1, to: today)!
if calendar.isDate(today, inSameDayAs: sortedDates[j]) ||
calendar.isDate(previousDay, inSameDayAs: sortedDates[j]) {
if calendar.isDate(today, inSameDayAs: sortedDatesDesc[j]) ||
calendar.isDate(previousDay, inSameDayAs: sortedDatesDesc[j]) {
currentStreak = tempStreak
}

Expand Down

This file was deleted.

55 changes: 30 additions & 25 deletions mindLAMP2Tests/mindLAMP2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import XCTest
import mindLAMP_2

extension TimeZone {
static let utc = TimeZone(abbreviation: "UTC")
}


final class mindLAMP2Tests: XCTestCase {

override func setUpWithError() throws {
Expand All @@ -17,57 +22,57 @@ final class mindLAMP2Tests: XCTestCase {
let sut = StreakWidgetHelper()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = .utc
let dates = [
dateFormatter.date(from: "2024-05-01 10:00:00")!,
dateFormatter.date(from: "2024-05-01 15:00:00")!,
dateFormatter.date(from: "2024-05-02 12:00:00")!,
dateFormatter.date(from: "2024-05-02 18:00:00")!,
dateFormatter.date(from: "2024-05-03 08:00:00")!,

dateFormatter.date(from: "2023-05-05 09:00:00")!,
dateFormatter.date(from: "2023-05-06 10:00:00")!,
dateFormatter.date(from: "2023-05-07 11:00:00")!,
dateFormatter.date(from: "2023-05-08 12:00:00")!,
dateFormatter.date(from: "2024-06-12 03:34:10")!,
dateFormatter.date(from: "2024-06-11 14:53:00")!,
dateFormatter.date(from: "2024-06-11 06:54:00")!,
dateFormatter.date(from: "2024-06-10 16:17:29")!,
dateFormatter.date(from: "2024-06-10 14:42:00")!,

dateFormatter.date(from: "2024-05-05 09:00:00")!,
dateFormatter.date(from: "2024-05-06 10:00:00")!,
dateFormatter.date(from: "2024-05-07 11:00:00")!,
dateFormatter.date(from: "2024-05-08 12:00:00")!,
dateFormatter.date(from: "2024-05-08 12:10:00")!,
dateFormatter.date(from: "2024-06-02 12:00:00")!,
dateFormatter.date(from: "2024-06-03 12:00:00")!

]
let currentStreak = sut.findCurrentStreak(from: dates)

XCTAssertEqual(currentStreak, 2)
XCTAssertEqual(currentStreak, 3)
}

func testCurrentAndLongest() throws {

let sut = StreakWidgetHelper()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = .utc
let dates = [
dateFormatter.date(from: "2024-05-01 10:00:00")!,
dateFormatter.date(from: "2024-05-01 15:00:00")!,
dateFormatter.date(from: "2024-05-02 12:00:00")!,
dateFormatter.date(from: "2024-05-02 18:00:00")!,
dateFormatter.date(from: "2024-05-03 08:00:00")!,

dateFormatter.date(from: "2023-05-05 09:00:00")!,
dateFormatter.date(from: "2023-05-06 10:00:00")!,
dateFormatter.date(from: "2023-05-07 11:00:00")!,
dateFormatter.date(from: "2023-05-08 12:00:00")!,
dateFormatter.date(from: "2024-06-12 03:34:10")!,
dateFormatter.date(from: "2024-06-11 14:53:00")!,
dateFormatter.date(from: "2024-06-11 06:54:00")!,
dateFormatter.date(from: "2024-06-10 16:17:29")!,
dateFormatter.date(from: "2024-06-10 14:42:00")!,
dateFormatter.date(from: "2024-06-09 20:42:00")!,

dateFormatter.date(from: "2024-05-05 09:00:00")!,
dateFormatter.date(from: "2024-05-06 10:00:00")!,
dateFormatter.date(from: "2024-05-07 11:00:00")!,
dateFormatter.date(from: "2024-05-08 12:00:00")!,
dateFormatter.date(from: "2024-05-08 12:10:00")!,
dateFormatter.date(from: "2024-06-02 12:00:00")!,
dateFormatter.date(from: "2024-06-03 12:00:00")!

]

let streak:(current: Int, max: Int) = sut.findCurentAndLongestStreak(dates: dates)

XCTAssertEqual(streak.max, 4)
XCTAssertEqual(streak.current, 2)
XCTAssertEqual(streak.current, 3)
}

func testPerformanceExample() throws {
Expand Down

0 comments on commit 95f4750

Please sign in to comment.