From 1a72bd0d141c46f1bc3c0620d6ca7daaf7fa2ce5 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Mon, 23 Jan 2017 17:08:00 -0500 Subject: [PATCH] Library collection view basic function --- .../Bookmarks/Dots.imageset/more-2.png | Bin 479 -> 0 bytes .../Dots.imageset/Contents.json | 2 +- .../Dots.imageset/rsz_1more-2.png | Bin 0 -> 1637 bytes .../BookmarkCollectionController.swift | 2 +- .../Library/LibraryBooksController.swift | 151 +++++++++++++++++- Kiwix-iOS/Storyboard/Library.storyboard | 110 ++++++++++++- Kiwix-iOS/View/Cells.swift | 31 +++- 7 files changed, 283 insertions(+), 13 deletions(-) delete mode 100644 Kiwix-iOS/Assets.xcassets/Bookmarks/Dots.imageset/more-2.png rename Kiwix-iOS/Assets.xcassets/{Bookmarks => }/Dots.imageset/Contents.json (87%) create mode 100644 Kiwix-iOS/Assets.xcassets/Dots.imageset/rsz_1more-2.png diff --git a/Kiwix-iOS/Assets.xcassets/Bookmarks/Dots.imageset/more-2.png b/Kiwix-iOS/Assets.xcassets/Bookmarks/Dots.imageset/more-2.png deleted file mode 100644 index 106baee98e31710d659a8aaa383c7e084d550507..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 479 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I3?%1nZ+ru!n2Vh}LpV4%Za?&Y0OWEOctjR6 zFfi^0VMePrj~@XAB}-f*N`mv#O3D+9QW?t2%k?tzvWt@w3sUv+i_&MmvylQSN(=A_ zasB`Qzj4}shpboDWv`vHKRM*SwaR{Pm-osh@3l?-E8E=HHaV~Dv)|h0ytK=E0~EB) zd2I(4w9R>ClMCd&vCVr8lme1K#kRRXEpNaK2p3B3nKeHYXrp3DkY6wZD-W-*jJ&F` zjlF|waC}-uaeZ6Qr0J_RZ{Kg~mcv@^ z*^@7Lzvh0iy<_W!Yn$iFf4b#YmdKI;Vst09-!LGynhq diff --git a/Kiwix-iOS/Assets.xcassets/Bookmarks/Dots.imageset/Contents.json b/Kiwix-iOS/Assets.xcassets/Dots.imageset/Contents.json similarity index 87% rename from Kiwix-iOS/Assets.xcassets/Bookmarks/Dots.imageset/Contents.json rename to Kiwix-iOS/Assets.xcassets/Dots.imageset/Contents.json index 1dd8ef56..6c053b83 100644 --- a/Kiwix-iOS/Assets.xcassets/Bookmarks/Dots.imageset/Contents.json +++ b/Kiwix-iOS/Assets.xcassets/Dots.imageset/Contents.json @@ -10,7 +10,7 @@ }, { "idiom" : "universal", - "filename" : "more-2.png", + "filename" : "rsz_1more-2.png", "scale" : "3x" } ], diff --git a/Kiwix-iOS/Assets.xcassets/Dots.imageset/rsz_1more-2.png b/Kiwix-iOS/Assets.xcassets/Dots.imageset/rsz_1more-2.png new file mode 100644 index 0000000000000000000000000000000000000000..1962d59a3ff4df037c4ac1eabff0301194b1ef87 GIT binary patch literal 1637 zcmbVLYfuwc6uwEo2TDajUPGtV0f(6ihKCBfd4L9ikq8DXG8zRT!Ipz* zb9c*<*2KEH__`p3T;t;uD)3_dogo67erIMYIH&ArWi&!9AI-97IK%qQ%s7=2p?4M` zR8)-6C_qKM2$?|0q$5OBA>@@?^W$0xLc+qNgp?Q{glRc>Zg*6kP3DQ^N=$CYgnl4o zD;KjGZ$ONc!csk!8gLXfDAQvJg(LM?VZ?w(=CRUTOwhfh43ZV#2oo#U_eC29LN$ZZ zd<-B-^R$S3+m2bBl;)WVZNH3Wod5>U3>W3t-G_W!Q235GuE-}E8k>p?* za11Q$0>Xn7mg})nfr;l^I9iV(=?cS$jK(m?yr)ntj7S=0W~B0V|_fSx&En>fhZ7Cy zn~D$M)cju)I0yrc1u}oG7b=`Y;MU+eB5rVCd1TU3d~cuM<12(O4ajkvF2(O8e0;wc zRcn&L|IbGt6u_4(1T`4m3nmu)A0hxF5Rj(2@>CBXAwghvwMS}soyqf4j&pQI^JFdK zi0*W7qPnrMar-D6E5ALuZ=Jh->5rZ5`@I(8vpuYhdGLU-UF>n1d)IW|{B&?+sIAS`mgP41 zX#R-%kk@N7JZ8E~>nPn*T3Yf!jABp8?vlNwMT%YTD}u>Nd)=9YUl zcmH_-l?~qB6<;T-G+kX`p$~d~`rIELnlsNF%AyCZDjxPD1+bhs?&- z!r^OPizO#|uiAcX^t7h^5M^;lS_7I3v|oK`+V-G4`AVa;=KQe{_IUDzL@Uv{rMvl> zuipFm(JLEw&}W+VH5P3>bMo;Q_0L%X(?ZA#wIw&~`f zR!jRO^{rD&`qO65OZ%zEw#lj{&4wGdwr60^kAFMY*|U6s{^su0XJlEf{$yEJVx`|< z|5!hN9V?Ft)cF+$xK_Bo(a~|>{_Jp)Eqt{gvEiFpAn@TcB Int { + return fetchedResultController.sections?.count ?? 0 + } + + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return fetchedResultController.sections?[section].numberOfObjects ?? 0 + } + + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! LibraryCollectionCell + + let book = fetchedResultController.object(at: indexPath) + cell.delegate = self + cell.imageView.image = UIImage(data: book.favIcon ?? Data()) + cell.titleLabel.text = book.title + cell.subtitleLabel.text = [ + book.dateDescription, + book.fileSizeDescription, + book.articleCountDescription + ].flatMap({$0}).joined(separator: " ") + cell.descriptionLabel.text = book.desc + cell.hasPicLabel.isHidden = book.hasPic + + return cell + } + + func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { + let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! LibraryCollectionHeader + header.textLabel.text = fetchedResultController.sections?[indexPath.section].name + return header + } + + // MARK: - UICollectionViewDelegateFlowLayout + + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + return CGSize(width: itemWidth, height: 66) + } + + // MARK: - LibraryCollectionCellDelegate + + func didTapMoreButton(cell: LibraryCollectionCell) { + guard let indexPath = collectionView.indexPath(for: cell) else {return} + print("\(indexPath) tapped") + + } + + // MARK: - NSFetchedResultsController + + let managedObjectContext = AppDelegate.persistentContainer.viewContext + lazy var fetchedResultController: NSFetchedResultsController = { + let fetchRequest = Book.fetchRequest() + let langDescriptor = NSSortDescriptor(key: "language.name", ascending: true) + let titleDescriptor = NSSortDescriptor(key: "title", ascending: true) + fetchRequest.sortDescriptors = [langDescriptor, titleDescriptor] + fetchRequest.predicate = NSPredicate(format: "language.name != nil") +//// fetchRequest.predicate = self.onlineCompoundPredicate + + let controller = NSFetchedResultsController(fetchRequest: fetchRequest, + managedObjectContext: self.managedObjectContext, + sectionNameKeyPath: "language.name", cacheName: nil) + controller.delegate = self + try? controller.performFetch() + return controller as! NSFetchedResultsController + }() } diff --git a/Kiwix-iOS/Storyboard/Library.storyboard b/Kiwix-iOS/Storyboard/Library.storyboard index 047a87cf..e442bc72 100644 --- a/Kiwix-iOS/Storyboard/Library.storyboard +++ b/Kiwix-iOS/Storyboard/Library.storyboard @@ -6,6 +6,7 @@ + @@ -44,22 +45,118 @@ - + - + - - + + - + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -144,5 +241,6 @@ + diff --git a/Kiwix-iOS/View/Cells.swift b/Kiwix-iOS/View/Cells.swift index 3e003733..4ecbcf13 100644 --- a/Kiwix-iOS/View/Cells.swift +++ b/Kiwix-iOS/View/Cells.swift @@ -101,8 +101,6 @@ class ArticleSnippetCell: ArticleCell { @IBOutlet weak var snippetLabel: UILabel! } -// MARK: - Bookmark Cell - class BookmarkCollectionCell: UICollectionViewCell { override func awakeFromNib() { clipsToBounds = false @@ -144,6 +142,35 @@ class BookmarkCollectionCell: UICollectionViewCell { } } +class LibraryCollectionCell: UICollectionViewCell { + override func awakeFromNib() { + imageView.clipsToBounds = true + imageView.layer.cornerRadius = 2.0 + hasPicLabel.layer.borderWidth = 1.5 + hasPicLabel.layer.borderColor = UIColor.orange.cgColor + hasPicLabel.layer.cornerRadius = 8.0 + } + + @IBAction func moreButtonTapped(_ sender: UIButton) { + delegate?.didTapMoreButton(cell: self) + } + + weak var delegate: LibraryCollectionCellDelegate? + @IBOutlet weak var imageView: UIImageView! + @IBOutlet weak var titleLabel: UILabel! + @IBOutlet weak var subtitleLabel: UILabel! + @IBOutlet weak var descriptionLabel: UILabel! + @IBOutlet weak var hasPicLabel: UILabel! +} + +protocol LibraryCollectionCellDelegate: class { + func didTapMoreButton(cell: LibraryCollectionCell) +} + +class LibraryCollectionHeader: UICollectionReusableView { + @IBOutlet weak var textLabel: UILabel! +} +