228 lines
9.7 KiB
Swift
Executable File
228 lines
9.7 KiB
Swift
Executable File
//
|
|
// Copyright (c) 2021 Related Code - https://relatedcode.com
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
import UIKit
|
|
|
|
//-----------------------------------------------------------------------------------------------------------------------------------------------
|
|
class Library2View: UIViewController {
|
|
|
|
@IBOutlet var viewHeader: UIView!
|
|
@IBOutlet var labelTitle: UILabel!
|
|
@IBOutlet var labelSubTitle: UILabel!
|
|
@IBOutlet var imageViewProfile: UIImageView!
|
|
@IBOutlet var segmentControl: UISegmentedControl!
|
|
@IBOutlet var collectionView: UICollectionView!
|
|
@IBOutlet var constraintCollectionViewHeight: NSLayoutConstraint!
|
|
|
|
private var navigationBackgroundImage: UIImage?
|
|
private var collections: [[String: String]] = []
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
navigationBackgroundImage = navigationController?.navigationBar.backgroundImage(for: .default)
|
|
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
|
|
|
|
segmentControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
|
|
segmentControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: AppColor.Theme], for: .normal)
|
|
|
|
collectionView.register(UINib(nibName: "Library2Cell", bundle: nil), forCellWithReuseIdentifier: "Library2Cell")
|
|
|
|
updateUI()
|
|
loadData()
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
constraintCollectionViewHeight.constant = collectionView.contentSize.height
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
|
|
navigationController?.navigationBar.setBackgroundImage(navigationBackgroundImage, for: .default)
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
updateUI()
|
|
}
|
|
|
|
// MARK: - Data methods
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func loadData() {
|
|
|
|
collections.removeAll()
|
|
|
|
var dict1: [String: String] = [:]
|
|
dict1["title"] = "Pop Music"
|
|
dict1["songs"] = "61 songs"
|
|
collections.append(dict1)
|
|
|
|
var dict2: [String: String] = [:]
|
|
dict2["title"] = "Recently Added"
|
|
dict2["songs"] = "5 songs"
|
|
collections.append(dict2)
|
|
|
|
var dict3: [String: String] = [:]
|
|
dict3["title"] = "Dance Hits"
|
|
dict3["songs"] = "78 songs"
|
|
collections.append(dict3)
|
|
|
|
collections.append(dict1)
|
|
collections.append(dict2)
|
|
collections.append(dict3)
|
|
|
|
refreshCollectionView()
|
|
}
|
|
|
|
// MARK: - Helper methods
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func updateUI() {
|
|
|
|
let background = UIColor.systemBackground.image(segmentControl.frame.size)
|
|
let selected = AppColor.Theme.image(segmentControl.frame.size)
|
|
|
|
segmentControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .selected)
|
|
segmentControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : AppColor.Theme], for: .normal)
|
|
segmentControl.setBackgroundImage(background, for: .normal, barMetrics: .default)
|
|
segmentControl.setBackgroundImage(selected, for: .selected, barMetrics: .default)
|
|
segmentControl.setDividerImage(AppColor.Theme.image(), forLeftSegmentState: .normal, rightSegmentState: [.normal, .highlighted, .selected], barMetrics: .default)
|
|
segmentControl.layer.borderWidth = 1
|
|
segmentControl.layer.borderColor = AppColor.Theme.cgColor
|
|
}
|
|
|
|
// MARK: - User actions
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
@IBAction func actionAdd(_ sender: Any) {
|
|
|
|
print(#function)
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
@IBAction func actionSegment(_ sender: Any) {
|
|
|
|
print(#function)
|
|
}
|
|
|
|
// MARK: - Refresh methods
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func refreshCollectionView() {
|
|
|
|
collectionView.reloadData()
|
|
}
|
|
}
|
|
|
|
// MARK: - UIScrollViewDelegate
|
|
//-----------------------------------------------------------------------------------------------------------------------------------------------
|
|
extension Library2View: UIScrollViewDelegate {
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
|
if scrollView.contentOffset.y > viewHeader.frame.origin.y {
|
|
navigationController?.navigationBar.setBackgroundImage(navigationBackgroundImage, for: .default)
|
|
title = labelTitle.text
|
|
}
|
|
else {
|
|
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
|
|
title = ""
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK:- UICollectionViewDataSource
|
|
//-----------------------------------------------------------------------------------------------------------------------------------------------
|
|
extension Library2View: UICollectionViewDataSource {
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
|
|
|
return 1
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
|
return collections.count
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Library2Cell", for: indexPath) as! Library2Cell
|
|
cell.bindData(index: indexPath.item, data: collections[indexPath.row])
|
|
return cell
|
|
}
|
|
}
|
|
|
|
// MARK:- UICollectionViewDelegate
|
|
//-----------------------------------------------------------------------------------------------------------------------------------------------
|
|
extension Library2View: UICollectionViewDelegate {
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
print("didSelectItemAt \(indexPath.row)")
|
|
}
|
|
}
|
|
|
|
// MARK:- UICollectionViewDelegateFlowLayout
|
|
//-----------------------------------------------------------------------------------------------------------------------------------------------
|
|
extension Library2View: UICollectionViewDelegateFlowLayout {
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
|
|
|
let height = view.frame.size.height
|
|
let width = collectionView.frame.size.width
|
|
|
|
return CGSize(width: (width - 40)/2, height: height / 3.2)
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
|
|
|
return UIEdgeInsets(top: 0, left: 15, bottom: 15, right: 15)
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
|
|
|
return 10
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
|
|
|
return 10
|
|
}
|
|
}
|
|
|
|
// MARK: - UIColor
|
|
//-----------------------------------------------------------------------------------------------------------------------------------------------
|
|
fileprivate extension UIColor {
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
|
func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
|
|
|
|
return UIGraphicsImageRenderer(size: size).image { rendererContext in
|
|
setFill()
|
|
rendererContext.fill(CGRect(origin: .zero, size: size))
|
|
}
|
|
}
|
|
}
|