From 1d1c9fc8887a884962ac95efcd9c572d6f55c121 Mon Sep 17 00:00:00 2001 From: Shresth Date: Thu, 20 Aug 2020 16:31:08 +0530 Subject: [PATCH 01/11] NAU login and user specific permission --- .../AmahiAnywhere.xcodeproj/project.pbxproj | 30 ++ .../AmahiAnywhere-Bridging-Header.h | 1 + AmahiAnywhere/AmahiAnywhere/AppDelegate.swift | 6 +- .../Data/Local/LocalStorage.swift | 17 + .../Data/Local/Models/HACModel.swift | 46 ++ .../Data/Local/PersistenceIdentifiers.swift | 2 + .../Data/Remote/ApiCalls/AmahiApi.swift | 4 + .../Data/Remote/ApiCalls/ServerApi.swift | 28 +- .../Data/Remote/Model/NAUPinResponse.swift | 14 + .../Data/Remote/Model/ServerShare.swift | 1 + .../Cast/RootContainerViewController.swift | 13 +- .../Presentation/Files/FilesPresenter.swift | 2 +- .../Files/FilesViewController.swift | 17 +- .../Login/LoginViewController.swift | 3 + .../Login/NAULoginController.swift | 123 +++++ .../Settings/SettingsViewController.swift | 4 +- .../Shares/SharesViewController.swift | 12 +- .../StoryBoards/Base.lproj/Main.storyboard | 272 ++++++++-- .../Utils/Gateway IP Utility/WifiGatewayIP.h | 20 + .../Utils/Gateway IP Utility/WifiGatewayIP.m | 32 ++ .../Gateway IP Utility/WifiGatewayIPHelper.c | 82 +++ .../Utils/Gateway IP Utility/route.h | 479 ++++++++++++++++++ .../AmahiAnywhere/Utils/Network.swift | 43 ++ 23 files changed, 1175 insertions(+), 76 deletions(-) create mode 100644 AmahiAnywhere/AmahiAnywhere/Data/Local/Models/HACModel.swift create mode 100644 AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/NAUPinResponse.swift create mode 100644 AmahiAnywhere/AmahiAnywhere/Presentation/Login/NAULoginController.swift create mode 100644 AmahiAnywhere/AmahiAnywhere/Utils/Gateway IP Utility/WifiGatewayIP.h create mode 100644 AmahiAnywhere/AmahiAnywhere/Utils/Gateway IP Utility/WifiGatewayIP.m create mode 100644 AmahiAnywhere/AmahiAnywhere/Utils/Gateway IP Utility/WifiGatewayIPHelper.c create mode 100644 AmahiAnywhere/AmahiAnywhere/Utils/Gateway IP Utility/route.h diff --git a/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj b/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj index 0097eb7938f..d9df1e77787 100644 --- a/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj +++ b/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj @@ -70,6 +70,10 @@ 46657183205085FC000E0D45 /* FilesPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46657182205085FC000E0D45 /* FilesPresenter.swift */; }; 46CB896B204FB0FC0031151F /* SharesPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46CB896A204FB0FC0031151F /* SharesPresenter.swift */; }; 46F87397204CC69500F1AF37 /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F87396204CC69500F1AF37 /* Server.swift */; }; + 7229DB3824EA9A490081AEF9 /* HACModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7229DB3724EA9A490081AEF9 /* HACModel.swift */; }; + 7229DB3A24EAF4700081AEF9 /* NAULoginController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7229DB3924EAF4700081AEF9 /* NAULoginController.swift */; }; + 7229DB4324EB303A0081AEF9 /* WifiGatewayIP.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229DB4224EB303A0081AEF9 /* WifiGatewayIP.m */; }; + 723E3D2A24EBD20C00FFFA9A /* NAUPinResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 723E3D2924EBD20C00FFFA9A /* NAUPinResponse.swift */; }; 7253F21824BDA89F0094C385 /* AudioThumbnailCollectionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7253F21624BDA89F0094C385 /* AudioThumbnailCollectionCell.swift */; }; 7253F21924BDA89F0094C385 /* AudioThumbnailCollectionCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7253F21724BDA89F0094C385 /* AudioThumbnailCollectionCell.xib */; }; 7253F21B24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7253F21A24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift */; }; @@ -215,6 +219,13 @@ 46F87396204CC69500F1AF37 /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = ""; }; 499AAFCDBDAB4C05C7264992 /* Pods-AmahiAnywhere.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AmahiAnywhere.release.xcconfig"; path = "Pods/Target Support Files/Pods-AmahiAnywhere/Pods-AmahiAnywhere.release.xcconfig"; sourceTree = ""; }; 665E1C7EF1F0DEBF3A9809B0 /* Pods-AmahiAnywhere.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AmahiAnywhere.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AmahiAnywhere/Pods-AmahiAnywhere.debug.xcconfig"; sourceTree = ""; }; + 7229DB3724EA9A490081AEF9 /* HACModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HACModel.swift; sourceTree = ""; }; + 7229DB3924EAF4700081AEF9 /* NAULoginController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NAULoginController.swift; sourceTree = ""; }; + 7229DB4224EB303A0081AEF9 /* WifiGatewayIP.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WifiGatewayIP.m; sourceTree = ""; }; + 7229DB4624EB31910081AEF9 /* WifiGatewayIPHelper.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = WifiGatewayIPHelper.c; sourceTree = ""; }; + 7229DB4824EB31CA0081AEF9 /* route.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = route.h; sourceTree = ""; }; + 723E3D2124EBA26600FFFA9A /* WifiGatewayIP.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WifiGatewayIP.h; sourceTree = ""; }; + 723E3D2924EBD20C00FFFA9A /* NAUPinResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NAUPinResponse.swift; sourceTree = ""; }; 7253F21624BDA89F0094C385 /* AudioThumbnailCollectionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioThumbnailCollectionCell.swift; sourceTree = ""; }; 7253F21724BDA89F0094C385 /* AudioThumbnailCollectionCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AudioThumbnailCollectionCell.xib; sourceTree = ""; }; 7253F21A24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AudioPlayerViewController+CollectionView.swift"; sourceTree = ""; }; @@ -390,6 +401,17 @@ path = Walkthrough; sourceTree = ""; }; + 723E3D2824EBA78500FFFA9A /* Gateway IP Utility */ = { + isa = PBXGroup; + children = ( + 7229DB4224EB303A0081AEF9 /* WifiGatewayIP.m */, + 723E3D2124EBA26600FFFA9A /* WifiGatewayIP.h */, + 7229DB4824EB31CA0081AEF9 /* route.h */, + 7229DB4624EB31910081AEF9 /* WifiGatewayIPHelper.c */, + ); + path = "Gateway IP Utility"; + sourceTree = ""; + }; 7258718D2496BC6100AF0A61 /* Views */ = { isa = PBXGroup; children = ( @@ -442,6 +464,7 @@ 3B37EBAE22ED11650065CB77 /* Units.swift */, 9935DAB32301FA21004816E1 /* RecentFiles.swift */, 727A95DF24D2DA350057C27C /* UIImageExtension.swift */, + 723E3D2824EBA78500FFFA9A /* Gateway IP Utility */, ); path = Utils; sourceTree = ""; @@ -503,6 +526,7 @@ 46098D03204FDF5500752EB6 /* ServerFile.swift */, 3B33324D22BAA17C00BC61EB /* FilteredServerFile.swift */, 3BF1E81922D0288700C531CD /* OfflineFileIndexes.swift */, + 723E3D2924EBD20C00FFFA9A /* NAUPinResponse.swift */, ); path = Model; sourceTree = ""; @@ -531,6 +555,7 @@ children = ( FB71BE99201CB2FC0005492C /* LoginViewController.swift */, C83C639C203992460089B9D8 /* LoginPresenter.swift */, + 7229DB3924EAF4700081AEF9 /* NAULoginController.swift */, ); path = Login; sourceTree = ""; @@ -562,6 +587,7 @@ 9935DAAE2301ED86004816E1 /* RecentFile+CoreDataClass.swift */, 3B2805E32312CA1C0022ED48 /* RecentFile+CoreDataProperties.swift */, 72DAFE1B24B6276A00755E25 /* AudioPlayerDataModel.swift */, + 7229DB3724EA9A490081AEF9 /* HACModel.swift */, ); path = Models; sourceTree = ""; @@ -893,6 +919,7 @@ 4605964B204F35E9004FA066 /* ServerRoute.swift in Sources */, 3B4DB99722A0431100AF287B /* ServerCollectionViewCell.swift in Sources */, 3B33325622BAA2AF00BC61EB /* FilesCollectionFooterView.swift in Sources */, + 7229DB3A24EAF4700081AEF9 /* NAULoginController.swift in Sources */, C8DEBFAB20BAE1D300B09AED /* ConnectionModeManager.swift in Sources */, C8ED025920B3E30300015846 /* URL.swift in Sources */, C8DA5B1A20D5DC2200F5A527 /* DownloadService.swift in Sources */, @@ -930,6 +957,7 @@ 72DAFE1C24B6276A00755E25 /* AudioPlayerDataModel.swift in Sources */, FDF80E94229FB2060087F9B8 /* VideoMenuCellSettings.swift in Sources */, AD8F2FE1226E9351009C8C4B /* SettingsViewController+MFMailComposeViewControllerDelegate.swift in Sources */, + 723E3D2A24EBD20C00FFFA9A /* NAUPinResponse.swift in Sources */, AD8F2FE2226E9351009C8C4B /* SettingsViewController+TableViewDelegate.swift in Sources */, C83C639D203992460089B9D8 /* LoginPresenter.swift in Sources */, C8DA5B2920D5E18800F5A527 /* FileSort.swift in Sources */, @@ -953,6 +981,7 @@ 3B2805E42312CA1C0022ED48 /* RecentFile+CoreDataProperties.swift in Sources */, 3B3F43DF22FECF7300BB42B0 /* DissmissAnimator.swift in Sources */, 725871932496C65300AF0A61 /* PlayerQueueContainerView.swift in Sources */, + 7229DB4324EB303A0081AEF9 /* WifiGatewayIP.m in Sources */, C83C637E2038D5150089B9D8 /* StringIdentifiers.swift in Sources */, 3B33325022BAA26600BC61EB /* FilesGridCollectionCell.swift in Sources */, 3BCAFBA322C537DE0044057E /* DownloadsGridCollectionCell.swift in Sources */, @@ -972,6 +1001,7 @@ 46098D08204FFFBE00752EB6 /* ServerApi.swift in Sources */, 3B33325422BAA29700BC61EB /* FilesCollectionHeaderView.swift in Sources */, 3B3F43E122FECF9300BB42B0 /* Interactor.swift in Sources */, + 7229DB3824EA9A490081AEF9 /* HACModel.swift in Sources */, C8D86160209490230014D8AC /* WebViewController.swift in Sources */, 3BF1E81A22D0288700C531CD /* OfflineFileIndexes.swift in Sources */, 7253F21B24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift in Sources */, diff --git a/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere-Bridging-Header.h b/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere-Bridging-Header.h index 74bcac72c4f..8afb8de1c34 100644 --- a/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere-Bridging-Header.h +++ b/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere-Bridging-Header.h @@ -11,3 +11,4 @@ // #import "MobileVLCKit/MobileVLCKit.h" +#import "WifiGatewayIP.h" diff --git a/AmahiAnywhere/AmahiAnywhere/AppDelegate.swift b/AmahiAnywhere/AmahiAnywhere/AppDelegate.swift index a75dc747d48..0ccd0dedea5 100644 --- a/AmahiAnywhere/AmahiAnywhere/AppDelegate.swift +++ b/AmahiAnywhere/AmahiAnywhere/AppDelegate.swift @@ -111,7 +111,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { self.window?.rootViewController = initialViewController self.window?.makeKeyAndVisible() } - } else { + }else if 2>3 { + //check for nau login. + } + + else { if LocalStorage.shared.contains(key: "walkthrough"){ // User already completed the onboarding initialViewController = mainStoryboard.instantiateInitialViewController() diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Local/LocalStorage.swift b/AmahiAnywhere/AmahiAnywhere/Data/Local/LocalStorage.swift index 3337ffadd6c..17961b6bf5e 100644 --- a/AmahiAnywhere/AmahiAnywhere/Data/Local/LocalStorage.swift +++ b/AmahiAnywhere/AmahiAnywhere/Data/Local/LocalStorage.swift @@ -21,12 +21,29 @@ final class LocalStorage: NSObject { UserDefaults.standard.setValue(string, forKey: key); UserDefaults.standard.synchronize(); } + + public func persistDictionaryArray(_ dictionary:[[String:String]],for key: String){ + UserDefaults.standard.set(dictionary, forKey: key) + } + + public func getDictionaryArray(for key: String)->[[String:String]]?{ + return UserDefaults.standard.object(forKey: key) as? [[String:String]] + } + public func getString(key: String!) -> String? { UserDefaults.standard.synchronize() return UserDefaults.standard.value(forKey: key) as? String; } + public func getBool(_ key:String) -> Bool{ + return UserDefaults.standard.bool(forKey: key) + } + + public func persist(_ bool:Bool, for key:String){ + UserDefaults.standard.set(bool, forKey: key) + } + public func contains(key: String!) -> Bool{ return UserDefaults.standard.object(forKey: key) != nil } diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Local/Models/HACModel.swift b/AmahiAnywhere/AmahiAnywhere/Data/Local/Models/HACModel.swift new file mode 100644 index 00000000000..1ef1927b6f9 --- /dev/null +++ b/AmahiAnywhere/AmahiAnywhere/Data/Local/Models/HACModel.swift @@ -0,0 +1,46 @@ +// +// HACModel.swift +// AmahiAnywhere +// +// Created by Shresth Pratap Singh on 17/08/20. +// Copyright © 2020 Amahi. All rights reserved. +// + +import Foundation +import EVReflection + +struct HACIdentifiers{ + static let server_ip = "server_ip" + static let session_token = "session_token" + static let auth_token = "auth_token" + static let server_address = "server_address" +} + +struct HDAAuthCache{ + + var serverLocalIP:String? + var sessionToken:String? + var authToken:String? + var serverAddress:String? + + init(from dictionary:[String:String]) { + serverLocalIP = (dictionary[HACIdentifiers.server_ip] == "") ? nil : dictionary[HACIdentifiers.server_ip] + sessionToken = (dictionary[HACIdentifiers.session_token] == "") ? nil : dictionary[HACIdentifiers.session_token] + authToken = (dictionary[HACIdentifiers.auth_token] == "") ? nil : dictionary[HACIdentifiers.auth_token] + serverAddress = (dictionary[HACIdentifiers.server_address] == "") ? nil : dictionary[HACIdentifiers.server_address] + } + + init(ip:String?, _ sessionToken:String?, _ authToken:String?,_ address:String?) { + self.serverLocalIP = ip + self.sessionToken = sessionToken + self.authToken = authToken + self.serverAddress = address + } + + var toDictionary:[String:String]{ + return [HACIdentifiers.server_ip : serverLocalIP ?? "", + HACIdentifiers.session_token : sessionToken ?? "", + HACIdentifiers.auth_token : authToken ?? "", + HACIdentifiers.server_address : serverAddress ?? ""] + } +} diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Local/PersistenceIdentifiers.swift b/AmahiAnywhere/AmahiAnywhere/Data/Local/PersistenceIdentifiers.swift index 3d0c77b9fa3..28a99bc422f 100644 --- a/AmahiAnywhere/AmahiAnywhere/Data/Local/PersistenceIdentifiers.swift +++ b/AmahiAnywhere/AmahiAnywhere/Data/Local/PersistenceIdentifiers.swift @@ -14,4 +14,6 @@ struct PersistenceIdentifiers { static let accessToken = "access_token" static let prefConnection = "pref_connection" + static let hdaAuthCache = "hda_auth_cache" + static let isNAULogin = "nau_logged_in" } diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/AmahiApi.swift b/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/AmahiApi.swift index 5b41462ee98..c723608d689 100644 --- a/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/AmahiApi.swift +++ b/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/AmahiApi.swift @@ -24,6 +24,10 @@ class AmahiApi { headers: headers, completion: completion) } + func login(pin:String,url:URL,compltion: @escaping (_ success:Bool,_ authToken:String?)->Void){ + Network.shared.login(pin: pin, url: url, completion: compltion) + } + func getServers(completion: @escaping (_ servers: [Server]?) -> Void ) { Network.shared.request(ApiEndPoints.fetchServers(), completion: completion) } diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift b/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift index 772eb05b5de..d1abc837aa6 100644 --- a/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift +++ b/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift @@ -11,11 +11,11 @@ import Alamofire import EVReflection class ServerApi { - public static var shared: ServerApi? + public static var shared: ServerApi? = ServerApi() - private var server: Server! + private var server: Server? private var serverRoute: ServerRoute? - private var serverAddress: String? + var serverAddress: String? public var auth_token: String? // if nil -> server is default welcome to amahi private init() {} @@ -44,9 +44,9 @@ class ServerApi { public func getServerHeaders() -> HTTPHeaders { if let authToken = auth_token{ - return [ "Session": server.session_token!, "Authorization": authToken] + return ["Session": (server?.session_token ?? ""), "Authorization": authToken] }else{ - return [ "Session": server.session_token! ] + return [ "Session": server?.session_token ?? "" ] } } @@ -102,13 +102,13 @@ class ServerApi { } func getShares(completion: @escaping (_ serverShares: [ServerShare]?) -> Void ) { - if serverRoute == nil{ - completion(nil) - return - } - - if serverAddress == nil{ - serverAddress = ConnectionModeManager.shared.currentConnectionBaseURL(serverRoute: serverRoute!) + if !LocalStorage.shared.getBool(PersistenceIdentifiers.isNAULogin){ + if serverRoute == nil{ + completion(nil) + } + if serverAddress == nil{ + serverAddress = ConnectionModeManager.shared.currentConnectionBaseURL(serverRoute: serverRoute!) + } } Network.shared.request(ApiEndPoints.getServerShares(serverAddress), headers: getServerHeaders(), completion: completion) @@ -181,7 +181,7 @@ class ServerApi { URLQueryItem(name: "s", value: file.parentShare!.name), URLQueryItem(name: "p", value: file.getPath()), URLQueryItem(name: "mtime", value: String(file.getLastModifiedEpoch())), - URLQueryItem(name: "session", value: server.session_token), + URLQueryItem(name: "session", value: (server?.session_token)), ] if let authToken = auth_token{ @@ -200,7 +200,7 @@ class ServerApi { URLQueryItem(name: "s", value: file.parentShare!.name), URLQueryItem(name: "p", value: file.getPath()), URLQueryItem(name: "mtime", value: String(file.getLastModifiedEpoch())), - URLQueryItem(name: "session", value: server.session_token) + URLQueryItem(name: "session", value: (server?.session_token ?? "")) ] if let authToken = auth_token{ diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/NAUPinResponse.swift b/AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/NAUPinResponse.swift new file mode 100644 index 00000000000..0195ed64ff9 --- /dev/null +++ b/AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/NAUPinResponse.swift @@ -0,0 +1,14 @@ +// +// NAUPinResponse.swift +// AmahiAnywhere +// +// Created by Shresth Pratap Singh on 18/08/20. +// Copyright © 2020 Amahi. All rights reserved. +// + +import Foundation +import EVReflection + +class NAUPinResponse: EVNetworkingObject { + +} diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/ServerShare.swift b/AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/ServerShare.swift index bf7560f372f..f2146310a1c 100644 --- a/AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/ServerShare.swift +++ b/AmahiAnywhere/AmahiAnywhere/Data/Remote/Model/ServerShare.swift @@ -16,6 +16,7 @@ public class ServerShare: EVNetworkingObject { public var name: String? = nil public var tags: [String]? = nil public var mtime: Date? = nil + public var writable:Bool = false // Overriding setValue for ignores undefined keys override public func setValue(_ value: Any!, forUndefinedKey key: String) {} diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Cast/RootContainerViewController.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Cast/RootContainerViewController.swift index dae4d7839e2..29cdd26686a 100644 --- a/AmahiAnywhere/AmahiAnywhere/Presentation/Cast/RootContainerViewController.swift +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Cast/RootContainerViewController.swift @@ -23,7 +23,7 @@ class RootContainerViewController: UIViewController, GCKUIMiniMediaControlsViewC } } } - + var isNAULogin = false var overridenNavigationController: UINavigationController? override var navigationController: UINavigationController? { get { @@ -85,7 +85,16 @@ class RootContainerViewController: UIViewController, GCKUIMiniMediaControlsViewC override func prepare(for segue: UIStoryboardSegue, sender _: Any?) { if segue.identifier == "NavigationVCEmbedSegue" { if let tabBarController = segue.destination as? UITabBarController{ - tabBarController.selectedIndex = 1 + if isNAULogin{ + let sharesVC = viewController(viewControllerClass: SharesViewController.self, from: StoryBoardIdentifiers.main) + sharesVC.isNAULogin = true + let navCon = UINavigationController(rootViewController: sharesVC) + tabBarController.viewControllers?[1] = navCon + navCon.tabBarItem = UITabBarItem(title: "HDA", image: UIImage(named: "tabBarHDA"), tag: 0) + tabBarController.selectedIndex = 1 + }else{ + tabBarController.selectedIndex = 1 + } } navigationController = (segue.destination as? UINavigationController) } diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesPresenter.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesPresenter.swift index 7ed8601091c..2cab34bba80 100644 --- a/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesPresenter.swift +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesPresenter.swift @@ -120,7 +120,7 @@ class FilesPresenter: BasePresenter { /* Auth-token for HDA authorisation in PIN */ let authToken = ServerApi.shared?.auth_token - let dict = ["day":day, "month":month!, "year":year!, "fileName":fileName, "fileURL":fileURL, "serverName":ServerApi.shared!.getServer()!.name!, "size":selectedFile.getFileSize(), "mimeType":mimeType, "mtimeDate":mtimeDate!, "authToken":authToken!, "path": path, "sizeNumber": selectedFile.size!] as [String : Any] + let dict = ["day":day, "month":month!, "year":year!, "fileName":fileName, "fileURL":fileURL, "serverName":(ServerApi.shared!.getServer()?.name! ?? ""), "size":selectedFile.getFileSize(), "mimeType":mimeType, "mtimeDate":mtimeDate!, "authToken":authToken!, "path": path, "sizeNumber": selectedFile.size!] as [String : Any] RecentsDatabaseHelper.shareInstance.save(object: dict) diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift index cff3d73df45..a1ea6be75d7 100644 --- a/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift @@ -94,7 +94,9 @@ class FilesViewController: BaseUIViewController, GCKRemoteMediaClientListener { super.viewDidLoad() setupNotifications() presenter = FilesPresenter(self) - setupFloaty() + if share.writable{ + setupFloaty() + } setupLayoutView() setupRefreshControl() setupSearchBar() @@ -375,10 +377,6 @@ class FilesViewController: BaseUIViewController, GCKRemoteMediaClientListener { self.removeOfflineFile(indexPath: indexPath) }! - let delete = self.creatAlertAction(StringLiterals.delete, style: .destructive) { (action) in - self.deleteFile(file) - }! - let stop = self.creatAlertAction(StringLiterals.stopDownload, style: .default) { (action) in if let offlineFile = OfflineFileIndexes.indexPathsForOfflineFiles[indexPath]{ DownloadService.shared.cancelDownload(offlineFile) @@ -395,8 +393,13 @@ class FilesViewController: BaseUIViewController, GCKRemoteMediaClientListener { } else if state == .downloading { actions.append(stop) } - - actions.append(delete) + if fileShare.writable{ + let delete = self.creatAlertAction(StringLiterals.delete, style: .destructive) { (action) in + self.deleteFile(file) + }! + + actions.append(delete) + } let cancel = self.creatAlertAction(StringLiterals.cancel, style: .cancel, clicked: nil)! actions.append(cancel) diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Login/LoginViewController.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Login/LoginViewController.swift index 519ea96e96d..f2e157227ce 100644 --- a/AmahiAnywhere/AmahiAnywhere/Presentation/Login/LoginViewController.swift +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Login/LoginViewController.swift @@ -15,6 +15,8 @@ class LoginViewController: BaseUIViewController { @IBOutlet private weak var passwordInputField: SkyFloatingLabelTextField! @IBOutlet private weak var showHideButton: UIButton! + @IBOutlet weak var pinLoginButton: UIButton! + private var presenter: LoginPresenter! override func viewDidLoad() { @@ -72,6 +74,7 @@ class LoginViewController: BaseUIViewController { passwordInputField.isSecureTextEntry = true showHideButton.setImage(UIImage(named: "passHidden"), for: .normal) showHideButton.isHidden = true + navigationController?.setNavigationBarHidden(true, animated: false) } diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Login/NAULoginController.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Login/NAULoginController.swift new file mode 100644 index 00000000000..ff341b4166d --- /dev/null +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Login/NAULoginController.swift @@ -0,0 +1,123 @@ +// +// NAULoginController.swift +// AmahiAnywhere +// +// Created by Shresth Pratap Singh on 17/08/20. +// Copyright © 2020 Amahi. All rights reserved. +// + +import UIKit +import SkyFloatingLabelTextField +import Alamofire + +class NAULoginController: UIViewController, UITextFieldDelegate { + + private var availableIPAddress:String? + + @IBOutlet weak var pinTextField: SkyFloatingLabelTextField! + + override func viewDidLoad() { + super.viewDidLoad() + title = "Pin Login" + pinTextField.textAlignment = .center + pinTextField.delegate = self + + if let ip = WifiGatewayIP.getGatewayIP(){ + let subnetBlocks = ip.components(separatedBy: ".") + let networkAddress = subnetBlocks[0] + "." + subnetBlocks[1] + "." + subnetBlocks[2] + "." + let host = (Int(subnetBlocks[3]) ?? 1) + 1 + findAvailableIP(with:networkAddress,host) + } + } + + override func viewWillAppear(_ animated: Bool) { + self.navigationController?.setNavigationBarHidden(false, animated: false) + } + + override func viewWillDisappear(_ animated: Bool) { + self.navigationController?.setNavigationBarHidden(true, animated: false) + } + + @IBAction func submitPin(_ sender: UIButton) { + if let pin = pinTextField.text, !pin.isEmpty{ + if pinFormatIsValid(pin){ + if let ip = availableIPAddress,let url = URL(string: "http://" + ip + ":4563/auth"){ + AmahiApi.shared.login(pin: pin, url: url) { (success, authToken) in + DispatchQueue.main.async { + ServerApi.shared?.auth_token = authToken + let address = "http://"+ip+":4563" + ServerApi.shared?.serverAddress = address + +// let hdaCache = HDAAuthCache(ip: ip, nil, authToken, address) +// let dict = hdaCache.toDictionary +// var authCache = LocalStorage.shared.getDictionaryArray(for: PersistenceIdentifiers.hdaAuthCache) as? [[String:String]] +// if authCache != nil{ +// authCache?.append(dict) +// } +// +// LocalStorage.shared.persistDictionaryArray(authCache ?? [dict], for: PersistenceIdentifiers.hdaAuthCache) + LocalStorage.shared.persist(true, for: PersistenceIdentifiers.isNAULogin) + self.setupViewController() + } + } + } + }else{ + pinTextField.errorColor = .red + pinTextField.errorMessage = "A 3-5 digit alpha-numeric pin is expected!" + } + }else{ + pinTextField.errorColor = .red + pinTextField.errorMessage = "Please enter a pin to continue!" + } + } + + private func setupViewController(){ + if let rootVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RootVC") as? RootContainerViewController{ + + rootVC.isNAULogin = true + if let appDelegate = UIApplication.shared.delegate as? AppDelegate, let currentWindow = appDelegate.window{ + UIView.transition(with: currentWindow, duration: 0.3, options: .transitionFlipFromRight, animations: { + currentWindow.rootViewController = rootVC + currentWindow.makeKeyAndVisible() + }, completion: nil) + } + } + } + + private func pinFormatIsValid(_ testString:String)->Bool{ + //evaluating pin regular expression + let pinExpression = "[A-Za-z0-9]+" + let pinTest = NSPredicate(format:"SELF MATCHES %@",pinExpression) + let expressionResult = pinTest.evaluate(with: testString) + + var isPinLengthValid = false + (testString.count >= 3 && testString.count <= 5) ? (isPinLengthValid = true) : (isPinLengthValid = false) + + return isPinLengthValid && expressionResult + } + + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + (textField as! SkyFloatingLabelTextField).errorMessage = nil + return true + } + + func findAvailableIP(with networkAddress: String,_ host : Int){ + if host > 255 || host < 0 || self.availableIPAddress != nil{ + return + } + let ip = networkAddress + "\(host)" + Network.shared.pingOnce(ip) { (success) in + DispatchQueue.main.async { + print("\n--------------\n") + print(success) + print(ip) + print("\n---------------\n") + if success{ + self.availableIPAddress = ip + }else{ + self.findAvailableIP(with: networkAddress, host+1) + } + } + } + } +} diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Settings/SettingsViewController.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Settings/SettingsViewController.swift index 5f70d886b57..7a2a3f8b952 100644 --- a/AmahiAnywhere/AmahiAnywhere/Presentation/Settings/SettingsViewController.swift +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Settings/SettingsViewController.swift @@ -53,11 +53,11 @@ class SettingsViewController: BaseUITableViewController { internal func signOut() { self.dismiss(animated: false, completion: nil) LocalStorage.shared.logout{} - let loginVc = self.viewController(viewControllerClass: LoginViewController.self, from: StoryBoardIdentifiers.main) + let loginNavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() if let appDelegate = UIApplication.shared.delegate as? AppDelegate, let currentWindow = appDelegate.window{ UIView.transition(with: currentWindow, duration: 0.3, options: .transitionFlipFromRight, animations: { //removing strong references from all other VC to let ARC automatically delete them from memmory - currentWindow.rootViewController = loginVc + currentWindow.rootViewController = loginNavigationController currentWindow.makeKeyAndVisible() }, completion: nil) } diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Shares/SharesViewController.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Shares/SharesViewController.swift index 97f20fd024f..1c1e8de490d 100644 --- a/AmahiAnywhere/AmahiAnywhere/Presentation/Shares/SharesViewController.swift +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Shares/SharesViewController.swift @@ -13,6 +13,7 @@ class SharesViewController: BaseUIViewController, UICollectionViewDelegate, UICo @IBOutlet fileprivate var sharesCollectionView: UICollectionView! @IBOutlet var serverNameLabel: UILabel! + var isNAULogin = false internal var server: Server? private var shares: [ServerShare] = [ServerShare]() private var presenter: SharesPresenter! @@ -35,7 +36,7 @@ class SharesViewController: BaseUIViewController, UICollectionViewDelegate, UICo override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(unreachableHDA), name: .HDAUnreachable, object: nil) - if server?.name != "Welcome to Amahi"{ + if server?.name != "Welcome to Amahi" && !isNAULogin{ navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Log out", style: .done, target: self, action: #selector(logOutTapped)) @@ -51,7 +52,6 @@ class SharesViewController: BaseUIViewController, UICollectionViewDelegate, UICo serverNameLabel.textColor = UIColor.white } - removePinVC() sharesCollectionView.delegate = self sharesCollectionView.dataSource = self sharesCollectionView.addSubview(refreshControl) @@ -60,7 +60,13 @@ class SharesViewController: BaseUIViewController, UICollectionViewDelegate, UICo serverNameLabel.text = ServerApi.shared!.getServer()?.name presenter = SharesPresenter(self) - presenter.loadServerRoute() + + if !isNAULogin{ + removePinVC() + presenter.loadServerRoute() + }else{ + presenter.getShares() + } NotificationCenter.default.addObserver(self, selector: #selector(expiredAuthTokenHDA), name: .HDATokenExpired, object: nil) } diff --git a/AmahiAnywhere/AmahiAnywhere/StoryBoards/Base.lproj/Main.storyboard b/AmahiAnywhere/AmahiAnywhere/StoryBoards/Base.lproj/Main.storyboard index fa9a410a737..5dd2f201813 100644 --- a/AmahiAnywhere/AmahiAnywhere/StoryBoards/Base.lproj/Main.storyboard +++ b/AmahiAnywhere/AmahiAnywhere/StoryBoards/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -12,7 +12,7 @@ - + @@ -104,25 +104,41 @@ + + + + + + + + + + + + + + + + - + - + - - + + - - + + @@ -153,7 +169,7 @@ - + @@ -184,14 +200,14 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + - + - + @@ -356,9 +439,9 @@ - + - +