Skip to content

Commit

Permalink
[FIX][Fixed refresh favorite list, Fixed character favorite button st…
Browse files Browse the repository at this point in the history
…ate]
  • Loading branch information
developersancho committed Mar 12, 2022
1 parent ffba1ca commit e7e5154
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 33 deletions.
4 changes: 4 additions & 0 deletions SwiftRorty.iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
E26BD3A127D002B0006CA548 /* GetCharacterDetail.swift in Sources */ = {isa = PBXBuildFile; fileRef = E26BD3A027D002B0006CA548 /* GetCharacterDetail.swift */; };
E26BD3A327D01629006CA548 /* DetailHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E26BD3A227D01629006CA548 /* DetailHeaderView.swift */; };
E26BD3A527D01769006CA548 /* DetailContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E26BD3A427D01769006CA548 /* DetailContentView.swift */; };
E27D2FF027DBE28E00A9B5B8 /* ViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E27D2FEF27DBE28E00A9B5B8 /* ViewExtension.swift */; };
E28A38BF27C94730005DD497 /* DtoExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E28A38BE27C94730005DD497 /* DtoExtension.swift */; };
E2900B9627D3F6080055AD96 /* CheckboxToggleStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2900B9527D3F6080055AD96 /* CheckboxToggleStyle.swift */; };
E2900B9827D3F6290055AD96 /* VerticalToggleStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2900B9727D3F6290055AD96 /* VerticalToggleStyle.swift */; };
Expand Down Expand Up @@ -165,6 +166,7 @@
E26BD3A027D002B0006CA548 /* GetCharacterDetail.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetCharacterDetail.swift; sourceTree = "<group>"; };
E26BD3A227D01629006CA548 /* DetailHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailHeaderView.swift; sourceTree = "<group>"; };
E26BD3A427D01769006CA548 /* DetailContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailContentView.swift; sourceTree = "<group>"; };
E27D2FEF27DBE28E00A9B5B8 /* ViewExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewExtension.swift; sourceTree = "<group>"; };
E28A38BE27C94730005DD497 /* DtoExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DtoExtension.swift; sourceTree = "<group>"; };
E2900B9527D3F6080055AD96 /* CheckboxToggleStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckboxToggleStyle.swift; sourceTree = "<group>"; };
E2900B9727D3F6290055AD96 /* VerticalToggleStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VerticalToggleStyle.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -637,6 +639,7 @@
E2B5A95E27D0258200C14287 /* BundleExtension.swift */,
E2900B9B27D3FF720055AD96 /* UITabBarControllerExtension.swift */,
E222E80627D6980600831125 /* LocalizedStringKeyExtension.swift */,
E27D2FEF27DBE28E00A9B5B8 /* ViewExtension.swift */,
);
path = extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -939,6 +942,7 @@
E2B5A95F27D0258200C14287 /* BundleExtension.swift in Sources */,
E26BD3A527D01769006CA548 /* DetailContentView.swift in Sources */,
E2555C0727A87252007E5266 /* GetCharacters.swift in Sources */,
E27D2FF027DBE28E00A9B5B8 /* ViewExtension.swift in Sources */,
E2AD77A127CAD8E1006565C2 /* AppModule.swift in Sources */,
E2991AB927AC76FA00BF4B9A /* Location.swift in Sources */,
E2555BFF27A87181007E5266 /* PageInfo.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,53 @@
landmarkType = "0">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "550CB415-C295-4483-8D3B-6E67B0CC799C"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftRorty.iOS/presentation/features/characters/CharactersScreen.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "28"
endingLineNumber = "28"
landmarkName = "body"
landmarkType = "24">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "5A5C66BD-1557-48CA-B681-375330B094C9"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftRorty.iOS/presentation/features/favorites/FavoritesViewModel.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "20"
endingLineNumber = "20"
landmarkName = "loadFavorites()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "6D67F390-A476-4E19-86C2-BFAA3C105EF8"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftRorty.iOS/presentation/features/favorites/FavoritesScreen.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "36"
endingLineNumber = "36"
landmarkName = "body"
landmarkType = "24">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
44 changes: 32 additions & 12 deletions SwiftRorty.iOS/data/model/dto/CharacterDto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,39 @@
import Foundation
import UIKit

//struct CharacterDto: Identifiable {
// let created: String?
// let episode: [String]?
// let gender: String?
// let id: Int
// let image: String?
// let location: Location?
// let name: String?
// let origin: Origin?
// let species: String?
// let status: Status?
// let type: String?
// let url: String?
// var isFavorite: Bool = false
//
// static func defaultDto() -> CharacterDto {
// return CharacterDto(created: nil, episode: nil, gender: nil, id: 1, image: nil, location: nil, name: "Rick Sanchez", origin: nil, species: "Human", status: Status.alive, type: nil, url: nil)
// }
//}

struct CharacterDto: Identifiable {
let created: String?
let episode: [String]?
let gender: String?
let id: Int
let image: String?
let location: Location?
let name: String?
let origin: Origin?
let species: String?
let status: Status?
let type: String?
let url: String?
var created: String? = nil
var episode: [String]? = nil
var gender: String? = nil
var id: Int = 0
var image: String? = nil
var location: Location? = nil
var name: String? = nil
var origin: Origin? = nil
var species: String? = nil
var status: Status? = nil
var type: String? = nil
var url: String? = nil
var isFavorite: Bool = false

static func defaultDto() -> CharacterDto {
Expand Down
18 changes: 18 additions & 0 deletions SwiftRorty.iOS/extensions/ViewExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// ViewExtension.swift
// SwiftRorty.iOS
//
// Created by developersancho on 11.03.2022.
//

import Foundation
import SwiftUI

extension View {
@ViewBuilder func hidden(_ shouldHide: Bool) -> some View {
switch shouldHide {
case true: self.hidden()
case false: self
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import SwiftUI
import Foundation

struct CharacterRow: View {
//@Binding
var dto: CharacterDto
var callback: ((_ favorState: Bool) -> Void)?

@State var favor: Bool = false
@State var isFavorite: Bool = false

var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 8, style: .continuous)
Expand Down Expand Up @@ -69,9 +68,9 @@ struct CharacterRow: View {
)

Button(action: {
favor = !favor
callback?(favor)
//dto.isFavorite = favor
isFavorite = dto.isFavorite
isFavorite = !isFavorite
callback?(isFavorite)
}) {
if dto.isFavorite {
Image(systemName: "heart.fill").foregroundColor(Color.red)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import SwiftUI

struct CharactersScreen: View {
// @StateObject
// private var viewModel = CharactersViewModel()
@ObservedObject
var viewModel = CharactersViewModel()

Expand All @@ -27,9 +25,7 @@ struct CharactersScreen: View {
label: {
CharacterRow(dto: character) { favorState in
Log.debug("\(favorState)")
//viewModel.charactersList.first(where: { $0.id == character.id })?.isFavorite = favorState
viewModel.updateFavor(dto: character)
//dto?.isFavorite = true
viewModel.updateFavor(dto: character, state: favorState)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,23 @@ class CharactersViewModel: ObservableObject {
@Injected
private var updateFavorite: UpdateFavorite

@Published var charactersList : [CharacterDto] = [] {
willSet {
objectWillChange.send()
}
}
// @Published var charactersList : [CharacterDto] = [] {
// willSet {
// objectWillChange.send()
// }
// }
@Published var charactersList : [CharacterDto] = []
private var cancelables = [AnyCancellable]()
private var page = 1
private var totalPages = 0
@Published var hasMorePages = false

init() {

func changeIsFavorite(for character: CharacterDto, favor: Bool) {
objectWillChange.send() // emits a change from the Store
//character.isFavorite = favor
self.$charactersList
.sink(receiveValue: { _ in self.objectWillChange.send() })
.store(in: &cancelables)
}

func loadPage() {
Expand All @@ -78,7 +83,15 @@ class CharactersViewModel: ObservableObject {
}
}

func updateFavor(dto: CharacterDto) {
func updateFavor(dto: CharacterDto, state: Bool) {
updateFavorite.invoke(dto: dto)
let index = charactersList.firstIndex(where: { $0.id == dto.id }) ?? 0
print(index)
charactersList[index].isFavorite = state
self.$charactersList
.sink(receiveValue: { [self] _ in
objectWillChange.send()
})
.store(in: &cancelables)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct FavoritesScreen: View {
@StateObject
@ObservedObject
private var viewModel = FavoritesViewModel()

var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class FavoritesViewModel: ObservableObject {
private var getFavorites: GetFavorites

@Published
var favorites: [CharacterDto] = []
var favorites = [CharacterDto]()

func loadFavorites() {
self.favorites.removeAll()
let list = getFavorites.invoke()
self.favorites.append(contentsOf: list)
}
Expand Down

0 comments on commit e7e5154

Please sign in to comment.