Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Danilo silveira #19

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Created Tweet View Model to handle the logic and send to the view
danilodns committed Jul 24, 2024
commit fb0b6905ee703011e64dbc0be149bbed48b52cd8
12 changes: 12 additions & 0 deletions OpenTweet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
009C4C9B1D9F0D4100F0BC6C /* timeline.json in Resources */ = {isa = PBXBuildFile; fileRef = 009C4C9A1D9F0D4100F0BC6C /* timeline.json */; };
F799789D2C516EB50059C33E /* Tweet.swift in Sources */ = {isa = PBXBuildFile; fileRef = F799789C2C516EB50059C33E /* Tweet.swift */; };
F79978A02C516F030059C33E /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F799789F2C516F030059C33E /* DateExtension.swift */; };
F79978A62C5172010059C33E /* TimeLineViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79978A52C5172010059C33E /* TimeLineViewModel.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
@@ -54,6 +55,7 @@
009C4C9D1D9F104800F0BC6C /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
F799789C2C516EB50059C33E /* Tweet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tweet.swift; sourceTree = "<group>"; };
F799789F2C516F030059C33E /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = "<group>"; };
F79978A52C5172010059C33E /* TimeLineViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeLineViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
@@ -106,6 +108,7 @@
009C4C6A1D9F0CD600F0BC6C /* OpenTweet */ = {
isa = PBXGroup;
children = (
F79978A42C5171EC0059C33E /* ViewModel */,
F799789E2C516EE60059C33E /* Extensions */,
F799789B2C516E950059C33E /* Model */,
009C4C6B1D9F0CD600F0BC6C /* AppDelegate.swift */,
@@ -160,6 +163,14 @@
path = Extensions;
sourceTree = "<group>";
};
F79978A42C5171EC0059C33E /* ViewModel */ = {
isa = PBXGroup;
children = (
F79978A52C5172010059C33E /* TimeLineViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
@@ -300,6 +311,7 @@
files = (
F799789D2C516EB50059C33E /* Tweet.swift in Sources */,
F79978A02C516F030059C33E /* DateExtension.swift in Sources */,
F79978A62C5172010059C33E /* TimeLineViewModel.swift in Sources */,
009C4C6E1D9F0CD600F0BC6C /* TimelineViewController.swift in Sources */,
009C4C6C1D9F0CD600F0BC6C /* AppDelegate.swift in Sources */,
);
61 changes: 61 additions & 0 deletions OpenTweet/ViewModel/TimeLineViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// TimeLineViewModel.swift
// OpenTweet
//
// Created by Danilo Silveira on 2024-07-24.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation

protocol TimeLineProtocol {
func reloadContent()
}

class TimeLineViewModel {
private var timelineTweets: [Tweet] = []
private var timelineProtocol : TimeLineProtocol?

func setProtocol(timelineProtocol: TimeLineProtocol) {
self.timelineProtocol = timelineProtocol
}

func getTimeLineTweetCounts() -> Int {
timelineTweets.count
}

func getTweet(for Index: Int) -> Tweet {
timelineTweets[Index]
}

func fetchTimelineFromJson() {
do {
guard let path = Bundle.main.path(forResource: "timeline", ofType: "json"),
let data = try String(contentsOfFile: path).data(using: .utf8) else { return }
let decoder = JSONDecoder()
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate]

decoder.dateDecodingStrategy = .custom({ decoder in
let container = try decoder.singleValueContainer()
let dateString = try container.decode(String.self)

if let date = formatter.date(from: dateString) {
return date
}

throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot decode date string \(dateString)")
})

// Decoding data for the Tweet Model
let timeLine = try decoder.decode([String:[Tweet]].self, from: data)

// Sorting in ascending order and trigger the protocol to reload the table
timelineTweets = timeLine["timeline"]?.sorted(by: { $0.date < $1.date}) ?? []
self.timelineProtocol?.reloadContent()

} catch {
print(error.localizedDescription)
}
}
}