Skip to content

Commit

Permalink
Merge pull request #86 from dwyl/converting-to-json
Browse files Browse the repository at this point in the history
we are converting the project details from JSON to a String (format found in #85)
  • Loading branch information
iteles committed Jun 23, 2017
2 parents 2fb75ed + d2201fc commit 17f6e23
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dwyl/TasksTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TasksTableViewController: UITableViewController, WCSessionDelegate {
if ProjectTimer.sharedInstance.isTimerRunning() {
print("cant start timer, another one is already running in \(ProjectTimer.sharedInstance.projectName)")
} else {
ProjectTimer.sharedInstance.startTimer()
ProjectTimer.sharedInstance.startTimer(name: (message["startTimerFor"] as! String?)!)
ProjectTimer.sharedInstance.projectName = message["startTimerFor"] as! String
}

Expand Down
4 changes: 4 additions & 0 deletions dwyl/dwyl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
0D2038421E6DA9EA008CB3F4 /* TasksTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D2038411E6DA9EA008CB3F4 /* TasksTableViewCell.swift */; };
0D2038441E70180C008CB3F4 /* AddTaskViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D2038431E70180C008CB3F4 /* AddTaskViewController.swift */; };
0D243CD21EAE192700053265 /* materialColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D243CD11EAE192700053265 /* materialColors.swift */; };
0D24BB911EFC0F8B009D2334 /* dateConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D24BB901EFC0F8B009D2334 /* dateConverter.swift */; };
0D42F4061EEFFD1D00471321 /* ProjectTimerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D42F4051EEFFD1D00471321 /* ProjectTimerTest.swift */; };
0D5AA1451E71881E002D87C3 /* TaskCoreData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D5AA1441E71881E002D87C3 /* TaskCoreData.swift */; };
0D662CE21E70537900AFDE32 /* Task.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D662CE11E70537900AFDE32 /* Task.swift */; };
Expand Down Expand Up @@ -125,6 +126,7 @@
0D2038411E6DA9EA008CB3F4 /* TasksTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TasksTableViewCell.swift; sourceTree = "<group>"; };
0D2038431E70180C008CB3F4 /* AddTaskViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AddTaskViewController.swift; path = ../AddTaskViewController.swift; sourceTree = "<group>"; };
0D243CD11EAE192700053265 /* materialColors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = materialColors.swift; sourceTree = "<group>"; };
0D24BB901EFC0F8B009D2334 /* dateConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = dateConverter.swift; sourceTree = "<group>"; };
0D42F4051EEFFD1D00471321 /* ProjectTimerTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProjectTimerTest.swift; sourceTree = "<group>"; };
0D5AA1441E71881E002D87C3 /* TaskCoreData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TaskCoreData.swift; sourceTree = "<group>"; };
0D662CE11E70537900AFDE32 /* Task.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Task.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -190,6 +192,7 @@
0DCC075B1E9BA9C400868547 /* formatTime.swift */,
0D243CD11EAE192700053265 /* materialColors.swift */,
0DB2792C1EC4B2350011559E /* ProjectTimer.swift */,
0D24BB901EFC0F8B009D2334 /* dateConverter.swift */,
);
name = lib;
sourceTree = "<group>";
Expand Down Expand Up @@ -546,6 +549,7 @@
0D2038111E6D82FD008CB3F4 /* time.xcdatamodeld in Sources */,
0DB2792D1EC4B2350011559E /* ProjectTimer.swift in Sources */,
0D1D19371E795B6F003C9B39 /* Project+CoreDataClass.swift in Sources */,
0D24BB911EFC0F8B009D2334 /* dateConverter.swift in Sources */,
0D11C4721E96A15200689D48 /* setNavbarLogo.swift in Sources */,
0D243CD21EAE192700053265 /* materialColors.swift in Sources */,
0D1D193A1E796B36003C9B39 /* TaskListTableViewCell.swift in Sources */,
Expand Down
19 changes: 17 additions & 2 deletions dwyl/time/ProjectTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProjectTimer {
do {
let RunningProject = try managedObjectContext!.fetch(fetchRequest)
if RunningProject.count == 1 {
startTimer()
startTimer(name: (RunningProject.first?.project_name)!)
projectName = (RunningProject.first?.project_name)!
startDate = (RunningProject.first?.task_start_date as Date?)!
updateTimer()
Expand All @@ -41,12 +41,27 @@ class ProjectTimer {
}
}

func startTimer() {
func startTimer(name: String) {
timerRunning = true
startDate = Date()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)

// create a json object.
do {
// let jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
let convertedDate = dateToString(date: startDate)
let jsonData = try JSONSerialization.data(withJSONObject: ["project_name": name, "start_date": convertedDate, "isTaskRunning": true], options: .prettyPrinted)
let jsonToString = String(data: jsonData, encoding: String.Encoding.utf8) // the data will be converted to the string

print(jsonToString ?? "no data")
}
catch {
print(error.localizedDescription)
}
}



func stopTimer() {
startDate = Date()
timer.invalidate()
Expand Down
2 changes: 1 addition & 1 deletion dwyl/time/ViewTaskViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ViewTaskViewController: UIViewController, UITableViewDataSource, UITableVi
let start_time = Date()
let project_name = self.project_name

ProjectTimer.sharedInstance.startTimer()
ProjectTimer.sharedInstance.startTimer(name: project_name)
playButton.isEnabled = false
ProjectTimer.sharedInstance.projectName = project_name
createTask(project_name: project_name, start_time: start_time)
Expand Down
21 changes: 21 additions & 0 deletions dwyl/time/dateConverter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// dateConverter.swift
// dwyl
//
// Created by Sohil Pandya on 22/06/2017.
// Copyright © 2017 dwyl. All rights reserved.
//

import Foundation

func dateToString(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
return formatter.string(from: date)
}

func stringToDate(string: String) -> Date {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
return formatter.date(from: string)!
}

0 comments on commit 17f6e23

Please sign in to comment.