Skip to content

Commit

Permalink
* Code Refraction
Browse files Browse the repository at this point in the history
* Enhancement
  • Loading branch information
AnbalaganD committed Mar 13, 2024
1 parent 992360c commit 2245915
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 96 deletions.
4 changes: 4 additions & 0 deletions LibraryManagement.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
54CD5A18229D352300B73CC1 /* NotificationCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CD5A17229D352300B73CC1 /* NotificationCell.swift */; };
54CD5A1A229D39C400B73CC1 /* LibraryNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CD5A19229D39C400B73CC1 /* LibraryNotification.swift */; };
54E1170A229817020026C6CD /* ShortcutIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54E11709229817020026C6CD /* ShortcutIdentifier.swift */; };
993118982BA20DE1007B0A34 /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 993118972BA20DE1007B0A34 /* DateExtension.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -86,6 +87,7 @@
54CD5A17229D352300B73CC1 /* NotificationCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationCell.swift; sourceTree = "<group>"; };
54CD5A19229D39C400B73CC1 /* LibraryNotification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibraryNotification.swift; sourceTree = "<group>"; };
54E11709229817020026C6CD /* ShortcutIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutIdentifier.swift; sourceTree = "<group>"; };
993118972BA20DE1007B0A34 /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -174,6 +176,7 @@
540B3664228C3A3800D123EE /* StringExtension.swift */,
540B3666228C3AB300D123EE /* ViewControllerExtension.swift */,
54673FF22ADCE00D0073B0A6 /* UIViewExtension.swift */,
993118972BA20DE1007B0A34 /* DateExtension.swift */,
);
path = Extension;
sourceTree = "<group>";
Expand Down Expand Up @@ -348,6 +351,7 @@
54C355FF229293BC0003E57D /* BookRequestController.swift in Sources */,
5415911A229519A4002EAF66 /* BookRequest.swift in Sources */,
5420912D228ECF70002320A4 /* SideMenuController.swift in Sources */,
993118982BA20DE1007B0A34 /* DateExtension.swift in Sources */,
5407B6F4228D7FFA000B1580 /* SlideTransitionAnimator.swift in Sources */,
540B365F228C35F100D123EE /* BookListController.swift in Sources */,
540B3667228C3AB300D123EE /* ViewControllerExtension.swift in Sources */,
Expand Down
26 changes: 26 additions & 0 deletions LibraryManagement/Extension/DateExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// DateExtension.swift
// LibraryManagement
//
// Created by Anbalagan on 13/03/24.
// Copyright © 2024 Anbalagan D. All rights reserved.
//

import Foundation

extension Date {
func toString(format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}

#if DEBUG
static func randomDate(in range: ClosedRange<Date>) -> Date {
let randomInterval = Double.random(
in: range.lowerBound.timeIntervalSince1970 ... range.upperBound.timeIntervalSince1970
)
return Date(timeIntervalSince1970: randomInterval)
}
#endif
}
4 changes: 1 addition & 3 deletions LibraryManagement/Extension/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import Foundation

extension String {
static var empty: String {
return ""
}
static let empty: String = ""

func substring(
fromIndex: Int,
Expand Down
2 changes: 1 addition & 1 deletion LibraryManagement/Modules/Book/BookListController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ extension BookListController: UITableViewDelegate, UITableViewDataSource {
]
)
}

