-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for tagging images from web
- Loading branch information
Raghav Bhasin
authored and
Raghav Bhasin
committed
Aug 22, 2018
1 parent
189f543
commit 89e4911
Showing
12 changed files
with
272 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
21 changes: 21 additions & 0 deletions
21
ios App/Dracker/Assets.xcassets/NoImage.imageset/Contents.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "NoImage.jpg", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions
22
ios App/Dracker/Cell Models/Collection View/ImageLibraryCell.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import UIKit | ||
|
||
class ImageLibraryCell: BaseCollectionViewCell { | ||
|
||
let tag_image: UIImageView = { | ||
let image = UIImageView() | ||
image.image = #imageLiteral(resourceName: "NoImage") | ||
return image | ||
}() | ||
|
||
override func setup_cell() { | ||
super.setup_cell() | ||
//Add the imageView | ||
addSubview(tag_image) | ||
addConstraintsWithFormat(format: "H:|[v0]|", views: tag_image) | ||
addConstraintsWithFormat(format: "V:|[v0]|", views: tag_image) | ||
} | ||
|
||
func load_image_from_url(url: String) { | ||
tag_image.downloadImage(url: url) | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import UIKit | ||
|
||
class ImageSearch: UIView { | ||
|
||
var initialSearch: String = "" | ||
let ID = "ImageSearchCell" | ||
weak var parent: AddTransaction? | ||
weak var first_responder: UITextField? | ||
var list_of_images: [String] = [] | ||
|
||
lazy var navigation: UINavigationBar = { | ||
let bar = UINavigationBar() | ||
bar.isTranslucent = false | ||
bar.tintColor = .white | ||
let items = UINavigationItem() | ||
bar.setItems([items], animated: true) | ||
return bar | ||
}() | ||
|
||
lazy var search: UISearchController = { | ||
let search = UISearchController(searchResultsController: nil) | ||
search.hidesNavigationBarDuringPresentation = false | ||
search.definesPresentationContext = false | ||
search.searchBar.isTranslucent = true | ||
search.searchResultsUpdater = self | ||
search.searchBar.placeholder = "Tag Image..." | ||
search.delegate = self | ||
search.obscuresBackgroundDuringPresentation = false | ||
search.searchBar.keyboardType = .phonePad | ||
return search | ||
}() | ||
|
||
let status_bar: UIView = { | ||
let view = UIView() | ||
view.backgroundColor = .theme | ||
return view | ||
}() | ||
|
||
let search_view: UIView = { | ||
let view = UIView() | ||
return view | ||
}() | ||
|
||
lazy var imageLibrary: UICollectionView = { | ||
let library = UICollectionView(frame: frame, collectionViewLayout: UICollectionViewFlowLayout()) | ||
library.dataSource = self | ||
library.delegate = self | ||
library.backgroundColor = .white | ||
library.register(ImageLibraryCell.self, forCellWithReuseIdentifier: ID) | ||
library.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) | ||
library.showsHorizontalScrollIndicator = false | ||
library.showsVerticalScrollIndicator = false | ||
return library | ||
}() | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
setup() | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
fileprivate func setup() { | ||
backgroundColor = .white | ||
addSubview(navigation) | ||
addSubview(status_bar) | ||
addSubview(search_view) | ||
addSubview(imageLibrary) | ||
let height = UIApplication.shared.statusBarFrame.height | ||
addConstraintsWithFormat(format: "H:|[v0]|", views: navigation) | ||
addConstraintsWithFormat(format: "H:|[v0]|", views: search_view) | ||
addConstraintsWithFormat(format: "H:|[v0]|", views: imageLibrary) | ||
addConstraintsWithFormat(format: "H:|[v0]|", views: status_bar) | ||
addConstraintsWithFormat(format: "V:|-\(height)-[v0][v1(50)]-5-[v2]|", views: navigation, search_view ,imageLibrary) | ||
addConstraintsWithFormat(format: "V:|[v0(\(height))]", views: status_bar) | ||
search_view.addSubview(search.searchBar) | ||
search.searchBar.sizeToFit() | ||
} | ||
|
||
func render_view() { | ||
search.searchBar.text = initialSearch | ||
} | ||
} | ||
|
||
//MARK: Searching Delegates | ||
|
||
extension ImageSearch: UISearchResultsUpdating, UISearchControllerDelegate { | ||
func updateSearchResults(for searchController: UISearchController) { | ||
let term = searchController.searchBar.text! | ||
if term == "" { return } | ||
search_images_call(search_term: term) {[unowned self] (data) in | ||
if data.isFailure { | ||
return | ||
} | ||
let result = data.value as! [String: Any] | ||
if result["mesage"] != nil { | ||
return | ||
} | ||
let items = result["value"] as! [[String: Any]] | ||
for item in items { | ||
let url = item["contentUrl"] as! String | ||
self.list_of_images.append(url) | ||
} | ||
execute_on_main { | ||
self.imageLibrary.reloadData() | ||
} | ||
} | ||
} | ||
|
||
fileprivate func send_back() { | ||
search.dismiss(animated: true, completion: nil) | ||
UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { | ||
self.layer.transform = CATransform3DMakeTranslation(0, self.frame.height, 0) | ||
}){[unowned self] (_) in | ||
self.first_responder?.becomeFirstResponder() | ||
self.parent?.navigationController?.interactivePopGestureRecognizer?.isEnabled = true | ||
self.removeFromSuperview() | ||
} | ||
} | ||
|
||
} | ||
|
||
//MARK: Image Library Delegates | ||
extension ImageSearch: UICollectionViewDataSource, UICollectionViewDelegate { | ||
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | ||
let cell = imageLibrary.dequeueReusableCell(withReuseIdentifier: ID, for: indexPath) as! ImageLibraryCell | ||
cell.load_image_from_url(url: list_of_images[indexPath.row]) | ||
return cell | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | ||
return list_of_images.count | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | ||
let item = imageLibrary.cellForItem(at: indexPath) as! ImageLibraryCell | ||
let image = item.tag_image.image! | ||
parent?.image_url = image.get_temporary_path(quality: 0.50) | ||
parent?.navigation_title.text = "Add Transaction" | ||
parent?.set_options(state: .selected, type: .a_camera) | ||
send_back() | ||
} | ||
} | ||
|
||
|
||
//MARK: Collection View Flow Layout | ||
extension ImageSearch: UICollectionViewDelegateFlowLayout { | ||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | ||
return CGSize(width: 112, height: 112) | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { | ||
return 8 | ||
} | ||
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { | ||
return 8 | ||
} | ||
} |
Oops, something went wrong.