iOS/Refactoring

[UIKit/Refactoring] MyPageNotificationViewModel 코드 개선하기

suee97 2024. 7. 9. 03:54
1

 

저는 이런식으로 주석을 달아서, 어느 파트가 눈에 쉽게 보이는지를 구성해왔습니다.

근데, 주석은.. 기껏해야 주절주절이기 때문에, 삭제하기로 했습니다.

또한, extension으로 구분하는 것도 없앴습니다.

 

2
func readNotification(index: Int) {
    if notifications[index].status == .UnRead {
        notifications[index].status = .Read
        putNotificationReadStatus(notificationId: notifications[index].id)
    }
}

 

알림을 읽지 않은 상태라면 읽음 처리를 하는 함수입니다.

들여쓰기를 없앨 수 있어서 없앴습니다.

func readNotification(index: Int) {
    guard notifications[index].status == .UnRead else { return }
    notifications[index].status = .Read
    putNotificationReadStatus(notificationId: notifications[index].id)
}

 

3
func scrollViewDidReachBottom(cellIds: [Int64]) {
    guard let lastId = notifications.last?.id, !isFetching else { return }
    let isFetched = fetchedPages.contains(lastId)
    if !isFetched && cellIds.contains(lastId) {
        fetchNotifications(notificationId: lastId)
    }
}

 

스크롤이 바닥에 닿았을 때, 추가로 api를 요청하는 함수입니다.

isFetched라는 변수는, 이미 해당 id로 호출한 기록을 확인하는 용도로 만든 변수인데, 차라리 fetchedPages.contains를 조건문에 직접 넣는게 읽기 편해보입니다.

func scrollViewDidReachBottom(cellIds: [Int64]) {
    guard let lastId = notifications.last?.id, !isFetching else { return }
    if !fetchedPages.contains(lastId) && cellIds.contains(lastId) {
        fetchNotifications(notificationId: lastId)
    }
}

 

isFetched 라는 변수가 fetchedPages에서 lastId를 포함하는지를 나타내는 변수구나 라고 인식하는 것보다

만약 fetchedPages가 lastId를 포함하지 않는다면 ... (중복이 없다면..)라고 인식하는게 자연스러울 것 같습니다.