룰렛 설정
- 확률 총합 추가
This commit is contained in:
@@ -46,20 +46,19 @@ final class RouletteSettingsViewModel: ObservableObject {
|
||||
private var rouletteId = 0
|
||||
@Published var rouletteList = [GetNewRouletteResponse]()
|
||||
|
||||
@Published var totalPercentage = Float(0)
|
||||
|
||||
func addOption() {
|
||||
if (options.count >= 10) {
|
||||
return
|
||||
}
|
||||
options.append(RouletteOption(title: "", percentage: ""))
|
||||
calculateTotalPercentage()
|
||||
}
|
||||
|
||||
func deleteOption(index: Int) {
|
||||
options.remove(at: index)
|
||||
}
|
||||
|
||||
private func removeAllAndAddOptions(options: [RouletteOption]) {
|
||||
self.options.removeAll()
|
||||
self.options.append(contentsOf: options)
|
||||
calculateTotalPercentage()
|
||||
}
|
||||
|
||||
func getAllRoulette(creatorId: Int) {
|
||||
@@ -311,7 +310,9 @@ final class RouletteSettingsViewModel: ObservableObject {
|
||||
let options = roulette.items.map {
|
||||
RouletteOption(title: $0.title, percentage: String($0.percentage))
|
||||
}
|
||||
removeAllAndAddOptions(options: options)
|
||||
|
||||
self.options.removeAll()
|
||||
self.options.append(contentsOf: options)
|
||||
} else {
|
||||
self.canText = ""
|
||||
self.isActive = false
|
||||
@@ -322,5 +323,21 @@ final class RouletteSettingsViewModel: ObservableObject {
|
||||
self.addOption()
|
||||
}
|
||||
}
|
||||
|
||||
calculateTotalPercentage()
|
||||
}
|
||||
|
||||
func calculateTotalPercentage() {
|
||||
let totalPercent = options.map {
|
||||
let percentage = Float($0.percentage)
|
||||
|
||||
if let percentage = percentage {
|
||||
return percentage
|
||||
} else {
|
||||
return Float(0)
|
||||
}
|
||||
}.reduce(0, +)
|
||||
|
||||
self.totalPercentage = totalPercent
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user