Skip to content

Commit

Permalink
Build Home Screen Permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazan98 committed Sep 5, 2023
1 parent 292b400 commit e4c34ea
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 42 deletions.
Binary file not shown.

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions RM Client/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@
</array>
</dict>
</dict>
<key>permi</key>
<string></string>
<key>NSAppleMusicUsageDescription</key>
<string> $(PRODUCT_NAME) photo use</string>
<key>NSCameraUsageDescription</key>
<string> $(PRODUCT_NAME) photo use</string>
</dict>
</plist>
44 changes: 23 additions & 21 deletions RM Client/UseCases/GetHomeScreenItemsVerticalListUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,37 @@ public class GetHomeScreenItemsVerticalListUseCase : RmUseCase<[HomeScreenItem]>
}

// 3. Notifications Permission
if RmPermissionsManager.shared.isNotificationsPermissionEnabled() == false {
screenItems.append(HomeScreenNotificationsPermissionItem())
}

// 4. Male Characters
self?.getCharachets(sectionName: "male", query: ["gender": "male"]) { result in
if result.isEmpty == false {
screenItems.append(HomeScreenCharactersListItem(
list: result.reversed(),
sectionName: "section_male".getLocalizedString()
))
}

// 5. Storage Permission
if RmPermissionsManager.shared.isStoragePermissionEnabled() == false {
screenItems.append(HomeScreenStorageItem())
RmPermissionsManager.shared.isNotificationsPermissionEnabled { permissionResult in
if !permissionResult {
screenItems.append(HomeScreenNotificationsPermissionItem())
}

// 6. Male Characters
self?.getCharachets(sectionName: "female", query: ["gender": "female"]) { result in
// 4. Male Characters
self?.getCharachets(sectionName: "male", query: ["gender": "male"]) { result in
if result.isEmpty == false {
screenItems.append(HomeScreenCharactersListItem(
list: result.reversed(),
sectionName: "section_female".getLocalizedString()
sectionName: "section_male".getLocalizedString()
))
}

self?.onSubmitLoadingValue(newState: false)
self?.onSubmitResponseValue(value: screenItems)
// 5. Storage Permission
if RmPermissionsManager.shared.isStoragePermissionEnabled() == false {
screenItems.append(HomeScreenStorageItem())
}

// 6. Male Characters
self?.getCharachets(sectionName: "female", query: ["gender": "female"]) { result in
if result.isEmpty == false {
screenItems.append(HomeScreenCharactersListItem(
list: result.reversed(),
sectionName: "section_female".getLocalizedString()
))
}

self?.onSubmitLoadingValue(newState: false)
self?.onSubmitResponseValue(value: screenItems)
}
}
}
}
Expand Down
32 changes: 26 additions & 6 deletions RM Client/Utils/RmPermissionsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@ public final class RmPermissionsManager {

private init() {}

public func isNotificationsPermissionEnabled() -> Bool {
var status: Bool = false
public func isNotificationsPermissionEnabled(onComplete: @escaping (Bool) -> Void) {
let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: { (settings) in
if settings.authorizationStatus == .notDetermined {
status = false
onComplete(false)
} else if settings.authorizationStatus == .denied {
status = false
onComplete(false)
} else if settings.authorizationStatus == .authorized {
status = true
onComplete(true)
}
})
return status
}

public func isStoragePermissionEnabled() -> Bool {
Expand All @@ -42,4 +40,26 @@ public final class RmPermissionsManager {
return status
}

public func onRequestNotificationsPermission(onComplete: @escaping () -> Void) {
UNUserNotificationCenter
.current()
.requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in
if let error = error {
print("Request Authorization Failed (\(error), \(error.localizedDescription))")
} else {
onComplete()
}
}
}

public func onRequestGalleryAccess(onComplete: @escaping () -> Void) {
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
onComplete()
} else {

}
}
}

}
17 changes: 16 additions & 1 deletion RM Client/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@ public class Utils {
let message = MDCSnackbarMessage()
message.text = errorMessage
MDCSnackbarManager.default.show(message)
}
}

static func onShowNotification(title: String, body: String) {
let content = UNMutableNotificationContent()
content.categoryIdentifier = "permission_notifications"
content.title = title
content.subtitle = ""
content.body = body
content.sound = UNNotificationSound.default

// trigger:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ class HomeNotificationsPermissionCollectionViewCell: UICollectionViewCell {
acceptPermissionButton?.setTitle("home_notifications_button".getLocalizedString(), for: .normal)
titleView?.text = "home_notifications_title".getLocalizedString()
descriptionView?.text = "home_notifications_des".getLocalizedString()

acceptPermissionButton?.addTarget(self, action: #selector(onRequestNotificationsPermission), for: .touchUpInside)
}

@objc func onRequestNotificationsPermission() {
RmPermissionsManager.shared.onRequestNotificationsPermission {
Utils.onShowNotification(
title: "Notifications Permission",
body: "Now Rick and Morty can Submit Notifications"
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ class HomeStoragePermissionItemCollectionViewCell: UICollectionViewCell {
button?.setTitle("permission_button".getLocalizedString(), for: .normal)
title?.text = "storage_permission".getLocalizedString()
hint?.text = "storage_permission_hint".getLocalizedString()

button?.addTarget(self, action: #selector(onRequestStoragePermission), for: .touchUpInside)
}

@objc func onRequestStoragePermission() {
RmPermissionsManager.shared.onRequestGalleryAccess {
Utils.onShowNotification(
title: "Gallery Permission",
body: "Now Rick and Morty can Access Local Storage ..."
)
}
}

}

0 comments on commit e4c34ea

Please sign in to comment.