NCActivity.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // MARK: TableView
  74. func numberOfSections(in tableView: UITableView) -> Int {
  75. return sectionDate.count
  76. }
  77. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  78. let startDate = sectionDate[section]
  79. let endDate: Date = {
  80. let components = DateComponents(day: 1, second: -1)
  81. return Calendar.current.date(byAdding: components, to: startDate)!
  82. }()
  83. let results = NCManageDatabase.sharedInstance.getActivity(predicate: NSPredicate(format: "account == %@ && date BETWEEN %@", appDelegate.activeAccount, [startDate, endDate]))
  84. return results.count
  85. }
  86. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  87. return 50
  88. }
  89. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  90. let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 30))
  91. view.backgroundColor = UIColor.white
  92. let label = UILabel(frame: CGRect(x: 35, y: 0, width: tableView.bounds.width - 35, height: 30))
  93. label.font = UIFont.boldSystemFont(ofSize: 18)
  94. label.textColor = UIColor.black
  95. label.text = CCUtility.getTitleSectionDate(sectionDate[section])
  96. view.addSubview(label)
  97. return view
  98. }
  99. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  100. return 100
  101. }
  102. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  103. return UITableView.automaticDimension
  104. }
  105. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  106. if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? activityTableViewCell {
  107. let tableActivity = datasource[indexPath.row]
  108. if tableActivity.icon.count > 0 {
  109. DispatchQueue.global().async {
  110. let encodedString = tableActivity.icon.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  111. if let data = try? Data(contentsOf: URL(string: encodedString!)!) {
  112. DispatchQueue.main.async {
  113. cell.icon.image = UIImage(data: data)
  114. }
  115. }
  116. }
  117. }
  118. if tableActivity.subjectRich.count > 0 {
  119. var subject = tableActivity.subjectRich
  120. let keys = subject.keyTags()
  121. for key in keys {
  122. if let result = NCManageDatabase.sharedInstance.getActivitySubjectRich(account: appDelegate.activeAccount, idActivity: tableActivity.idActivity, key: key) {
  123. subject = subject.replacingOccurrences(of: "{\(key)}", with: result.name)
  124. }
  125. }
  126. cell.subject.text = subject
  127. }
  128. return cell
  129. }
  130. return UITableViewCell()
  131. }
  132. }
  133. class activityTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  134. @IBOutlet weak var collectionView: UICollectionView!
  135. @IBOutlet weak var icon: UIImageView!
  136. @IBOutlet weak var subject: UILabel!
  137. var imageArray = [String] ()
  138. override func awakeFromNib() {
  139. super.awakeFromNib()
  140. collectionView.delegate = self
  141. collectionView.dataSource = self
  142. imageArray = ["1.jpeg","2.jpeg","3.jpeg","4.jpeg","5.jpeg","6.jpeg","7.jpeg","8.jpeg","9.jpeg","10.jpeg"]
  143. // Initialization code
  144. }
  145. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
  146. let size = CGSize(width: 120, height: 120)
  147. return size
  148. }
  149. func numberOfSections(in collectionView: UICollectionView) -> Int {
  150. return 1
  151. }
  152. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  153. return 10
  154. }
  155. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  156. if let cell: activityCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? activityCollectionViewCell {
  157. let randomNumber = Int(arc4random_uniform(UInt32(imageArray.count)))
  158. cell.imageView.image = UIImage(named: imageArray[randomNumber])
  159. return cell
  160. }
  161. return UICollectionViewCell()
  162. }
  163. }
  164. class activityCollectionViewCell: UICollectionViewCell {
  165. @IBOutlet weak var imageView: UIImageView!
  166. override func awakeFromNib() {
  167. super.awakeFromNib()
  168. }
  169. }
  170. // MARK: Extension
  171. extension String
  172. {
  173. func keyTags() -> [String] {
  174. if let regex = try? NSRegularExpression(pattern: "\\{[a-z0-9]+\\}", options: .caseInsensitive) {
  175. let string = self as NSString
  176. return regex.matches(in: self, options: [], range: NSRange(location: 0, length: string.length)).map {
  177. string.substring(with: $0.range).replacingOccurrences(of: "[\\{\\}]", with: "", options: .regularExpression)
  178. }
  179. }
  180. return []
  181. }
  182. }