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

March 14, 2024 - Coding Challenge - Qi Hu #13

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Data/timelineTestOne.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

{
"timeline":[]
}
152 changes: 136 additions & 16 deletions OpenTweet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

19 changes: 13 additions & 6 deletions OpenTweet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var coordinator: MainCoordinator?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// Override point for customization after application launch.
let navController = UINavigationController()

coordinator = MainCoordinator(navigationController: navController)
coordinator?.start()

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navController
window?.makeKeyAndVisible()

return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Expand All @@ -40,7 +49,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

27 changes: 0 additions & 27 deletions OpenTweet/Base.lproj/Main.storyboard

This file was deleted.

17 changes: 17 additions & 0 deletions OpenTweet/Coordinators/Coordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Coordinator.swift
// OpenTweet
//
// Created by HU QI on 2024-03-11.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation
import UIKit

public protocol Coordinator: AnyObject {
var childCoordinators: [Coordinator] { get set }
var navigationController: UINavigationController { get set }

func start()
}
28 changes: 28 additions & 0 deletions OpenTweet/Coordinators/MainCoordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// MainCoordinator.swift
// OpenTweet
//
// Created by HU QI on 2024-03-13.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation
import UIKit

class MainCoordinator: Coordinator {
var childCoordinators: [Coordinator] = []

var navigationController: UINavigationController

func start() {
let tweetFeedCoordinator = TweetFeedCoordinator(navigationController: navigationController)
tweetFeedCoordinator.start()
childCoordinators.append(tweetFeedCoordinator)
}

init(navigationController: UINavigationController) {
self.navigationController = navigationController
navigationController.modalTransitionStyle = .crossDissolve
navigationController.modalPresentationStyle = .fullScreen
}
}
41 changes: 41 additions & 0 deletions OpenTweet/Coordinators/TweetFeedCoordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// TweetFeedCoordinator.swift
// OpenTweet
//
// Created by HU QI on 2024-03-13.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation
import UIKit

public final class TweetFeedCoordinator: Coordinator {
public var childCoordinators: [Coordinator] = []

public var navigationController: UINavigationController

public init(navigationController: UINavigationController) {
self.navigationController = navigationController
}

public func start() {
routeToTimeline()
}

private func routeToTimeline() {
let timelineViewModel = TimelineViewModel(
timelineService: TimelineService()) { [weak self] tweet in
self?.routeToTweetThread(tweet: tweet)
}
let timelineViewController = TimelineViewController(viewModel: timelineViewModel)

navigationController.pushViewController(timelineViewController, animated: true)
}

private func routeToTweetThread(tweet: Tweet) {
let threadViewModel = ThreadViewModel(tweet: tweet)
let threadViewController = ThreadViewController(viewModel: threadViewModel)

navigationController.pushViewController(threadViewController, animated: true)
}
}
2 changes: 0 additions & 2 deletions OpenTweet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
Expand Down
13 changes: 13 additions & 0 deletions OpenTweet/Models/Timeline.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Timeline.swift
// OpenTweet
//
// Created by HU QI on 2024-03-11.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation

struct Timeline: Codable {
var timeline: [Tweet]
}
31 changes: 31 additions & 0 deletions OpenTweet/Models/Tweet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Tweet.swift
// OpenTweet
//
// Created by HU QI on 2024-03-11.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation

final class Tweet: Codable {
let id: String
let author: String
let content: String
let date: String
let avatar: String?
let inReplyTo: String?

var tweetRepliesTo: Tweet?
var tweetReplies: [Tweet]?
}

extension Tweet: Hashable {
static func == (lhs: Tweet, rhs: Tweet) -> Bool {
return lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
43 changes: 43 additions & 0 deletions OpenTweet/Services/TimelineService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// TimelineService.swift
// OpenTweet
//
// Created by HU QI on 2024-03-12.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation

protocol TimelineServiceProtocol {
func loadTimelineFeed() throws -> Timeline
}

public enum TimelineServiceError: Error {
case filePathError
case dataConversionError
case decodingError
}

final class TimelineService: TimelineServiceProtocol {
func loadTimelineFeed() throws -> Timeline {
let filename = Constants.TimelineService.fileName
let fileType = Constants.TimelineService.fileType

guard let path = Bundle.main.url(forResource: filename, withExtension: fileType, subdirectory: nil) else {
throw TimelineServiceError.filePathError
}

do {
let data = try Data(contentsOf: path)
let decoder = JSONDecoder()
do {
let result = try decoder.decode(Timeline.self, from: data)
return result
} catch {
throw TimelineServiceError.decodingError
}
} catch {
throw TimelineServiceError.dataConversionError
}
}
}
50 changes: 50 additions & 0 deletions OpenTweet/Shared/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Constants.swift
// OpenTweet
//
// Created by HU QI on 2024-03-13.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import Foundation

enum Constants {
enum Dimens {
static let cellCornerRadius: CGFloat = 5
static let padding: CGFloat = 12

enum FontSize {
static let small: CGFloat = 12
static let body: CGFloat = 16
static let large: CGFloat = 20
}
}

enum TweetCell {
static let reuseIdentifier = "OpenTweet.TweetCell"
static let avatarImageHeight: CGFloat = 50
static let avatarImageWidth: CGFloat = 50
static let avatarImageCornerRadius: CGFloat = 20
}

enum ThreadHeader {
static let reuseIdentifier = "OpenTweet.ThreadHeaderView"
}

enum TimelineService {
static let fileName = "timeline"
static let fileType = "json"
}

enum TimelineView {
static let title = "Timeline"
}

enum ThreadView {
static let title = "Thread"
static let headerHeight: CGFloat = 40
static let originalTweetHeaderTitle = "Tweet Details"
static let tweetRepliesToHeaderTitle = "Replies To"
static let tweetRepliesHeaderTitle = "Replies"
}
}
50 changes: 50 additions & 0 deletions OpenTweet/Shared/UIImageViewExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// UIImageViewExtension.swift
// OpenTweet
//
// Created by HU QI on 2024-03-13.
// Copyright © 2024 OpenTable, Inc. All rights reserved.
//

import UIKit

let imageCache = NSCache<NSString, UIImage>()

extension UIImageView {
/// Loads an image from a URL and saves it into an image cache, returns
/// the image if already available in the cache.
/// - Parameter urlString: String representation of the URL to load the image from
/// - Parameter placeholder: An optional placeholder to show while the image is being fetched
/// - Returns: A reference to the data task in order to pause, cancel, resume, etc.
@discardableResult
func loadImageFromURL(urlString: String,
placeholder: UIImage? = nil,
urlSession: URLSession = URLSession.shared,
completionBlock: (() -> Void)? = nil) -> URLSessionDataTask? {
self.image = nil
let key = NSString(string: urlString)
if let cachedImage = imageCache.object(forKey: key) {
self.image = cachedImage
return nil
}
if let placeholder = placeholder {
self.image = placeholder
}
guard let url = URL(string: urlString) else {
return nil
}
let task = urlSession.dataTask(with: url) { data, _, _ in
DispatchQueue.main.async {
if let data = data,
let downloadedImage = UIImage(data: data) {
imageCache.setObject(downloadedImage, forKey: NSString(string: urlString))
self.image = downloadedImage
completionBlock?()
}

}
}
task.resume()
return task
}
}
Loading