Skip to content

Commit

Permalink
Make Topics code a little prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemeierj authored and noahtallen committed Feb 2, 2019
1 parent 1e003ad commit 1baaf5b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
8 changes: 0 additions & 8 deletions Buddies.xcodeproj/xcshareddata/xcschemes/Buddies.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@
BlueprintName = "BuddiesTests"
ReferencedContainer = "container:Buddies.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "UIApplicationTests/testSetRootView()">
</Test>
<Test
Identifier = "UIViewControlTests/testSetupHideKeyboardOnTap()">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
<MacroExpansion>
Expand Down
10 changes: 6 additions & 4 deletions Buddies/Topics/Collections/TopicCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ class TopicCollection: NSObject {
print("Cannot get Image URL for \(snap.documentID)")
continue
}

let onDownload: (URL)->Void = { destURL in

let _ = StorageManager.downloadFile(
for: firebaseImageURL,
to: snap.documentID,
session: nil
) { destURL in
OperationQueue.main.addOperation {
self.updateImage(with: destURL, for: snap.documentID)
}
}

let _ = StorageManager.downloadFile(for: firebaseImageURL, to: snap.documentID, session: nil, callback: onDownload)

}

Expand Down
17 changes: 6 additions & 11 deletions Buddies/Topics/Layouts/TopicLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import UIKit
class TopicLayout: UICollectionViewLayout {

let topicWidth = CGFloat(150);

let heightToWidthRatio = 0.9
var cellPadding: CGFloat = 6

var topicElementWidth: CGFloat {
get {
Expand All @@ -50,7 +50,6 @@ class TopicLayout: UICollectionViewLayout {
}
return offsets
}

}

var columnWidth: CGFloat {
Expand All @@ -64,8 +63,6 @@ class TopicLayout: UICollectionViewLayout {
return Int(floor(contentWidth / topicElementWidth))
}
}
var cellPadding: CGFloat = 6


override func invalidateLayout() {
cache = []
Expand All @@ -74,11 +71,9 @@ class TopicLayout: UICollectionViewLayout {

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
guard let collectionView = collectionView else { return false }

return newBounds.width != collectionView.bounds.width
}


fileprivate var cache = [UICollectionViewLayoutAttributes]()

//Content height and size
Expand All @@ -95,10 +90,8 @@ class TopicLayout: UICollectionViewLayout {
override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}


override func prepare() {
// 1. Only calculate once
guard cache.isEmpty == true, let collectionView = collectionView else {
return
}
Expand All @@ -111,11 +104,14 @@ class TopicLayout: UICollectionViewLayout {

let indexPath = IndexPath(item: item, section: 0)


let photoHeight = topicWidth*CGFloat(heightToWidthRatio)
let height = cellPadding * 2 + photoHeight

let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height)
let frame = CGRect(
x: xOffset[column], y: yOffset[column],
width: columnWidth, height: height
)

let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)

// 5. Creates an UICollectionViewLayoutItem with the frame and add it to the cache
Expand All @@ -141,7 +137,6 @@ class TopicLayout: UICollectionViewLayout {
visibleLayoutAttributes.append(attributes)
}
}

return visibleLayoutAttributes
}

Expand Down
7 changes: 6 additions & 1 deletion Buddies/Topics/View Controllers/TopicViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ class TopicViewController: UICollectionViewController, TopicCollectionDelegate {
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TopicCell", for: indexPath)
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: "TopicCell",
for: indexPath
)

if let topicCell = cell as? TopicCell {
topicCell.topic = topicCollection.topics[indexPath.item]
}

return cell
}
}
Expand Down
7 changes: 3 additions & 4 deletions Buddies/Utilities/FirestoreManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ class FirestoreManager {
static func loadAllDocuments(ofType type: String,
_ callback: @escaping ([DocumentSnapshot]) -> ()) {
db.collection(type).getDocuments { result, error in
guard let result = result, error == nil else {
if let error = error {
print("Error loading \(type) from Firestore: \n \(error)")
}
guard let result = result,
error == nil else {
print("Error loading \(type) from Firestore: \n \(String(describing: error))")
callback([])
return
}
Expand Down
39 changes: 22 additions & 17 deletions Buddies/Utilities/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class StorageManager {

let url = URL(string: path)

let session: URLSession = providedSession ?? URLSession(configuration: URLSessionConfiguration.default)
let session: URLSession = providedSession
?? URLSession(configuration: URLSessionConfiguration.default)

let request = URLRequest(url:url!)

Expand All @@ -31,7 +32,11 @@ class StorageManager {

let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
StorageManager.persistDownload(temp: tempLocalUrl, dest: localDestURL, callback: callback)
StorageManager.persistDownload(
temp: tempLocalUrl,
dest: localDestURL,
callback: callback
)
} else {
print("Error took place while downloading a file. Error description: \(String(describing: error?.localizedDescription))");
}
Expand All @@ -51,24 +56,24 @@ class StorageManager {
}

static func localURL(for path: String) -> URL? {
guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
print("Couldn't create directory.")
return nil
}
guard let url = directory.appendingPathComponent(path) else {
print("Couldn't create file URL.")
return nil
}

return url
do {
let directory = try FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false
) as NSURL
return directory.appendingPathComponent(path)
} catch { return nil }
}

static func getSavedImage(filename: String) -> UIImage? {
if let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
let dirUrl = URL(fileURLWithPath: dir.absoluteString)
let fileUrl = dirUrl.appendingPathComponent(filename).path
return UIImage(contentsOfFile: fileUrl)
} else {
do {
let fileURL = localURL(for: filename)
let imageData = try Data(contentsOf: fileURL!)
return UIImage(data: imageData)
} catch {
return nil
}
}
Expand Down

0 comments on commit 1baaf5b

Please sign in to comment.