1- // SPDX-FileCopyrightText: Nextcloud GmbH
2- // SPDX-FileCopyrightText: 2019 Marino Faggiana
3- // SPDX-License-Identifier: GPL-3.0-or-later
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+ // Author Henrik Storch <henrik.storch@nextcloud.com>
10+ //
11+ // This program is free software: you can redistribute it and/or modify
12+ // it under the terms of the GNU General Public License as published by
13+ // the Free Software Foundation, either version 3 of the License, or
14+ // (at your option) any later version.
15+ //
16+ // This program is distributed in the hope that it will be useful,
17+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
18+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+ // GNU General Public License for more details.
20+ //
21+ // You should have received a copy of the GNU General Public License
22+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
23+ //
424
525import UIKit
626import SwiftRichString
@@ -21,6 +41,7 @@ class NCActivity: UIViewController, NCSharePagingContent {
2141 let database = NCManageDatabase . shared
2242 var allItems : [ DateCompareable ] = [ ]
2343 var sectionDates : [ Date ] = [ ]
44+ var dataSourceTask : URLSessionTask ?
2445
2546 var insets = UIEdgeInsets ( top: 8 , left: 0 , bottom: 0 , right: 0 )
2647 var didSelectItemEnable : Bool = true
@@ -29,14 +50,11 @@ class NCActivity: UIViewController, NCSharePagingContent {
2950
3051 var isFetchingActivity = false
3152 var hasActivityToLoad = true {
32- didSet {
33- tableView. tableFooterView? . isHidden = hasActivityToLoad
34- }
53+ didSet { tableView. tableFooterView? . isHidden = hasActivityToLoad }
3554 }
3655 var dateAutomaticFetch : Date ?
3756 private let appDelegate = ( UIApplication . shared. delegate as? AppDelegate ) !
3857
39- @MainActor
4058 var session : NCSession . Session {
4159 if account. isEmpty {
4260 NCSession . shared. getSession ( controller: tabBarController)
@@ -55,7 +73,7 @@ class NCActivity: UIViewController, NCSharePagingContent {
5573 override func viewDidLoad( ) {
5674 super. viewDidLoad ( )
5775
58- navigationController? . setNavigationBarAppearance ( )
76+ self . navigationController? . navigationBar . prefersLargeTitles = true
5977 view. backgroundColor = . systemBackground
6078 self . title = NSLocalizedString ( " _activity_ " , comment: " " )
6179
@@ -76,14 +94,7 @@ class NCActivity: UIViewController, NCSharePagingContent {
7694 commentView = Bundle . main. loadNibNamed ( " NCActivityCommentView " , owner: self , options: nil ) ? . first as? NCActivityCommentView
7795 commentView? . setup ( account: metadata. account) { newComment in
7896 guard let newComment = newComment, !newComment. isEmpty, let metadata = self . metadata else { return }
79- NextcloudKit . shared. putComments ( fileId: metadata. fileId, message: newComment, account: metadata. account) { task in
80- Task {
81- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: metadata. account,
82- path: metadata. fileId,
83- name: " putComments " )
84- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
85- }
86- } completion: { _, _, error in
97+ NextcloudKit . shared. putComments ( fileId: metadata. fileId, message: newComment, account: metadata. account) { _, _, error in
8798 if error == . success {
8899 self . commentView? . newCommentField. text? . removeAll ( )
89100 self . loadComments ( )
@@ -109,12 +120,9 @@ class NCActivity: UIViewController, NCSharePagingContent {
109120 override func viewWillDisappear( _ animated: Bool ) {
110121 super. viewWillDisappear ( animated)
111122
112- Task {
113- await NCNetworking . shared. networkingTasks. cancel ( identifier: " NCActivity " )
114- }
115-
116123 // Cancel Queue & Retrieves Properties
117124 NCNetworking . shared. downloadThumbnailActivityQueue. cancelAll ( )
125+ dataSourceTask? . cancel ( )
118126 }
119127
120128 override func viewWillLayoutSubviews( ) {
@@ -234,8 +242,8 @@ extension NCActivity: UITableViewDataSource {
234242 cell. avatarImage? . image = results. image
235243 }
236244
237- if let tblAvatar = results. tblAvatar ,
238- !tblAvatar . loaded,
245+ if let tableAvatar = results. tableAvatar ,
246+ !tableAvatar . loaded,
239247 NCNetworking . shared. downloadAvatarQueue. operations. filter ( { ( $0 as? NCOperationDownloadAvatar ) ? . fileName == fileName } ) . isEmpty {
240248 NCNetworking . shared. downloadAvatarQueue. addOperation ( NCOperationDownloadAvatar ( user: comment. actorId, fileName: fileName, account: account, view: tableView) )
241249 }
@@ -307,7 +315,7 @@ extension NCActivity: UITableViewDataSource {
307315 cell. avatar? . image = results. image
308316 }
309317
310- if !( results. tblAvatar ? . loaded ?? false ) ,
318+ if !( results. tableAvatar ? . loaded ?? false ) ,
311319 NCNetworking . shared. downloadAvatarQueue. operations. filter ( { ( $0 as? NCOperationDownloadAvatar ) ? . fileName == fileName } ) . isEmpty {
312320 NCNetworking . shared. downloadAvatarQueue. addOperation ( NCOperationDownloadAvatar ( user: activity. user, fileName: fileName, account: session. account, view: tableView) )
313321 }
@@ -331,7 +339,7 @@ extension NCActivity: UITableViewDataSource {
331339 for key in keys {
332340 if let result = database. getActivitySubjectRich ( account: session. account, idActivity: activity. idActivity, key: key) {
333341 orderKeysId. append ( result. id)
334- subject = subject. replacingOccurrences ( of: " { \( key) } " , with: " <bold> " + result. name. escapedForMarkup + " </bold> " )
342+ subject = subject. replacingOccurrences ( of: " { \( key) } " , with: " <bold> " + result. name + " </bold> " )
335343 }
336344 }
337345
@@ -370,8 +378,12 @@ extension NCActivity {
370378 func fetchAll( isInitial: Bool ) {
371379 guard !isFetchingActivity else { return }
372380 self . isFetchingActivity = true
381+ var bottom : CGFloat = 0
373382
374- NCActivityIndicator . shared. start ( backgroundView: self . view, style: . medium)
383+ if let mainTabBar = self . tabBarController? . tabBar as? NCMainTabBar {
384+ bottom = - mainTabBar. getHeight ( )
385+ }
386+ NCActivityIndicator . shared. start ( backgroundView: self . view, bottom: bottom - 35 , style: . medium)
375387
376388 let dispatchGroup = DispatchGroup ( )
377389 loadComments ( disptachGroup: dispatchGroup)
@@ -418,14 +430,7 @@ extension NCActivity {
418430 guard showComments, let metadata = metadata else { return }
419431 disptachGroup? . enter ( )
420432
421- NextcloudKit . shared. getComments ( fileId: metadata. fileId, account: metadata. account) { task in
422- Task {
423- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: metadata. account,
424- path: metadata. fileId,
425- name: " getComments " )
426- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
427- }
428- } completion: { _, comments, _, error in
433+ NextcloudKit . shared. getComments ( fileId: metadata. fileId, account: metadata. account) { _, comments, _, error in
429434 if error == . success, let comments = comments {
430435 self . database. addComments ( comments, account: metadata. account, objectId: metadata. fileId)
431436 } else if error. errorCode != NCGlobal . shared. errorResourceNotFound {
@@ -446,29 +451,21 @@ extension NCActivity {
446451
447452 /// Check if most recent activivities are loaded, if not trigger reload
448453 func checkRecentActivity( disptachGroup: DispatchGroup ) {
449- Task {
450- // If is already in-flight, do nothing
451- if await NCNetworking . shared. networkingTasks. isReading ( identifier: " NCActivity " ) {
452- return
453- }
454- }
455-
456454 guard let result = database. getLatestActivityId ( account: session. account) , metadata == nil , hasActivityToLoad else {
457455 return self . loadActivity ( idActivity: 0 , disptachGroup: disptachGroup)
458456 }
459457 let resultActivityId = max ( result. activityFirstKnown, result. activityLastGiven)
460458
461459 disptachGroup. enter ( )
462460
463- NextcloudKit . shared. getActivity ( since: 0 ,
464- limit: 1 ,
465- objectId: nil ,
466- objectType: objectType,
467- previews: true ,
468- account: session. account) { task in
469- Task {
470- await NCNetworking . shared. networkingTasks. track ( identifier: " NCActivity " , task: task)
471- }
461+ NextcloudKit . shared. getActivity (
462+ since: 0 ,
463+ limit: 1 ,
464+ objectId: nil ,
465+ objectType: objectType,
466+ previews: true ,
467+ account: session. account) { task in
468+ self . dataSourceTask = task
472469 } completion: { account, _, activityFirstKnown, activityLastGiven, _, error in
473470 defer { disptachGroup. leave ( ) }
474471
@@ -486,21 +483,18 @@ extension NCActivity {
486483 }
487484
488485 func loadActivity( idActivity: Int , limit: Int = 200 , disptachGroup: DispatchGroup ) {
489- guard hasActivityToLoad else {
490- return
491- }
486+ guard hasActivityToLoad else { return }
492487 var resultActivityId = 0
493488
494489 disptachGroup. enter ( )
495- NextcloudKit . shared. getActivity ( since: idActivity,
496- limit: min ( limit, 200 ) ,
497- objectId: metadata? . fileId,
498- objectType: objectType,
499- previews: true ,
500- account: session. account) { task in
501- Task {
502- await NCNetworking . shared. networkingTasks. track ( identifier: " NCActivity " , task: task)
503- }
490+ NextcloudKit . shared. getActivity (
491+ since: idActivity,
492+ limit: min ( limit, 200 ) ,
493+ objectId: metadata? . fileId,
494+ objectType: objectType,
495+ previews: true ,
496+ account: session. account) { task in
497+ self . dataSourceTask = task
504498 } completion: { account, activities, activityFirstKnown, activityLastGiven, _, error in
505499 defer { disptachGroup. leave ( ) }
506500 guard error == . success,
@@ -544,26 +538,22 @@ extension NCActivity: NCShareCommentsCellDelegate {
544538 guard let tableComment = tableComment else {
545539 return
546540 }
547- self . showProfileMenu ( userId: tableComment. actorId, session: session, sender : sender )
541+ self . showProfileMenu ( userId: tableComment. actorId, session: session)
548542 }
549543
550544 func tapMenu( with tableComments: tableComments ? , sender: Any ) {
551- toggleMenu ( with: tableComments, sender : sender )
545+ toggleMenu ( with: tableComments)
552546 }
553547
554- func toggleMenu( with tableComments: tableComments ? , sender : Any ) {
548+ func toggleMenu( with tableComments: tableComments ? ) {
555549 var actions = [ NCMenuAction] ( )
556550
557551 actions. append (
558552 NCMenuAction (
559553 title: NSLocalizedString ( " _edit_comment_ " , comment: " " ) ,
560554 icon: utility. loadImage ( named: " pencil " , colors: [ NCBrandColor . shared. iconImageColor] ) ,
561- sender: sender,
562555 action: { _ in
563- guard let metadata = self . metadata,
564- let tableComments = tableComments else {
565- return
566- }
556+ guard let metadata = self . metadata, let tableComments = tableComments else { return }
567557
568558 let alert = UIAlertController ( title: NSLocalizedString ( " _edit_comment_ " , comment: " " ) , message: nil , preferredStyle: . alert)
569559 alert. addAction ( UIAlertAction ( title: NSLocalizedString ( " _cancel_ " , comment: " " ) , style: . cancel, handler: nil ) )
@@ -575,14 +565,7 @@ extension NCActivity: NCShareCommentsCellDelegate {
575565 alert. addAction ( UIAlertAction ( title: NSLocalizedString ( " _ok_ " , comment: " " ) , style: . default, handler: { _ in
576566 guard let message = alert. textFields? . first? . text, !message. isEmpty else { return }
577567
578- NextcloudKit . shared. updateComments ( fileId: metadata. fileId, messageId: tableComments. messageId, message: message, account: metadata. account) { task in
579- Task {
580- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: metadata. account,
581- path: metadata. fileId,
582- name: " updateComments " )
583- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
584- }
585- } completion: { _, _, error in
568+ NextcloudKit . shared. updateComments ( fileId: metadata. fileId, messageId: tableComments. messageId, message: message, account: metadata. account) { _, _, error in
586569 if error == . success {
587570 self . loadComments ( )
588571 } else {
@@ -604,19 +587,11 @@ extension NCActivity: NCShareCommentsCellDelegate {
604587 NCMenuAction (
605588 title: NSLocalizedString ( " _delete_comment_ " , comment: " " ) ,
606589 destructive: true ,
607- icon: utility. loadImage ( named: " trashIcon " , colors: [ . red] ) ,
608- sender: sender,
590+ icon: utility. loadImage ( named: " trash " , colors: [ . red] ) ,
609591 action: { _ in
610592 guard let metadata = self . metadata, let tableComments = tableComments else { return }
611593
612- NextcloudKit . shared. deleteComments ( fileId: metadata. fileId, messageId: tableComments. messageId, account: metadata. account) { task in
613- Task {
614- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: metadata. account,
615- path: metadata. fileId,
616- name: " deleteComments " )
617- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
618- }
619- } completion: { _, _, error in
594+ NextcloudKit . shared. deleteComments ( fileId: metadata. fileId, messageId: tableComments. messageId, account: metadata. account) { _, _, error in
620595 if error == . success {
621596 self . loadComments ( )
622597 } else {
@@ -631,6 +606,6 @@ extension NCActivity: NCShareCommentsCellDelegate {
631606 )
632607 )
633608
634- presentMenu ( with: actions, sender : sender )
609+ presentMenu ( with: actions)
635610 }
636611}
0 commit comments