func tableView(
_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@ final class AddBookRequestController: UIViewController {
setupView()
}

@objc private func requestBookTapped(_: UIButton) {
@objc private func requestBookTapped() {
if let book = selectedBook {
bookNotSelectedErrorLabel.isHidden = true

let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy 'at' HH:mm a"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
let date = formatter.string(from: Date())

let bookRequest = BookRequest(
id: UUID().uuidString.substring(fromIndex: 0, count: 8),
userName: "Anbalagan D",
date: date,
date: .now,
bookName: book.name,
status: .pending,
bookId: book.id
Expand All @@ -51,35 +45,7 @@ final class AddBookRequestController: UIViewController {
navigationController?.popViewController(animated: true)

if AppSettings.isNotificationEnable {
let content = UNMutableNotificationContent()
content.title = "New Book Request"
content.body = "\(bookRequest.userName) is request \(bookRequest.bookName)"
content.sound = UNNotificationSound.default

let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 0.1,
repeats: false
)
let notificationRequest = UNNotificationRequest(
identifier: "newBookRequest",
content: content,
trigger: trigger
)

let notificationDateFormatter = DateFormatter()
notificationDateFormatter.dateFormat = "dd/MM/yyyy"
let notificationDate = notificationDateFormatter.string(from: Date())
NotificationManager.shared.addNotification(
notification: LibraryNotification(
title: content.title,
detail: content.body,
date: notificationDate
)
)

UNUserNotificationCenter.current().add(notificationRequest) { error in
print(error?.localizedDescription ?? "Error")
}
NotificationManager.shared.addNotification(notification: bookRequest)
}
} else {
bookNotSelectedErrorLabel.isHidden = false
Expand Down
4 changes: 3 additions & 1 deletion LibraryManagement/Modules/BookRequest/BookRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
// Copyright © 2019 Anbalagan D. All rights reserved.
//

import Foundation

struct BookRequest {
let id: String
let userName: String
let date: String
let date: Date
let bookName: String
var status: BookRequestStatus
var bookId: String
Expand Down
2 changes: 1 addition & 1 deletion LibraryManagement/Modules/Cells/BookRequsetCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class BookRequsetCell: UITableViewCell {

userNameLabel.text = data.userName
requestIdLabel.text = "#\(data.id)"
dateLabel.text = data.date
dateLabel.text = data.date.toString(format: "dd/MM/yyyy 'at' hh:mm a")
bookNameLabel.text = data.bookName

updateStatus(data.status)
Expand Down
6 changes: 3 additions & 3 deletions LibraryManagement/Modules/Cells/NotificationCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ final class NotificationCell: UITableViewCell {
}

func setupData(data: LibraryNotification) {
notificationTitleLabel.text = data.title
dateLabel.text = data.date
notificationDetailLabel.text = data.detail
notificationTitleLabel.text = "New Book Request"
dateLabel.text = data.date.toString(format: "dd/MM/yyyy")
notificationDetailLabel.text = "\(data.userName) is request \(data.bookName)"
}
}

Expand Down
12 changes: 8 additions & 4 deletions LibraryManagement/Modules/Common/BookManger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ final class BookManager {
private var bookList = [Book]()

private init() {
#if DEBUG
addMockData()
#endif
}

func getBooks() -> [Book] {
Expand Down Expand Up @@ -96,6 +98,7 @@ final class BookManager {
}
}

#if DEBUG
private func addMockData() {

Check warning on line 102 in LibraryManagement/Modules/Common/BookManger.swift

View workflow job for this annotation

GitHub Actions / build

Function Body Length Violation: Function body should span 50 lines or less excluding comments and whitespace: currently spans 65 lines (function_body_length)
bookList.append(contentsOf: [
Book(
Expand Down Expand Up @@ -136,32 +139,33 @@ final class BookManager {
)
])

// Book Request List
let startDate = Date().addingTimeInterval(-(60 * 60 * 24 * 30)) // From last one month
bookRequestList.append(contentsOf: [
BookRequest(
id: UUID().uuidString.substring(fromIndex: 0, count: 8),
userName: "Anbalagan D",
date: "13/05/2019 at 12:05 PM",
date: .randomDate(in: startDate ... .now),
bookName: "Java Programming 8",
status: .pending,
bookId: bookList[0].id
),
BookRequest(
id: UUID().uuidString.substring(fromIndex: 0, count: 8),
userName: "Anbalagan D",
date: "15/05/2019 at 06:43 AM",
date: .randomDate(in: startDate ... .now),
bookName: "Advanced C# Programming",
status: .pending,
bookId: bookList[3].id
),
BookRequest(
id: UUID().uuidString.substring(fromIndex: 0, count: 8),
userName: "Anbalagan D",
date: "20/05/2019 at 01:27 PM",
date: .randomDate(in: startDate ... .now),
bookName: "Kotlin for Android",
status: .pending,
bookId: bookList[2].id
)
])
}
#endif
}
75 changes: 73 additions & 2 deletions LibraryManagement/Modules/Common/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,90 @@
//

import Foundation
import UserNotifications

final class NotificationManager {
final class NotificationManager: NSObject {
static let shared = NotificationManager()

private var notificationList = [LibraryNotification]()

private init() {}
private override init() {
super.init()
initalSetup()
}

private func initalSetup() {
UNUserNotificationCenter.current().delegate = self
}

func requestNotificationAuthorization(_ completion: @escaping (Bool) -> Void) {
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert, .badge, .sound]
) { granted, _ in
completion(granted)
}
}

func checkHasNotificationPermission(_ completion: @escaping (Bool) -> Void) {
UNUserNotificationCenter.current().getNotificationSettings { notificationSettings in
completion(notificationSettings.authorizationStatus == .authorized)
}
}

func setNotificationBadgeCount(_ count: Int) {
UNUserNotificationCenter.current().setBadgeCount(count)
}

func getNotification() -> [LibraryNotification] {
return notificationList
}

func addNotification(notification: LibraryNotification) {
notificationList.append(notification)
triggerNotification(notification)
}

private func triggerNotification(_ notification: LibraryNotification) {
let content = UNMutableNotificationContent()
content.title = "New Book Request"
content.body = "\(notification.userName) is request \(notification.bookName)"
content.sound = UNNotificationSound.default

let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 0.1,
repeats: false
)
let notificationRequest = UNNotificationRequest(
identifier: "newBookRequest",
content: content,
trigger: trigger
)

UNUserNotificationCenter.current().add(notificationRequest) { error in
if let error {
print(error.localizedDescription)
}
}
}
}

extension NotificationManager: UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
setNotificationBadgeCount(0)
print(response.notification.request.content.title)
completionHandler()
}

func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
setNotificationBadgeCount(0)
completionHandler([.list, .banner, .badge, .sound])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@
// Copyright © 2019 Anbalagan D. All rights reserved.
//

struct LibraryNotification {
let title: String
let detail: String
let date: String
}
typealias LibraryNotification = BookRequest
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension NotificationController {
notificationTableView.trailingAnchor.constraint(equalTo: view.safeTrailingAnchor),
notificationTableView.topAnchor.constraint(equalTo: view.safeTopAnchor),
notificationTableView.bottomAnchor.constraint(equalTo: view.safeBottomAnchor),

emptyImageView.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor, constant: -20),
emptyImageView.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor),
emptyImageView.heightAnchor.constraint(equalToConstant: 120),
Expand Down
16 changes: 15 additions & 1 deletion LibraryManagement/Modules/Setting/SettingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import UserNotifications

final class SettingController: UIViewController {
private var notificationSwitch: UISwitch!
private let notificationManager = NotificationManager.shared

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -23,7 +24,20 @@ final class SettingController: UIViewController {
}

@objc private func notificationChanged(_ sender: UISwitch) {
AppSettings.isNotificationEnable = sender.isOn
notificationManager.requestNotificationAuthorization { granded in
DispatchQueue.main.async {
if granded {
AppSettings.isNotificationEnable = sender.isOn
} else {
if sender.isOn {
sender.isOn = false
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}
}
}
}
}

@objc private func onLogout() {
Expand Down
Loading

0 comments on commit 2245915

Please sign in to comment.