-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeViewModel.swift
94 lines (71 loc) · 3.36 KB
/
HomeViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// HomeViewModel.swift
// EAInterviewTask
//
// Created by Banka, Sathyanath (Cognizant) on 14/06/24.
//
import SwiftUI
import Combine
// MARK: - README
/*
HomeViewModel
This class serves as the view model for handling artist response model data.
Properties:
- cancellable: A set to hold AnyCancellable objects for managing Combine subscriptions.
Methods:
- getbandDetails(homeService: HomeInterface): Fetches band details using the provided home service and updates the view model's data accordingly.
- getBandItems(item: ArtistResponseModel): Processes the received artist response model, sorting it alphabetically based on artist names and band names.
README
Usage:
- cancellable: Use this property to manage Combine subscriptions. Ensure to store AnyCancellable objects returned by Combine publishers in this set to avoid memory leaks.
- getbandDetails(homeService: HomeInterface): Call this method to fetch band details using a concrete implementation of HomeInterface. It updates the view model's data and handles any errors encountered during the fetch operation.
- getBandItems(item: ArtistResponseModel): Call this method to process the received artist response model. It sorts the data alphabetically based on artist names and band names, preparing it for presentation.
*/
class HomeViewModel: Observable<ArtistResponseModel> {
var cancellable = Set<AnyCancellable>()
func getbandDetails(homeService: HomeInterface){
self.isLoading = true
homeService.getbandDetails().receive(on: DispatchQueue.main).sink { completion in
switch completion {
case .finished:
break
case .failure(let error):
self.isLoading = false
self.isShowAlert = true
self.errorMessage = error.localizedDescription
}
} receiveValue: { item in
self.isLoading = false
self.getBandItems(item: item)
}.store(in: &cancellable)
}
func groupBandsByRecordLabel(from artistResponse: [ArtistResponseModelItem]) -> [ArtistResponseModelItem] {
var groupedBands: [String: [Band]] = [:]
var separatedData: [ArtistResponseModelItem] = []
for item in artistResponse {
guard let bands = item.bands else {
continue
}
for band in bands {
// Use empty string if recordLabel is nil
let recordLabel = band.recordLabel ?? ""
if groupedBands[recordLabel] == nil {
groupedBands[recordLabel] = []
}
groupedBands[recordLabel]?.append(Band(name: band.name, recordLabel: item.name))
}
}
for (recordLabel, bands) in groupedBands {
let separatedItem = ArtistResponseModelItem(name: recordLabel, bands: bands)
separatedData.append(separatedItem)
}
return separatedData
}
func getBandItems(item: ArtistResponseModel){
let dic = groupBandsByRecordLabel(from: item)
self.data = dic.sorted { $0.name ?? "" < $1.name ?? ""}
.map { event in
ArtistResponseModelItem(name: event.name, bands: event.bands?.sorted { $0.name ?? "" < $1.name ?? ""})
}
}
}