FRE-4738: Implement mark-as-read and mark-all-read actions
- Extract NotificationItem/NotificationType to Models/Notification.swift - Create NotificationsServiceProtocol with testable service layer - Implement markAsRead(id:) and markAllAsRead() with HTTP calls - Add NotificationError enum with localized descriptions - Update NotificationsViewModel to use protocol-based service - Add 18 unit tests (12 ViewModel + 6 Model) with mock service - Update README with architecture documentation
This commit is contained in:
92
Lendair/Models/Notification.swift
Normal file
92
Lendair/Models/Notification.swift
Normal file
@@ -0,0 +1,92 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
// MARK: - Notification Item
|
||||
|
||||
struct NotificationItem: Identifiable, Equatable, Codable {
|
||||
let id: String
|
||||
let type: NotificationType
|
||||
let title: String
|
||||
let message: String
|
||||
let createdAt: Date
|
||||
var isRead: Bool
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id, type, title, message, createdAt, isRead
|
||||
}
|
||||
|
||||
init(id: String, type: NotificationType, title: String, message: String, createdAt: Date, isRead: Bool) {
|
||||
self.id = id
|
||||
self.type = type
|
||||
self.title = title
|
||||
self.message = message
|
||||
self.createdAt = createdAt
|
||||
self.isRead = isRead
|
||||
}
|
||||
|
||||
static func == (lhs: NotificationItem, rhs: NotificationItem) -> Bool {
|
||||
lhs.id == rhs.id && lhs.isRead == rhs.isRead
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Notification Type
|
||||
|
||||
enum NotificationType: String, CaseIterable, Codable {
|
||||
case loanApproved = "LOAN_APPROVED"
|
||||
case loanRejected = "LOAN_REJECTED"
|
||||
case paymentReceived = "PAYMENT_RECEIVED"
|
||||
case paymentDue = "PAYMENT_DUE"
|
||||
case newLender = "NEW_LENDER"
|
||||
case systemUpdate = "SYSTEM_UPDATE"
|
||||
|
||||
var icon: String {
|
||||
switch self {
|
||||
case .loanApproved: return "checkmark.circle.fill"
|
||||
case .loanRejected: return "xmark.circle.fill"
|
||||
case .paymentReceived: return "arrow.down.circle.fill"
|
||||
case .paymentDue: return "exclamationmark.circle.fill"
|
||||
case .newLender: return "person.circle.fill"
|
||||
case .systemUpdate: return "info.circle.fill"
|
||||
}
|
||||
}
|
||||
|
||||
var color: Color {
|
||||
switch self {
|
||||
case .loanApproved: return .green
|
||||
case .loanRejected: return .red
|
||||
case .paymentReceived: return .green
|
||||
case .paymentDue: return .orange
|
||||
case .newLender: return .blue
|
||||
case .systemUpdate: return .gray
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - List Parameters
|
||||
|
||||
struct NotificationListParams: Encodable {
|
||||
var limit: Int
|
||||
var offset: Int
|
||||
|
||||
init(limit: Int = 20, offset: Int = 0) {
|
||||
self.limit = limit
|
||||
self.offset = offset
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - API Response Types
|
||||
|
||||
struct NotificationListResponse: Decodable {
|
||||
let notifications: [NotificationItem]
|
||||
let hasMore: Bool
|
||||
}
|
||||
|
||||
struct NotificationMarkAsReadResponse: Decodable {
|
||||
let success: Bool
|
||||
let notificationId: String
|
||||
}
|
||||
|
||||
struct NotificationMarkAllReadResponse: Decodable {
|
||||
let success: Bool
|
||||
let markedCount: Int
|
||||
}
|
||||
@@ -6,59 +6,90 @@ SwiftUI implementation of the notifications feature for the Lendair iOS app.
|
||||
## Architecture
|
||||
|
||||
### MVVM Pattern
|
||||
- **View**: `NotificationsView` - Main container view
|
||||
- **ViewModel**: `NotificationsViewModel` - Manages notification state and business logic
|
||||
- **Service**: `NotificationsService` - Data layer for API communication
|
||||
- **View**: `Views/` - SwiftUI views for notification display
|
||||
- **ViewModel**: `ViewModels/` - State management and business logic
|
||||
- **Service**: `Services/` - Data layer with API communication
|
||||
- **Model**: `Models/` - Data structures and type definitions
|
||||
|
||||
### Components
|
||||
### File Structure
|
||||
```
|
||||
Lendair/
|
||||
├── Models/
|
||||
│ └── Notification.swift # NotificationItem, NotificationType, API response types
|
||||
├── Services/
|
||||
│ └── NotificationService.swift # NotificationsServiceProtocol + implementation
|
||||
├── ViewModels/
|
||||
│ └── NotificationsViewModel.swift # State management, mark-as-read actions
|
||||
├── Views/
|
||||
│ ├── NotificationsView.swift # Main notifications list screen
|
||||
│ └── NotificationRowView.swift # Individual notification row
|
||||
└── README.md
|
||||
```
|
||||
|
||||
#### NotificationsView (`Views/NotificationsView.swift`)
|
||||
## Components
|
||||
|
||||
### NotificationsView (`Views/NotificationsView.swift`)
|
||||
- Main navigation container for the notifications screen
|
||||
- Implements pull-to-refresh functionality
|
||||
- Handles empty state display
|
||||
- Provides "Mark All Read" action in toolbar
|
||||
- Integrates with navigation stack
|
||||
- Pull-to-refresh via `.refreshable`
|
||||
- Empty state when no notifications
|
||||
- "Mark All Read" toolbar button when unread count > 0
|
||||
- Tap-to-mark-as-read on individual rows
|
||||
- Swipe-to-delete (TODO: backend integration)
|
||||
|
||||
#### NotificationRowView (`Views/NotificationRowView.swift`)
|
||||
### NotificationRowView (`Views/NotificationRowView.swift`)
|
||||
- Individual notification list item
|
||||
- Displays notification icon, title, message, and timestamp
|
||||
- Shows read/unread indicator
|
||||
- Supports tap-to-mark-as-read interaction
|
||||
- Type-specific SF Symbol icon with color coding
|
||||
- Read/unread indicator (blue dot)
|
||||
- Relative timestamp display
|
||||
|
||||
#### NotificationsViewModel (`ViewModels/NotificationsViewModel.swift`)
|
||||
- Observable object managing notification state
|
||||
- Fetches notifications from service layer
|
||||
- Handles mark-as-read and mark-all-as-read operations
|
||||
- Calculates unread count for badge display
|
||||
- Implements refresh logic
|
||||
### NotificationsViewModel (`ViewModels/NotificationsViewModel.swift`)
|
||||
- `@Published notifications` — sorted by createdAt descending
|
||||
- `@Published isLoading` — loading state for UI feedback
|
||||
- `@Published error` — typed error state (NotificationError)
|
||||
- `fetchNotifications()` — loads from service
|
||||
- `markAsRead(id:)` — marks single notification, updates local state
|
||||
- `markAllAsRead()` — marks all unread, updates local state
|
||||
- `unreadCount` — computed property for badge display
|
||||
|
||||
### NotificationsService (`Services/NotificationService.swift`)
|
||||
- Protocol: `NotificationsServiceProtocol` (Sendable, testable)
|
||||
- `list(params:)` — GET `/api/notifications?limit=&offset=`
|
||||
- `markAsRead(id:)` — PATCH `/api/notifications/:id/read`
|
||||
- `markAllAsRead()` — PATCH `/api/notifications/read-all`
|
||||
- Error handling: `NotificationError` enum with localized descriptions
|
||||
- Configurable: baseURL, URLSession, authToken
|
||||
|
||||
### Models (`Models/Notification.swift`)
|
||||
- `NotificationItem` — Identifiable, Equatable, Codable
|
||||
- `NotificationType` — 6 cases with icon/color mappings
|
||||
- `NotificationListParams` — pagination parameters
|
||||
- `NotificationListResponse`, `NotificationMarkAsReadResponse`, `NotificationMarkAllReadResponse` — API response types
|
||||
|
||||
## Notification Types
|
||||
|
||||
The app supports the following notification types:
|
||||
- `LOAN_APPROVED` - Green checkmark icon
|
||||
- `LOAN_REJECTED` - Red X icon
|
||||
- `PAYMENT_RECEIVED` - Green down arrow icon
|
||||
- `PAYMENT_DUE` - Orange exclamation icon
|
||||
- `NEW_LENDER` - Blue person icon
|
||||
- `SYSTEM_UPDATE` - Gray info icon
|
||||
| Type | Icon | Color |
|
||||
|------|------|-------|
|
||||
| `LOAN_APPROVED` | checkmark.circle.fill | Green |
|
||||
| `LOAN_REJECTED` | xmark.circle.fill | Red |
|
||||
| `PAYMENT_RECEIVED` | arrow.down.circle.fill | Green |
|
||||
| `PAYMENT_DUE` | exclamationmark.circle.fill | Orange |
|
||||
| `NEW_LENDER` | person.circle.fill | Blue |
|
||||
| `SYSTEM_UPDATE` | info.circle.fill | Gray |
|
||||
|
||||
## Integration Points
|
||||
## API Endpoints
|
||||
|
||||
### tRPC Router (TODO)
|
||||
The service layer is designed to connect to the tRPC notifications router:
|
||||
```typescript
|
||||
// web/src/server/api/routers/notifications.ts
|
||||
notifications: router({
|
||||
list: protectedQuery(...),
|
||||
markAsRead: protectedMutation(...),
|
||||
markAllAsRead: protectedMutation(...),
|
||||
})
|
||||
```
|
||||
| Method | Endpoint | Description |
|
||||
|--------|----------|-------------|
|
||||
| GET | `/api/notifications?limit=&offset=` | List notifications |
|
||||
| PATCH | `/api/notifications/:id/read` | Mark single as read |
|
||||
| PATCH | `/api/notifications/read-all` | Mark all as read |
|
||||
|
||||
### API Endpoints (TODO)
|
||||
- `GET /api/notifications` - List notifications
|
||||
- `PATCH /api/notifications/:id/read` - Mark single as read
|
||||
- `PATCH /api/notifications/read-all` - Mark all as read
|
||||
## Testing
|
||||
|
||||
Tests are in `LendairTests/NotificationServiceTests.swift`:
|
||||
- 12 ViewModel tests (fetch, mark-as-read, mark-all-read, unread count, refresh, error handling)
|
||||
- 6 Model tests (icons, colors, equality, raw values, params)
|
||||
- Uses `MockNotificationsService` conforming to `NotificationsServiceProtocol`
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -69,15 +100,6 @@ NavigationStack {
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
Run the preview in Xcode to see the notification row designs:
|
||||
```swift
|
||||
#Preview {
|
||||
NotificationRowView(notification: sampleNotification)
|
||||
}
|
||||
```
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
1. **Push Notifications**: Integrate with UNUserNotificationCenter
|
||||
|
||||
134
Lendair/Services/NotificationService.swift
Normal file
134
Lendair/Services/NotificationService.swift
Normal file
@@ -0,0 +1,134 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - Service Protocol
|
||||
|
||||
protocol NotificationsServiceProtocol: Sendable {
|
||||
func list(params: NotificationListParams) async throws -> [NotificationItem]
|
||||
func markAsRead(id: String) async throws
|
||||
func markAllAsRead() async throws
|
||||
}
|
||||
|
||||
// MARK: - Default Service
|
||||
|
||||
class NotificationsService: NotificationsServiceProtocol {
|
||||
private let baseURL: URL
|
||||
private let session: URLSession
|
||||
private let authToken: String?
|
||||
|
||||
init(
|
||||
baseURL: URL = URL(string: "http://localhost:3000")!,
|
||||
session: URLSession = .shared,
|
||||
authToken: String? = nil
|
||||
) {
|
||||
self.baseURL = baseURL
|
||||
self.session = session
|
||||
self.authToken = authToken
|
||||
}
|
||||
|
||||
func list(params: NotificationListParams = NotificationListParams()) async throws -> [NotificationItem] {
|
||||
var components = URLComponents(url: baseURL.appendingPathComponent("/api/notifications"), resolvingAgainstBaseURL: true)!
|
||||
var queryItems: [URLQueryItem] = [
|
||||
URLQueryItem(name: "limit", value: String(params.limit)),
|
||||
URLQueryItem(name: "offset", value: String(params.offset))
|
||||
]
|
||||
components.queryItems = queryItems
|
||||
|
||||
let request = try buildRequest(url: components.url!)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
|
||||
let decoded = try JSONDecoder().decode(NotificationListResponse.self, from: data)
|
||||
return decoded.notifications
|
||||
}
|
||||
|
||||
func markAsRead(id: String) async throws {
|
||||
let url = baseURL.appendingPathComponent("/api/notifications/\(id)/read")
|
||||
let request = try buildRequest(url: url, method: .patch)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
|
||||
_ = try JSONDecoder().decode(NotificationMarkAsReadResponse.self, from: data)
|
||||
}
|
||||
|
||||
func markAllAsRead() async throws {
|
||||
let url = baseURL.appendingPathComponent("/api/notifications/read-all")
|
||||
let request = try buildRequest(url: url, method: .patch)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
|
||||
_ = try JSONDecoder().decode(NotificationMarkAllReadResponse.self, from: data)
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func buildRequest(url: URL, method: HTTPMethod = .get, body: Data? = nil) throws -> URLRequest {
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = method.rawValue
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
|
||||
if let token = authToken {
|
||||
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
||||
}
|
||||
|
||||
if let body = body {
|
||||
request.httpBody = body
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
private func validateResponse(_ response: URLResponse) throws {
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw NotificationError.invalidResponse
|
||||
}
|
||||
|
||||
guard (200...299).contains(httpResponse.statusCode) else {
|
||||
switch httpResponse.statusCode {
|
||||
case 401: throw NotificationError.unauthorized
|
||||
case 403: throw NotificationError.forbidden
|
||||
case 404: throw NotificationError.notFound
|
||||
case 429: throw NotificationError.rateLimited
|
||||
case 500...599: throw NotificationError.serverError(httpResponse.statusCode)
|
||||
default: throw NotificationError.httpError(httpResponse.statusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Error Types
|
||||
|
||||
enum NotificationError: LocalizedError {
|
||||
case invalidResponse
|
||||
case unauthorized
|
||||
case forbidden
|
||||
case notFound
|
||||
case rateLimited
|
||||
case serverError(Int)
|
||||
case httpError(Int)
|
||||
case decodingError(Error)
|
||||
|
||||
var errorDescription: String {
|
||||
switch self {
|
||||
case .invalidResponse: return "Invalid server response"
|
||||
case .unauthorized: return "Unauthorized — please log in again"
|
||||
case .forbidden: return "Forbidden — check permissions"
|
||||
case .notFound: return "Notification not found"
|
||||
case .rateLimited: return "Too many requests — try again shortly"
|
||||
case .serverError(let code): return "Server error (\(code))"
|
||||
case .httpError(let code): return "HTTP error (\(code))"
|
||||
case .decodingError(let error): return "Decoding error: \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - HTTP Method
|
||||
|
||||
enum HTTPMethod: String {
|
||||
case get = "GET"
|
||||
case post = "POST"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
}
|
||||
@@ -6,132 +6,64 @@ class NotificationsViewModel: ObservableObject {
|
||||
@Published var notifications: [NotificationItem] = []
|
||||
@Published var isLoading: Bool = false
|
||||
@Published var lastRefreshDate: Date?
|
||||
|
||||
private let notificationsService: NotificationsService
|
||||
|
||||
init(notificationsService: NotificationsService = NotificationsService()) {
|
||||
@Published var error: NotificationError?
|
||||
|
||||
private let notificationsService: NotificationsServiceProtocol
|
||||
|
||||
init(notificationsService: NotificationsServiceProtocol = NotificationsService()) {
|
||||
self.notificationsService = notificationsService
|
||||
}
|
||||
|
||||
|
||||
func fetchNotifications() async {
|
||||
isLoading = true
|
||||
error = nil
|
||||
defer {
|
||||
isLoading = false
|
||||
lastRefreshDate = Date()
|
||||
}
|
||||
|
||||
|
||||
do {
|
||||
let fetchedNotifications = try await notificationsService.list()
|
||||
notifications = fetchedNotifications.sorted { $0.createdAt > $1.createdAt }
|
||||
} catch let error as NotificationError {
|
||||
self.error = error
|
||||
} catch {
|
||||
print("Failed to fetch notifications: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func refresh() async {
|
||||
await fetchNotifications()
|
||||
}
|
||||
|
||||
|
||||
func markAsRead(id: String) async {
|
||||
guard let index = notifications.firstIndex(where: { $0.id == id }) else { return }
|
||||
|
||||
|
||||
do {
|
||||
try await notificationsService.markAsRead(id: id)
|
||||
notifications[index].isRead = true
|
||||
objectWillChange.send()
|
||||
} catch {
|
||||
print("Failed to mark notification as read: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func markAllAsRead() async {
|
||||
let unreadIds = notifications.filter { !$0.isRead }.map { $0.id }
|
||||
guard !unreadIds.isEmpty else { return }
|
||||
|
||||
|
||||
do {
|
||||
try await notificationsService.markAllAsRead()
|
||||
for index in notifications.indices {
|
||||
notifications[index].isRead = true
|
||||
}
|
||||
objectWillChange.send()
|
||||
} catch {
|
||||
print("Failed to mark all as read: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var unreadCount: Int {
|
||||
notifications.filter { !$0.isRead }.count
|
||||
}
|
||||
}
|
||||
|
||||
struct NotificationItem: Identifiable, Equatable {
|
||||
let id: String
|
||||
let type: NotificationType
|
||||
let title: String
|
||||
let message: String
|
||||
let createdAt: Date
|
||||
var isRead: Bool
|
||||
|
||||
static func == (lhs: NotificationItem, rhs: NotificationItem) -> Bool {
|
||||
lhs.id == rhs.id && lhs.isRead == rhs.isRead
|
||||
}
|
||||
}
|
||||
|
||||
enum NotificationType: String, CaseIterable {
|
||||
case loanApproved = "LOAN_APPROVED"
|
||||
case loanRejected = "LOAN_REJECTED"
|
||||
case paymentReceived = "PAYMENT_RECEIVED"
|
||||
case paymentDue = "PAYMENT_DUE"
|
||||
case newLender = "NEW_LENDER"
|
||||
case systemUpdate = "SYSTEM_UPDATE"
|
||||
|
||||
var icon: String {
|
||||
switch self {
|
||||
case .loanApproved:
|
||||
return "checkmark.circle.fill"
|
||||
case .loanRejected:
|
||||
return "xmark.circle.fill"
|
||||
case .paymentReceived:
|
||||
return "arrow.down.circle.fill"
|
||||
case .paymentDue:
|
||||
return "exclamationmark.circle.fill"
|
||||
case .newLender:
|
||||
return "person.circle.fill"
|
||||
case .systemUpdate:
|
||||
return "info.circle.fill"
|
||||
}
|
||||
}
|
||||
|
||||
var color: Color {
|
||||
switch self {
|
||||
case .loanApproved:
|
||||
return .green
|
||||
case .loanRejected:
|
||||
return .red
|
||||
case .paymentReceived:
|
||||
return .green
|
||||
case .paymentDue:
|
||||
return .orange
|
||||
case .newLender:
|
||||
return .blue
|
||||
case .systemUpdate:
|
||||
return .gray
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationsService {
|
||||
func list() async throws -> [NotificationItem] {
|
||||
// TODO: Connect to tRPC notifications router
|
||||
// let result = await APIClient.shared.query("notifications.list")
|
||||
return []
|
||||
}
|
||||
|
||||
func markAsRead(id: String) async throws {
|
||||
// TODO: Connect to tRPC notifications router
|
||||
// try await APIClient.shared.mutation("notifications.markAsRead", { id })
|
||||
}
|
||||
|
||||
func markAllAsRead() async throws {
|
||||
// TODO: Connect to tRPC notifications router
|
||||
// try await APIClient.shared.mutation("notifications.markAllAsRead", {})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user