Browse Source

Notification view

Marino Faggiana 8 years ago
parent
commit
bf4f68d266

+ 16 - 11
iOSClient/Main/CCMain.m

@@ -1035,22 +1035,27 @@
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
     
     // Insert/update listOfNotifications in Dictionary App.listOfNotifications
-    if ([listOfNotifications isKindOfClass:[NSArray class]])
+    if ([listOfNotifications isKindOfClass:[NSArray class]]) {
+      
         for (OCNotifications *notification in listOfNotifications)
             [appDelegate.listOfNotifications setObject:notification forKey:[NSString stringWithFormat:@"%lu", (unsigned long)notification.idNotification]];
+        
+    } else {
+        
+        if ([app.listOfNotifications count] > 0) {
+            
+            CCNotification *notificationVC = [[UIStoryboard storyboardWithName:@"CCNotification" bundle:nil] instantiateViewControllerWithIdentifier:@"CCNotification"];
+            
+            notificationVC.view.superview.frame = CGRectMake(100,100,self.view.bounds.size.width-100,self.view.bounds.size.height-100);
+            
+            [self presentViewController:notificationVC animated:YES completion:nil];
+        }
+    }
     
     // Test if is already open, otherwise view the messages
-    if ([JSAlertView isOpenAlertWindows])
-        return;
-    
-    if ([app.listOfNotifications count] > 0) {
+    //if ([JSAlertView isOpenAlertWindows])
+    //    return;
     
-        CCNotification *notificationVC = [[UIStoryboard storyboardWithName:@"CCNotification" bundle:nil] instantiateViewControllerWithIdentifier:@"CCNotification"];
-
-        notificationVC.view.superview.frame = CGRectMake(100,100,self.view.bounds.size.width-100,self.view.bounds.size.height-100);
-
-        [self presentViewController:notificationVC animated:YES completion:nil];
-    }
     
     /*
     for (NSString *idNotification in app.listOfNotifications) {

+ 16 - 17
iOSClient/Notification/CCNotification.storyboard

@@ -26,49 +26,49 @@
                                     <rect key="frame" x="0.0" y="0.0" width="375" height="119"/>
                                     <autoresizingMask key="autoresizingMask"/>
                                     <subviews>
-                                        <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="YM1-uH-2Cz" userLabel="icon">
-                                            <rect key="frame" x="5" y="0.0" width="25" height="25"/>
-                                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
-                                        </imageView>
-                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" numberOfLines="4" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9fz-sr-8Gv" userLabel="message">
-                                            <rect key="frame" x="5" y="70" width="362" height="44"/>
+                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" numberOfLines="3" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pg1-7p-7Rz" userLabel="subject">
+                                            <rect key="frame" x="8" y="27" width="359" height="38"/>
                                             <constraints>
-                                                <constraint firstAttribute="height" constant="44" id="w3S-Mr-J41"/>
+                                                <constraint firstAttribute="height" constant="38" id="HIf-ra-ApH"/>
                                             </constraints>
                                             <fontDescription key="fontDescription" type="system" pointSize="10"/>
                                             <nil key="textColor"/>
                                             <nil key="highlightedColor"/>
                                         </label>
-                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YPu-No-c8x" userLabel="date">
-                                            <rect key="frame" x="49" y="2" width="316" height="21"/>
+                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" numberOfLines="4" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9fz-sr-8Gv" userLabel="message">
+                                            <rect key="frame" x="8" y="70" width="359" height="44"/>
                                             <constraints>
-                                                <constraint firstAttribute="height" constant="21" id="7uN-7d-1nq"/>
+                                                <constraint firstAttribute="height" constant="44" id="w3S-Mr-J41"/>
                                             </constraints>
                                             <fontDescription key="fontDescription" type="system" pointSize="10"/>
                                             <nil key="textColor"/>
                                             <nil key="highlightedColor"/>
                                         </label>
-                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" numberOfLines="3" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pg1-7p-7Rz" userLabel="subject">
-                                            <rect key="frame" x="5" y="27" width="362" height="38"/>
+                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YPu-No-c8x" userLabel="date">
+                                            <rect key="frame" x="49" y="2" width="316" height="21"/>
                                             <constraints>
-                                                <constraint firstAttribute="height" constant="38" id="HIf-ra-ApH"/>
+                                                <constraint firstAttribute="height" constant="21" id="7uN-7d-1nq"/>
                                             </constraints>
                                             <fontDescription key="fontDescription" type="system" pointSize="10"/>
                                             <nil key="textColor"/>
                                             <nil key="highlightedColor"/>
                                         </label>
+                                        <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="YM1-uH-2Cz" userLabel="icon">
+                                            <rect key="frame" x="8" y="0.0" width="25" height="25"/>
+                                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
+                                        </imageView>
                                     </subviews>
                                     <constraints>
                                         <constraint firstItem="pg1-7p-7Rz" firstAttribute="top" secondItem="9Bv-1W-yVV" secondAttribute="topMargin" constant="19" id="1w3-ai-rMk"/>
                                         <constraint firstAttribute="trailingMargin" secondItem="pg1-7p-7Rz" secondAttribute="trailing" id="DSQ-Yx-x7F"/>
                                         <constraint firstAttribute="trailingMargin" secondItem="9fz-sr-8Gv" secondAttribute="trailing" id="NcX-bX-zFO"/>
-                                        <constraint firstItem="9fz-sr-8Gv" firstAttribute="leading" secondItem="9Bv-1W-yVV" secondAttribute="leadingMargin" constant="-3" id="OV5-dL-Q2H"/>
+                                        <constraint firstItem="9fz-sr-8Gv" firstAttribute="leading" secondItem="9Bv-1W-yVV" secondAttribute="leadingMargin" id="OV5-dL-Q2H"/>
                                         <constraint firstItem="YPu-No-c8x" firstAttribute="top" secondItem="9Bv-1W-yVV" secondAttribute="topMargin" constant="-6" id="Z41-Sh-h01"/>
                                         <constraint firstAttribute="trailingMargin" secondItem="YPu-No-c8x" secondAttribute="trailing" constant="2" id="ato-Mb-ehY"/>
                                         <constraint firstItem="9fz-sr-8Gv" firstAttribute="leading" secondItem="pg1-7p-7Rz" secondAttribute="leading" id="h4U-rq-Ehk"/>
                                         <constraint firstItem="9fz-sr-8Gv" firstAttribute="top" secondItem="pg1-7p-7Rz" secondAttribute="bottom" constant="5" id="heM-QX-U30"/>
                                         <constraint firstItem="YPu-No-c8x" firstAttribute="leading" secondItem="9Bv-1W-yVV" secondAttribute="leadingMargin" constant="41" id="oKi-mq-icZ"/>
-                                        <constraint firstItem="pg1-7p-7Rz" firstAttribute="leading" secondItem="9Bv-1W-yVV" secondAttribute="leadingMargin" constant="-3" id="wM1-G2-xgz"/>
+                                        <constraint firstItem="pg1-7p-7Rz" firstAttribute="leading" secondItem="9Bv-1W-yVV" secondAttribute="leadingMargin" id="wM1-G2-xgz"/>
                                     </constraints>
                                 </tableViewCellContentView>
                                 <connections>
@@ -87,7 +87,6 @@
                     <navigationItem key="navigationItem" id="bV4-Hy-bmE"/>
                 </tableViewController>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="I7i-N5-tEB" userLabel="First Responder" sceneMemberID="firstResponder"/>
-                <searchDisplayController id="8eI-WG-bvp"/>
             </objects>
             <point key="canvasLocation" x="636" y="167.76611694152925"/>
         </scene>
@@ -96,7 +95,7 @@
             <objects>
                 <navigationController storyboardIdentifier="CCNotification" automaticallyAdjustsScrollViewInsets="NO" id="eRR-MW-ghy" sceneMemberID="viewController">
                     <toolbarItems/>
-                    <navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" id="jDh-Ju-bUL">
+                    <navigationBar key="navigationBar" contentMode="scaleToFill" id="jDh-Ju-bUL">
                         <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
                         <autoresizingMask key="autoresizingMask"/>
                     </navigationBar>

+ 89 - 30
iOSClient/Notification/CCNotification.swift

@@ -23,10 +23,9 @@
 
 import UIKit
 
-class CCNotification: UITableViewController, UISearchResultsUpdating {
+class CCNotification: UITableViewController {
 
     var resultSearchController = UISearchController()
-    
     let appDelegate = UIApplication.shared.delegate as! AppDelegate
     
     override func viewDidLoad() {
@@ -36,21 +35,6 @@ class CCNotification: UITableViewController, UISearchResultsUpdating {
         self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("_notification_", comment: "")
         self.navigationItem.setRightBarButton(UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(close)), animated: true)
         
-        self.resultSearchController = ({
-
-            let controller = UISearchController(searchResultsController: nil)
-            
-            controller.searchBar.sizeToFit()
-            controller.searchResultsUpdater = self
-            controller.dimsBackgroundDuringPresentation = false
-            controller.searchBar.scopeButtonTitles = ["A", "B", "C", "D"]
-            
-            self.tableView.tableHeaderView = controller.searchBar
-            self.tableView.tableFooterView = UIView()
-            
-            return controller
-        })()
-        
         self.tableView.reloadData()
     }
     
@@ -73,17 +57,48 @@ class CCNotification: UITableViewController, UISearchResultsUpdating {
    
     override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
         
-        let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
-            print("favorite button tapped")
-        }
-        favorite.backgroundColor = .red
+        let idsNotification: [String] = appDelegate.listOfNotifications.allKeys as! [String]
         
-        let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
-            print("share button tapped")
+        let idNotification : String! = idsNotification[editActionsForRowAt.row]
+        let notification = appDelegate.listOfNotifications[idNotification] as! OCNotifications
+
+        // No Action request
+        if notification.actions.count == 0 {
+            
+            let delete = UITableViewRowAction(style: .normal, title: NSLocalizedString("_delete_", comment: "")) { action, index in
+                print("delete button tapped")
+            }
+            delete.backgroundColor = .red
+            
+            return [delete]
+            
+        } else {
+        // Action request
+            
+            var buttons = [UITableViewRowAction]()
+            
+            for action in notification.actions {
+                
+                let button = UITableViewRowAction(style: .normal, title: (action as! OCNotificationsAction).label) { action, index in
+                    
+                    for actionNotification in notification.actions {
+                        
+                        if (actionNotification as! OCNotificationsAction).label == action.title  {
+                            print(action.title!)
+                        }
+                    }
+                }
+                if (action as! OCNotificationsAction).type == "DELETE" {
+                    button.backgroundColor = .red
+                } else {
+                    button.backgroundColor = .green
+                }
+                
+                buttons.append(button)
+            }
+            
+            return buttons
         }
-        share.backgroundColor = .green
-        
-        return [share, favorite]
     }
     
     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
@@ -112,17 +127,61 @@ class CCNotification: UITableViewController, UISearchResultsUpdating {
 
             let idNotification : String! = idsNotification[indexPath.row]
             let notification = appDelegate.listOfNotifications[idNotification] as! OCNotifications
-                
+            
+            let urlIcon = URL(string: notification.icon)!
+            let pathFileName = (appDelegate.directoryUser) + "/" + urlIcon.lastPathComponent
+            let image = UIImage(contentsOfFile: pathFileName)
+            
+            if image == nil {
+                //downloadImage(url: urlIcon)
+            } else {
+                cell.icon.image = image
+            }
+            
             cell.date.text = DateFormatter.localizedString(from: notification.date, dateStyle: .medium, timeStyle: .medium)
-            cell.subject.text = "let notification = appDelegate.listOfNotifications[idNotification] as! OCNotificationslet notification = appDelegate.listOfNotifications[idNotification] as! OCNotifications"//notification.subject
-            cell.message.text = "let notification = appDelegate.listOfNotifications[idNotification] as! OCNotificationslet notification = appDelegate.listOfNotifications[idNotification] as! OCNotifications"//notification.message
+            cell.subject.text = notification.subject
+            cell.message.text = notification.message
+            
         }
         
         return cell
     }
     
-    func updateSearchResults(for searchController: UISearchController) {
+    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        
+        tableView.deselectRow(at: indexPath, animated: true)
+    }
+    
+    // MARK: - Get Image from url
+    
+    func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _  response: URLResponse?, _ error: Error?) -> Void) {
+        URLSession.shared.dataTask(with: url) {
+            (data, response, error) in
+            completion(data, response, error)
+            }.resume()
+    }
+    
+    func downloadImage(url: URL) {
+        
+        print("Download Started")
+        getDataFromUrl(url: url) { (data, response, error)  in
+            guard let data = data, error == nil else { return }
+            let fileName = response?.suggestedFilename ?? url.lastPathComponent
+            print("Download Finished")
+            DispatchQueue.main.async() { () -> Void in
+                
+                do {
+                    let pathFileName = (self.appDelegate.directoryUser) + "/" + fileName
+                    try data.write(to: URL(fileURLWithPath: pathFileName), options: .atomic)
+                    
+                    self.tableView.reloadData()
+                } catch {
+                    print(error)
+                }
+            }
+        }
     }
+
 }
 
 // MARK: - Class UITableViewCell