NCActivity.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // NCActivity.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/01/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. class NCActivity: UIViewController, UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  26. @IBOutlet weak var tableView: UITableView!
  27. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. var datasource = [tableActivity]()
  29. var sectionDate = [Date]()
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. // empty Data Source
  33. tableView.emptyDataSetDelegate = self;
  34. tableView.emptyDataSetSource = self;
  35. tableView.estimatedRowHeight = 120
  36. tableView.tableFooterView = UIView()
  37. self.title = NSLocalizedString("_activity_", comment: "")
  38. }
  39. override func viewWillAppear(_ animated: Bool) {
  40. super.viewWillAppear(animated)
  41. // Color
  42. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  43. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  44. loadDatasource()
  45. }
  46. // MARK: DZNEmpty
  47. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  48. return NCBrandColor.sharedInstance.backgroundView
  49. }
  50. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  51. return CCGraphics.changeThemingColorImage(UIImage.init(named: "activityNoRecord"), multiplier: 2, color: NCBrandColor.sharedInstance.graySoft)
  52. }
  53. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  54. let text = "\n"+NSLocalizedString("_no_activity_", comment: "")
  55. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  56. return NSAttributedString.init(string: text, attributes: attributes)
  57. }
  58. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  59. return true
  60. }
  61. func loadDatasource() {
  62. datasource = NCManageDatabase.sharedInstance.getActivity(predicate: NSPredicate(format: "account == %@", appDelegate.activeAccount))
  63. for tableActivity in datasource {
  64. guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: tableActivity.date as Date)) else {
  65. continue
  66. }
  67. if !sectionDate.contains(date) {
  68. sectionDate.append(date)
  69. }
  70. }
  71. tableView.reloadData()
  72. }
  73. func numberOfSections(in tableView: UITableView) -> Int {
  74. return sectionDate.count
  75. }
  76. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  77. return datasource.count
  78. }
  79. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  80. if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? activityTableViewCell {
  81. let tableActivity = datasource[indexPath.row]
  82. if tableActivity.icon.count > 0 {
  83. DispatchQueue.global().async {
  84. let encodedString = tableActivity.icon.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  85. if let data = try? Data(contentsOf: URL(string: encodedString!)!) {
  86. DispatchQueue.main.async {
  87. cell.icon.image = UIImage(data: data)
  88. }
  89. }
  90. }
  91. }
  92. return cell
  93. }
  94. return UITableViewCell()
  95. }
  96. }
  97. class activityTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  98. @IBOutlet weak var collectionView: UICollectionView!
  99. @IBOutlet weak var icon: UIImageView!
  100. var imageArray = [String] ()
  101. override func awakeFromNib() {
  102. super.awakeFromNib()
  103. collectionView.delegate = self
  104. collectionView.dataSource = self
  105. imageArray = ["1.jpeg","2.jpeg","3.jpeg","4.jpeg","5.jpeg","6.jpeg","7.jpeg","8.jpeg","9.jpeg","10.jpeg"]
  106. // Initialization code
  107. }
  108. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
  109. let size = CGSize(width: 120, height: 120)
  110. return size
  111. }
  112. func numberOfSections(in collectionView: UICollectionView) -> Int {
  113. return 1
  114. }
  115. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  116. return 10
  117. }
  118. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  119. if let cell: activityCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? activityCollectionViewCell {
  120. let randomNumber = Int(arc4random_uniform(UInt32(imageArray.count)))
  121. cell.imageView.image = UIImage(named: imageArray[randomNumber])
  122. return cell
  123. }
  124. return UICollectionViewCell()
  125. }
  126. }
  127. class activityCollectionViewCell: UICollectionViewCell {
  128. @IBOutlet weak var imageView: UIImageView!
  129. override func awakeFromNib() {
  130. super.awakeFromNib()
  131. }
  132. }