Created
March 19, 2023 17:17
-
-
Save ajumalebrahim/392845887dcb14226393d2c12cf15deb to your computer and use it in GitHub Desktop.
Test alraedah
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
enum ImageFetchingError: Error { | |
case timeout | |
case unknown | |
} | |
protocol CatImageCellModel { | |
var placeholderImage: UIImage { get } | |
func fetchCatImage(completion: @escaping (Result<UIImage, ImageFetchingError>) -> Void) | |
} | |
class CatImageCell: UICollectionViewCell { | |
private var currentModel: CatImageCellModel? | |
private var retryCount = 0 | |
let imageView = UIImageView() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
imageView.contentMode = .scaleAspectFit | |
contentView.addSubview(imageView) | |
imageView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), | |
imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), | |
imageView.topAnchor.constraint(equalTo: contentView.topAnchor), | |
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) | |
]) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func set(model: CatImageCellModel) { | |
currentModel = model | |
imageView.image = model.placeholderImage | |
model.fetchCatImage { [weak self] result in | |
guard let self = self else { return } | |
switch result { | |
case .success(let image): | |
DispatchQueue.main.async { | |
self.imageView.image = image | |
} | |
case .failure(let error): | |
if error == .timeout && self.retryCount < 2 { | |
self.retryCount += 1 | |
self.set(model: model) | |
} else { | |
DispatchQueue.main.async { | |
self.imageView.image = model.placeholderImage | |
} | |
} | |
} | |
} | |
} | |
override func prepareForReuse() { | |
super.prepareForReuse() | |
imageView.image = nil | |
currentModel = nil | |
retryCount = 0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func toDate() -> Date? { | |
let formatter = DateFormatter() | |
var result: Date? | |
// First Format - "12 Dec 2018 22:15:00 CST" | |
formatter.dateFormat = "dd MMM yyyy HH:mm:ss" | |
formatter.locale = Locale(identifier: "en_US_POSIX") | |
var components = self.components(separatedBy: " ") | |
let timeZone = TimeZone(abbreviation: components.last!) | |
formatter.timeZone = TimeZone(abbreviation: components.last!) | |
components.removeLast() | |
result = formatter.date(from: components.joined(separator: " ")) | |
if let r1 = result { return r1 } | |
// Second Format - Sunday 26/April/2020 | |
formatter.dateFormat = "EEEE dd/MMMM/yyyy" | |
result = formatter.date(from: self) | |
if let r2 = result { return r2 } | |
// Third Format - "6 (Mon) . 1 (Jan) .2020" | |
formatter.dateFormat = "d (E) . M (MMM) .yyyy" | |
result = formatter.date(from: self) | |
if let r3 = result { return r3 } | |
// Otherwise | |
return nil | |
} | |
} | |
extension Date { | |
var day: Int { | |
return Calendar.current.component(.day, from: self) | |
} | |
var nameOfDay: String { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "EEEE" | |
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | |
return dateFormatter.string(from: self) | |
} | |
var month: Int { | |
return Calendar.current.component(.month, from: self) | |
} | |
var nameOfTheMonth: String { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "MMMM" | |
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | |
return dateFormatter.string(from: self) | |
} | |
var year: Int { | |
return Calendar.current.component(.year, from: self) | |
} | |
static func numberOfDaysBetween(date1: Date, date2: Date) -> Int { | |
let calendar = Calendar.current | |
let startOfDay1 = calendar.startOfDay(for: date1) | |
let startOfDay2 = calendar.startOfDay(for: date2) | |
let components = calendar.dateComponents([.day], from: startOfDay1, to: startOfDay2) | |
return abs(components.day ?? 0) | |
} | |
} | |
"12 Dec 2018 22:15:00 CST".toDate() | |
"03 Feb 2020 12:10:45 CET".toDate() | |
"22 Aug 2005 10:17:20 GMT".toDate() | |
"Sunday 26/April/2020".toDate() | |
"Monday 06/April/2020".toDate() | |
"Friday 04/September /2020".toDate() | |
"6 (Mon) . 1 (Jan) .2020".toDate() | |
"20 (Wed) . 12 (Dec) .2006".toDate() | |
"20 (Wed) . 12 (Dec) .2006".toDate() | |
let dateString = "12 Dec 2018 2:15:00 CST" | |
if let date = dateString.toDate() { | |
print(date.day) // 12 | |
print(date.nameOfDay) // Wednesday | |
print(date.month) // 12 | |
print(date.nameOfTheMonth) // December | |
print(date.year) // 2018 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment