Browse Source

Fix share permission advanced cells

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 years ago
parent
commit
7b58640d9e

+ 1 - 1
Nextcloud.xcodeproj/project.pbxproj

@@ -1210,8 +1210,8 @@
 			children = (
 			children = (
 				F700510022DF63AC003A3356 /* NCShare.storyboard */,
 				F700510022DF63AC003A3356 /* NCShare.storyboard */,
 				F700510422DF6A89003A3356 /* NCShare.swift */,
 				F700510422DF6A89003A3356 /* NCShare.swift */,
-				AF93471327E235EB002537EE /* Advanced */,
 				AF730AF727834B1400B7520E /* NCShare+NCCellDelegate.swift */,
 				AF730AF727834B1400B7520E /* NCShare+NCCellDelegate.swift */,
+				AF93471327E235EB002537EE /* Advanced */,
 				F723B3DC22FC6D1C00301EFE /* NCShareCommentsCell.xib */,
 				F723B3DC22FC6D1C00301EFE /* NCShareCommentsCell.xib */,
 				F7E4D9C322ED929B003675FD /* NCShareCommentsCell.swift */,
 				F7E4D9C322ED929B003675FD /* NCShareCommentsCell.swift */,
 				F769454522E9F1B0000A798A /* NCShareCommon.swift */,
 				F769454522E9F1B0000A798A /* NCShareCommon.swift */,

+ 2 - 2
iOSClient/Menu/NCShare+Menu.swift

@@ -9,7 +9,7 @@
 import Foundation
 import Foundation
 
 
 extension NCShare {
 extension NCShare {
-    func toggleShareMenu(for tableShare: tableShare) {
+    func toggleShareMenu(for share: tableShare) {
         
         
         var actions = [NCMenuAction]()
         var actions = [NCMenuAction]()
         
         
@@ -34,7 +34,7 @@ extension NCShare {
                     guard
                     guard
                         let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
                         let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
                         let navigationController = self.navigationController else { return }
                         let navigationController = self.navigationController else { return }
-                    advancePermission.share = tableShare
+                    advancePermission.share = tableShare(value: share)
                     advancePermission.metadata = self.metadata
                     advancePermission.metadata = self.metadata
                     navigationController.pushViewController(advancePermission, animated: true)
                     navigationController.pushViewController(advancePermission, animated: true)
                 }
                 }

+ 104 - 26
iOSClient/Share/Advanced/NCShareAdvancePermission.swift

@@ -20,7 +20,7 @@ class NCShareAdvancePermission: UITableViewController {
         super.viewDidLoad()
         super.viewDidLoad()
         self.shareConfig = ShareConfig(isDirectory: metadata.directory, share: share)
         self.shareConfig = ShareConfig(isDirectory: metadata.directory, share: share)
     }
     }
-    
+
     override func viewWillLayoutSubviews() {
     override func viewWillLayoutSubviews() {
         super.viewWillLayoutSubviews()
         super.viewWillLayoutSubviews()
         guard tableView.tableHeaderView == nil, tableView.tableFooterView == nil else { return }
         guard tableView.tableHeaderView == nil, tableView.tableFooterView == nil else { return }
@@ -120,11 +120,50 @@ class NCShareAdvancePermission: UITableViewController {
         guard let cell = shareConfig.cellFor(indexPath: indexPath) else { return UITableViewCell() }
         guard let cell = shareConfig.cellFor(indexPath: indexPath) else { return UITableViewCell() }
         return cell
         return cell
     }
     }
+
+    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        tableView.deselectRow(at: indexPath, animated: true)
+        guard let cellConfig = shareConfig.config(for: indexPath) else { return }
+        if let cellConfig = cellConfig as? ToggleCellConfig {
+            cellConfig.didSelect(for: share)
+            tableView.reloadData()
+        } else if let cellConfig = cellConfig as? Advanced {
+            switch cellConfig {
+            case .hideDownload:
+                share.hideDownload.toggle()
+                tableView.reloadData()
+            case .expirationDate: tableView.cellForRow(at: indexPath)?.becomeFirstResponder()
+            case .password:
+                guard share.password.isEmpty else {
+                    share.password = ""
+                    tableView.reloadData()
+                    return
+                }
+                let alertController = UIAlertController(title: NSLocalizedString("_enforce_password_protection_", comment: ""), message: "", preferredStyle: .alert)
+                alertController.addTextField { textField in
+                    textField.isSecureTextEntry = true
+                }
+                alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
+                let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
+                    let password = alertController.textFields?.first?.text
+                    self.share.password = password ?? ""
+                    tableView.reloadData()
+                }
+
+                alertController.addAction(okAction)
+
+                self.present(alertController, animated: true)
+            case .note: break
+                // TODO: Pushnote VC
+            }
+        }  // else: unkown cell
+    }
 }
 }
 
 
 protocol ShareCellConfig {
 protocol ShareCellConfig {
     var title: String { get }
     var title: String { get }
     func getCell(for share: tableShare) -> UITableViewCell
     func getCell(for share: tableShare) -> UITableViewCell
+    func didSelect(for share: tableShare)
 }
 }
 
 
 protocol ToggleCellConfig: ShareCellConfig {
 protocol ToggleCellConfig: ShareCellConfig {
@@ -136,6 +175,10 @@ extension ToggleCellConfig {
     func getCell(for share: tableShare) -> UITableViewCell {
     func getCell(for share: tableShare) -> UITableViewCell {
         return ToggleCell(isOn: isOn(for: share))
         return ToggleCell(isOn: isOn(for: share))
     }
     }
+
+    func didSelect(for share: tableShare) {
+        didChange(share, to: isOn(for: share))
+    }
 }
 }
 
 
 protocol Permission: ToggleCellConfig {
 protocol Permission: ToggleCellConfig {
@@ -157,15 +200,6 @@ enum UserPermission: CaseIterable, Permission {
         }
         }
     }
     }
 
 
-    func handleAction(for tableShare: tableShare) {
-        switch self {
-        case .reshare: break
-        case .edit: break
-        case .create: break
-        case .delete: break
-        }
-    }
-
     case reshare, edit, create, delete
     case reshare, edit, create, delete
     static let forDirectory: [UserPermission] = UserPermission.allCases
     static let forDirectory: [UserPermission] = UserPermission.allCases
     static let forFile: [UserPermission] = [.reshare, .edit]
     static let forFile: [UserPermission] = [.reshare, .edit]
@@ -209,17 +243,22 @@ enum LinkPermission: Permission {
 }
 }
 
 
 enum Advanced: CaseIterable, ShareCellConfig {
 enum Advanced: CaseIterable, ShareCellConfig {
+    func didSelect(for share: tableShare) {
+        switch self {
+        case .hideDownload: share.hideDownload.toggle()
+        case .expirationDate: return
+        case .password: return
+        case .note: return
+        }
+    }
+
     func getCell(for share: tableShare) -> UITableViewCell {
     func getCell(for share: tableShare) -> UITableViewCell {
         switch self {
         switch self {
         case .hideDownload:
         case .hideDownload:
             return ToggleCell(isOn: share.hideDownload)
             return ToggleCell(isOn: share.hideDownload)
         case .expirationDate:
         case .expirationDate:
-            let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareExpDate")
-            if let expDate = share.expirationDate {
-                cell.detailTextLabel?.text = DateFormatter.shareExpDate.string(from: expDate as Date)
-            }
-            return cell
-        case .password: return ToggleCell(isOn: !share.shareWith.isEmpty)
+            return DatePickerTableViewCell(date: share.expirationDate)
+        case .password: return ToggleCell(isOn: !share.password.isEmpty)
         case .note:
         case .note:
             let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareNote")
             let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareNote")
             cell.detailTextLabel?.text = share.note
             cell.detailTextLabel?.text = share.note
@@ -228,10 +267,6 @@ enum Advanced: CaseIterable, ShareCellConfig {
         }
         }
     }
     }
 
 
-    func didChange(_ share: tableShare, to newValue: Bool) {
-        
-    }
-
     var title: String {
     var title: String {
         switch self {
         switch self {
         case .hideDownload: return NSLocalizedString("_share_hide_download_", comment: "")
         case .hideDownload: return NSLocalizedString("_share_hide_download_", comment: "")
@@ -260,15 +295,23 @@ struct ShareConfig {
     }
     }
 
 
     func cellFor(indexPath: IndexPath) -> UITableViewCell? {
     func cellFor(indexPath: IndexPath) -> UITableViewCell? {
-        let cellConfig: ShareCellConfig
+        let cellConfig = config(for: indexPath)
+        let cell = cellConfig?.getCell(for: share)
+        cell?.textLabel?.text = cellConfig?.title
+        return cell
+    }
+
+    func didSelectRow(at indexPath: IndexPath) {
+        let cellConfig = config(for: indexPath)
+        cellConfig?.didSelect(for: share)
+    }
+
+    func config(for indexPath: IndexPath) -> ShareCellConfig? {
         if indexPath.section == 0, indexPath.row < permissions.count {
         if indexPath.section == 0, indexPath.row < permissions.count {
-            cellConfig = permissions[indexPath.row]
+            return  permissions[indexPath.row]
         } else if indexPath.section == 1, indexPath.row < advanced.count {
         } else if indexPath.section == 1, indexPath.row < advanced.count {
-            cellConfig = advanced[indexPath.row]
+            return advanced[indexPath.row]
         } else { return nil }
         } else { return nil }
-        let cell = cellConfig.getCell(for: share)
-        cell.textLabel?.text = cellConfig.title
-        return cell
     }
     }
 }
 }
 
 
@@ -295,3 +338,38 @@ extension DateFormatter {
         return dateFormatter
         return dateFormatter
     }()
     }()
 }
 }
+
+open class DatePickerTableViewCell: UITableViewCell {
+    let picker = UIDatePicker()
+
+    open override func awakeFromNib() {
+        super.awakeFromNib()
+        picker.datePickerMode = .date
+        if #available(iOS 13.4, *) {
+            picker.preferredDatePickerStyle = .wheels
+        }
+    }
+
+    open override var canBecomeFirstResponder: Bool {
+        return true
+    }
+
+    open override var canResignFirstResponder: Bool {
+        return true
+    }
+
+    open override var inputView: UIView? {
+        return picker
+    }
+
+    init(date: NSDate?) {
+        super.init(style: .value1, reuseIdentifier: "shareExpDate")
+        if let expDate = date {
+            detailTextLabel?.text = DateFormatter.shareExpDate.string(from: expDate as Date)
+        }
+    }
+    
+    required public init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+}

+ 0 - 4
iOSClient/Share/Advanced/NCShareAdvancePermissionHeader.swift

@@ -15,7 +15,3 @@ class NCShareAdvancePermissionHeader: UIView {
     @IBOutlet weak var favorite: UIButton!
     @IBOutlet weak var favorite: UIButton!
     @IBOutlet weak var fullWidthImageView: UIImageView!
     @IBOutlet weak var fullWidthImageView: UIImageView!
 }
 }
-
-protocol NCShareAdvancePermissionHeaderDelegate: AnyObject {
-    func favoriteClicked()
-}