Skip to content

Instantly share code, notes, and snippets.

View PetreVane's full-sized avatar
🍃

Petre Vane PetreVane

🍃
View GitHub Profile
@PetreVane
PetreVane / Lazy Methods.swift
Last active January 24, 2020 17:20
Remember to mark your method as 'Private' so you don't call it by accident
// Lazy methods
class Person {
let name: String
init(name: String) {
self.name = name
@PetreVane
PetreVane / Lazy Closures.swift
Last active January 24, 2020 17:22
"Yes, you often need to explicitly declare the type. Yes, you need that = sign. And Yes, you need the parentheses after the closing brace."
// Lazy Closures
class Person {
let name: String
init(name: String) {
self.name = name
}
// Remember to add the closing brace '()' at the end of the closure declaration
@PetreVane
PetreVane / Converts Unix time in Date.swift
Created November 18, 2019 14:27
Decodes Unix time in Time that has a significance
func decodeDatefrom(_ unixTime: String) -> String? {
// converts comment date into meaningful date
var decodedDate: String?
if let unixTime = Double(unixTime) {
let dateFormatter = DateFormatter()
let date = Date(timeIntervalSince1970: unixTime)
@PetreVane
PetreVane / ResizeImage.swift
Last active January 24, 2020 17:23
Helper method which scales down the size of an image, before being uploaded to Firebase Storage
extension UIImage {
// Usage:
// let scaledImage = image.scale(newWidth: 960.0)
func reduceImageSize(to newWidth: CGFloat) -> UIImage {
if self.size.width == newWidth {
return self
}
// Calculates the scaling factor
@PetreVane
PetreVane / Fragment
Last active January 24, 2020 17:24
In order to do any kind of zooming, you need to implement a Scrollviewdelegate Protocol. This protocol contains a bunch of handy methods. The method we're after though, is viewForZooming (in: UIScrollView), which just sets the view to be zoomed. All you need to do is to return the view that you want to zoom from this method. If there are multip…
extension PhotoDetailsVC: UIScrollViewDelegate {
/// Returns the view that is expected to be zoomed
/// - Parameter scrollView: A view that allows the scrolling and zooming of its contained views.
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
@PetreVane
PetreVane / Retreive UserInfo from FileManager.swift
Last active January 24, 2020 17:25
Remember to save UserInfo to fileManager, before attempting to retrieve any data
func retrieveUserInfo() throws -> User {
let plistDecoder = PropertyListDecoder()
let filePath = FileManager.documentsDirectory
let file = filePath.appendingPathComponent("SavedUserData").appendingPathExtension("plist")
guard let retrievedData = try? Data(contentsOf: file) else { print("Errors: \(Failure.missingFile.errorDescription)"); throw Failure.missingFile }
guard let decodedUserData = try? plistDecoder.decode(User.self, from: retrievedData) else { print("Error: \(Failure.failedDecodingFile.errorDescription)"); throw Failure.failedDecodingFile}
// Decoding User Info
func decodeUserInfo(from data: Data) -> Result<User, Failure> {
let decoder = JSONDecoder()
let decodedData = try? decoder.decode(DecodedUserInfo.self, from: data)
guard let userData = decodedData?.person else { print("Errors while decoding UserData: \(Failure.failedDecodingFile.errorDescription)"); return .failure(.failedDecodingFile)}
let user = User(userID: userData.id, userName: userData.username.content, iconServer: userData.iconserver, iconFarm: userData.iconfarm)
_ = user.iconURL
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetailedImage" {
guard let indexPath = tableView.indexPathForSelectedRow,
let destinationVC = segue.destination as? DetailsVC else {return}
let imageToBePassed = UIImage(named: testImages[indexPath.row])
class ErrorManager {
static func showError(message: String, on viewController: UIViewController?, dismissAction: ((UIAlertAction) -> Void)? = nil) {
weak var currentViewController = viewController
DispatchQueue.main.async {
let alertController = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: dismissAction))
func fetchImageWithFileManager(image: String) -> UIImage? {
if let pathUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] {
let dirURL = pathUrl.appendingPathComponent(image)
do {
let imageData = try Data(contentsOf: dirURL)
return UIImage(data: imageData)
} catch {
print("Failed loading image")