Skip to content

Commit

Permalink
date formating extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
grenos committed Apr 4, 2020
1 parent db5b277 commit f16d6d7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
8 changes: 8 additions & 0 deletions GHFollowers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
7DA45F812433DE1300AB426F /* GFTitleLabel-Secondary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DA45F802433DE1300AB426F /* GFTitleLabel-Secondary.swift */; };
7DA45F832433F4FC00AB426F /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DA45F822433F4FC00AB426F /* Constants.swift */; };
7DA45F852433FAF800AB426F /* GFUserInfoheaderVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DA45F842433FAF800AB426F /* GFUserInfoheaderVC.swift */; };
7DB735092438AD0F00E7B09B /* Date+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB735082438AD0F00E7B09B /* Date+Ext.swift */; };
7DB7350B2438AD9000E7B09B /* String+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7350A2438AD9000E7B09B /* String+Ext.swift */; };
7DFAC88A242A86D200F5780C /* GFAlertContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -71,6 +73,8 @@
7DA45F802433DE1300AB426F /* GFTitleLabel-Secondary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GFTitleLabel-Secondary.swift"; sourceTree = "<group>"; };
7DA45F822433F4FC00AB426F /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
7DA45F842433FAF800AB426F /* GFUserInfoheaderVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFUserInfoheaderVC.swift; sourceTree = "<group>"; };
7DB735082438AD0F00E7B09B /* Date+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Ext.swift"; sourceTree = "<group>"; };
7DB7350A2438AD9000E7B09B /* String+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Ext.swift"; sourceTree = "<group>"; };
7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFAlertContainerView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -155,6 +159,8 @@
isa = PBXGroup;
children = (
7D98BB4B242A7327006C53E9 /* UIViewController+EXT.swift */,
7DB735082438AD0F00E7B09B /* Date+Ext.swift */,
7DB7350A2438AD9000E7B09B /* String+Ext.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -347,11 +353,13 @@
7D98BB4C242A7327006C53E9 /* UIViewController+EXT.swift in Sources */,
7DA45F812433DE1300AB426F /* GFTitleLabel-Secondary.swift in Sources */,
7D98BB44242A29B4006C53E9 /* FollowerListVC.swift in Sources */,
7DB7350B2438AD9000E7B09B /* String+Ext.swift in Sources */,
7DA45F6C242BAEEC00AB426F /* ErrorMessage.swift in Sources */,
7D7C6654243897F300733687 /* GFItemInfoVC.swift in Sources */,
7D7C66582438A22C00733687 /* GFFollowerItemVC.swift in Sources */,
7DA45F73242E332000AB426F /* UIHelper.swift in Sources */,
7DA45F69242B831900AB426F /* NetworkManager.swift in Sources */,
7DB735092438AD0F00E7B09B /* Date+Ext.swift in Sources */,
7D98BB282428FE73006C53E9 /* AppDelegate.swift in Sources */,
7DA45F852433FAF800AB426F /* GFUserInfoheaderVC.swift in Sources */,
7D7C665224388F0900733687 /* GFItemInfoView.swift in Sources */,
Expand Down
Binary file not shown.
21 changes: 21 additions & 0 deletions GHFollowers/Extensions/Date+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Date+Ext.swift
// GHFollowers
//
// Created by Vasileios Gkreen on 04/04/2020.
// Copyright © 2020 Vasileios Gkreen. All rights reserved.
//

import Foundation


extension Date {

func convertToMonthYearFormat() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM yyyy"

return dateFormatter.string(from: self)
}

}
34 changes: 34 additions & 0 deletions GHFollowers/Extensions/String+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// String+Ext.swift
// GHFollowers
//
// Created by Vasileios Gkreen on 04/04/2020.
// Copyright © 2020 Vasileios Gkreen. All rights reserved.
//

import Foundation


// EXTENSION TO GET A DATE STRING FROM SERVER AND CONVERT IT TO A DIFERNET TYPE OF DATE FORMAT
// the first part converts the string taken from the server to a Date format

extension String {


// this function returns a Date? which is optioanl
func convertToDate() -> Date? {

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

return dateFormatter.date(from: self)
}


func convertToDisplayFormat() -> String {
guard let date = self.convertToDate() else {return "N/A"}
return date.convertToMonthYearFormat()
}
}
7 changes: 6 additions & 1 deletion GHFollowers/Screens/UserInfoVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class UserInfoVC: UIViewController {
let headerView = UIView()
let itemViewOne = UIView()
let itemViewTwo = UIView()
let dateLabel = GFBodyLabel(textAlignment: .center)
var itemViews: [UIView] = []

// username gets passed here from FollowerListVC from function -- extension FollowerListVC: UICollectionViewDelegate --
Expand Down Expand Up @@ -52,6 +53,7 @@ class UserInfoVC: UIViewController {
self.add(childVC: GFUserInfoheaderVC(user: user), to: self.headerView)
self.add(childVC: GFRepoItemVC(user: user), to: self.itemViewOne)
self.add(childVC: GFFollowerItemVC(user: user), to: self.itemViewTwo)
self.dateLabel.text = "GitHub since \(user.createdAt.convertToDisplayFormat())"
}

case .failure(let error):
Expand All @@ -64,7 +66,7 @@ class UserInfoVC: UIViewController {

// MARK: UI layout
func layoutUI() {
itemViews = [headerView, itemViewOne, itemViewTwo]
itemViews = [headerView, itemViewOne, itemViewTwo, dateLabel]
let padding: CGFloat = 20

for itemView in itemViews {
Expand All @@ -88,6 +90,9 @@ class UserInfoVC: UIViewController {

itemViewTwo.topAnchor.constraint(equalTo: itemViewOne.bottomAnchor, constant: padding),
itemViewTwo.heightAnchor.constraint(equalToConstant: 140),

dateLabel.topAnchor.constraint(equalTo: itemViewTwo.bottomAnchor, constant: padding),
dateLabel.heightAnchor.constraint(equalToConstant: 18)
])
}

Expand Down

0 comments on commit f16d6d7

Please sign in to comment.