Skip to content

Commit

Permalink
Merge branch 'step4/feedback' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeffie committed Feb 13, 2024
2 parents 0fca4b4 + 0e24dbe commit 9172fd7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import UIKit

final class ClientListTableView: UITableView {
typealias ClientCell = ClientListTableViewCell
typealias DataSource = ClientListDataSource
typealias HeaderView = ClientListHeaderView

let type: BankList
Expand Down
85 changes: 28 additions & 57 deletions BankManagerUIApp/BankManagerUIApp/ViewModel/BankViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ final class BankViewModel {

private var waitingList: [Client] {
didSet {
updateWaitingList()
self.delegate?.updateWaitingList(with: self.waitingList)
}
}

private var workingList: [Client] {
didSet {
updateWorkingList()
self.delegate?.updateWorkingList(with: self.workingList)
}
}

private var timeString: String {
didSet {
updateTime()
self.delegate?.updateTime(with: self.timeString)
}
}

Expand Down Expand Up @@ -62,90 +62,61 @@ extension BankViewModel: BankIntput {
// MARK: - BankManagerDelegate
extension BankViewModel: BankManagerDelegate {
func handleDequeueClient(client: Client) {
removeWaitingClient(client: client)
}

func handleEnqueueClient(client: Client) {
addWaitingClient(client: client)
}

func handleEndTask(client: Client) {
removeWorkingClient(client: client)
}

func handleStartTask(client: Client) {
addWorkingClient(client: client)
}

func handleClearClient() {
clearClients()
}

func handleTimer(timeString: String) {
self.timeString = timeString
}
}

// MARK: - Private Methods
private extension BankViewModel {
func addWaitingClient(client: Client) {
DispatchQueue.global().async {
self.waitingSemaphore.wait()
self.waitingList.append(client)
guard
let index = self.waitingList.firstIndex(where: { target in client == target })
else {
self.waitingSemaphore.signal()
return
}
self.waitingList.remove(at: index)
self.waitingSemaphore.signal()
}
}

func removeWaitingClient(client: Client) {
func handleEnqueueClient(client: Client) {
DispatchQueue.global().async {
self.waitingSemaphore.wait()
guard
let index = self.waitingList.firstIndex(where: { target in client == target })
else { return }
self.waitingList.remove(at: index)
self.waitingSemaphore.signal()
self.waitingList.append(client)
self.waitingSemaphore.signal()
}
}

func addWorkingClient(client: Client) {
func handleEndTask(client: Client) {
DispatchQueue.global().async {
self.workingSemaphore.wait()
self.workingList.append(client)
guard
let index = self.workingList.firstIndex(where: { target in client == target })
else {
self.workingSemaphore.signal()
return
}
self.workingList.remove(at: index)
self.workingSemaphore.signal()
}
}

func removeWorkingClient(client: Client) {
func handleStartTask(client: Client) {
DispatchQueue.global().async {
self.workingSemaphore.wait()
guard
let index = self.workingList.firstIndex(where: { target in client == target })
else { return }
self.workingList.remove(at: index)
self.workingList.append(client)
self.workingSemaphore.signal()
}
}

func clearClients() {
func handleClearClient() {
DispatchQueue.global().async {
self.waitingSemaphore.wait()
self.workingSemaphore.wait()
self.waitingList.removeAll()
self.workingList.removeAll()
self.waitingSemaphore.signal()
self.workingSemaphore.signal()
}
}
}

private extension BankViewModel {
func updateWaitingList() {
self.delegate?.updateWaitingList(with: self.waitingList)
}

func updateWorkingList() {
self.delegate?.updateWorkingList(with: self.workingList)
}

func updateTime() {
self.delegate?.updateTime(with: self.timeString)
func handleTimer(timeString: String) {
self.timeString = timeString
}
}

0 comments on commit 9172fd7

Please sign in to comment.