kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Reactive Programming open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Reactive Programming

v0.26.1

3.1.4

Atlas

12.0.0

axios

v0.26.1

RxJava

3.1.4

fetch

RxSwift

Atlas

ReactiveCocoa

12.0.0

Popular Libraries in Reactive Programming

Trending New libraries in Reactive Programming

Top Authors in Reactive Programming

1

59 Libraries

1402

2

53 Libraries

11846

3

36 Libraries

22896

4

27 Libraries

1723

5

26 Libraries

1089

6

23 Libraries

5125

7

20 Libraries

135294

8

20 Libraries

954

9

20 Libraries

21648

10

19 Libraries

3883

1

59 Libraries

1402

2

53 Libraries

11846

3

36 Libraries

22896

4

27 Libraries

1723

5

26 Libraries

1089

6

23 Libraries

5125

7

20 Libraries

135294

8

20 Libraries

954

9

20 Libraries

21648

10

19 Libraries

3883

Trending Kits in Reactive Programming

No Trending Kits are available at this moment for Reactive Programming

Trending Discussions on Reactive Programming

    Swift custom toggle style freezes UI
    How to return a Reactive Flux that contains a Reactive Mono and Flux?
    Setting an @Published var to nil after no more events are received
    Is it possible for a non reactive Client (RestTemplate) to consume a Reactive REST API (WebFlux)
    How to re-render UI in response to computed property buried in a nested class?
    Scatter & Gather using Spring Webclient
    Return a String value when method return type is Mono<String>
    In reactive programming, is the sequence of doOnNext calls guaranteed?
    Why does the `R` pipe operator `|>` not work in the reactive programming using Shiny?
    Spring Webflux vs Rsocket

QUESTION

Swift custom toggle style freezes UI

Asked 2022-Feb-10 at 12:20

I would like to create a view with a list of checkboxes which I can select independently and keep track of these changes in an array. I have implemented some code to try and achieve this however Im experiencing weird behaviour where the screen freezes as a result of this code. Im still new to SwiftUI and reactive programming so I'm not sure if I am missusing reactive components.

When selecting the checkboxes the screen freezes and from there onwards I'm unable to click anything on the screen.

Entity

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17

MyToggleStyle1

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17struct MyToggleStyle1: ToggleStyle {
18    func makeBody(configuration: Configuration) -> some View {
19        return HStack {
20            configuration.label
21            Image(systemName: configuration.isOn ? "checkmark.square" : "square")
22                .resizable()
23                .foregroundColor(configuration.isOn ? .green : .red)
24                .frame(width: 22, height: 22)
25                .onTapGesture { configuration.isOn.toggle() }
26        }
27    }
28}
29

EntitySelectorView

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17struct MyToggleStyle1: ToggleStyle {
18    func makeBody(configuration: Configuration) -> some View {
19        return HStack {
20            configuration.label
21            Image(systemName: configuration.isOn ? "checkmark.square" : "square")
22                .resizable()
23                .foregroundColor(configuration.isOn ? .green : .red)
24                .frame(width: 22, height: 22)
25                .onTapGesture { configuration.isOn.toggle() }
26        }
27    }
28}
29struct EntitySelectorView: View {
30    @State var customArray: [Entity] = [Entity(name: "All entities", enabled: false),
31                                          Entity(name: "Boston Merchandising", enabled: false),
32                                             Entity(name: "Canns & Co", enabled: false),
33                                             Entity(name: "Sterling Auto Spares", enabled: false),
34                                             Entity(name: "Compendia Bioscience Life Technologies", enabled: true),
35                                             Entity(name: "Boston Consulting Group", enabled: false)]
36
37    var body: some View {
38        VStack(spacing: 0) {
39            ScrollView(.vertical, showsIndicators: false) {
40                    VStack{
41                        ForEach($customArray) { $entity in
42                            HStack {
43                                Toggle(isOn: $entity.enabled) {
44                                    Text(entity.name)
45                                        .foregroundColor(Colors.darkTextColor.color)
46                                        .font(.system(size: 16, weight: entity.name == "All entities" ? .bold : .regular, design: .default))
47                                }
48                                .toggleStyle(MyToggleStyle1())
49                                
50                                Spacer()
51                            }.padding(.top, 12)
52                        }
53                        
54                        Spacer()
55                    }
56                }
57
58            Spacer()
59        }.padding()
60    }
61}
62
63

enter image description here

ANSWER

Answered 2022-Feb-10 at 12:20

It is about model: 1st - it must be value, and 2nd - equatable should depend on any changed property.

Here is fixed part. Tested with Xcode 13.2 / iOS 15.2

demo

copy icondownload icon

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17struct MyToggleStyle1: ToggleStyle {
18    func makeBody(configuration: Configuration) -> some View {
19        return HStack {
20            configuration.label
21            Image(systemName: configuration.isOn ? "checkmark.square" : "square")
22                .resizable()
23                .foregroundColor(configuration.isOn ? .green : .red)
24                .frame(width: 22, height: 22)
25                .onTapGesture { configuration.isOn.toggle() }
26        }
27    }
28}
29struct EntitySelectorView: View {
30    @State var customArray: [Entity] = [Entity(name: "All entities", enabled: false),
31                                          Entity(name: "Boston Merchandising", enabled: false),
32                                             Entity(name: "Canns & Co", enabled: false),
33                                             Entity(name: "Sterling Auto Spares", enabled: false),
34                                             Entity(name: "Compendia Bioscience Life Technologies", enabled: true),
35                                             Entity(name: "Boston Consulting Group", enabled: false)]
36
37    var body: some View {
38        VStack(spacing: 0) {
39            ScrollView(.vertical, showsIndicators: false) {
40                    VStack{
41                        ForEach($customArray) { $entity in
42                            HStack {
43                                Toggle(isOn: $entity.enabled) {
44                                    Text(entity.name)
45                                        .foregroundColor(Colors.darkTextColor.color)
46                                        .font(.system(size: 16, weight: entity.name == "All entities" ? .bold : .regular, design: .default))
47                                }
48                                .toggleStyle(MyToggleStyle1())
49                                
50                                Spacer()
51                            }.padding(.top, 12)
52                        }
53                        
54                        Spacer()
55                    }
56                }
57
58            Spacer()
59        }.padding()
60    }
61}
62
63struct Entity: Identifiable, Codable, Equatable {   // << here !!
64
65    var id = UUID()
66    let name: String
67    var enabled: Bool
68
69    init(name: String, enabled: Bool) {
70        self.name = name
71        self.enabled = enabled
72    }
73
74    static func == (lhs: Entity, rhs: Entity) -> Bool {
75        lhs.id == rhs.id && lhs.enabled == rhs.enabled   // << here !!
76    }
77}
78

Source https://stackoverflow.com/questions/71063062

Community Discussions contain sources that include Stack Exchange Network

    Swift custom toggle style freezes UI
    How to return a Reactive Flux that contains a Reactive Mono and Flux?
    Setting an @Published var to nil after no more events are received
    Is it possible for a non reactive Client (RestTemplate) to consume a Reactive REST API (WebFlux)
    How to re-render UI in response to computed property buried in a nested class?
    Scatter & Gather using Spring Webclient
    Return a String value when method return type is Mono<String>
    In reactive programming, is the sequence of doOnNext calls guaranteed?
    Why does the `R` pipe operator `|>` not work in the reactive programming using Shiny?
    Spring Webflux vs Rsocket

QUESTION

Swift custom toggle style freezes UI

Asked 2022-Feb-10 at 12:20

I would like to create a view with a list of checkboxes which I can select independently and keep track of these changes in an array. I have implemented some code to try and achieve this however Im experiencing weird behaviour where the screen freezes as a result of this code. Im still new to SwiftUI and reactive programming so I'm not sure if I am missusing reactive components.

When selecting the checkboxes the screen freezes and from there onwards I'm unable to click anything on the screen.

Entity

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17

MyToggleStyle1

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17struct MyToggleStyle1: ToggleStyle {
18    func makeBody(configuration: Configuration) -> some View {
19        return HStack {
20            configuration.label
21            Image(systemName: configuration.isOn ? "checkmark.square" : "square")
22                .resizable()
23                .foregroundColor(configuration.isOn ? .green : .red)
24                .frame(width: 22, height: 22)
25                .onTapGesture { configuration.isOn.toggle() }
26        }
27    }
28}
29

EntitySelectorView

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17struct MyToggleStyle1: ToggleStyle {
18    func makeBody(configuration: Configuration) -> some View {
19        return HStack {
20            configuration.label
21            Image(systemName: configuration.isOn ? "checkmark.square" : "square")
22                .resizable()
23                .foregroundColor(configuration.isOn ? .green : .red)
24                .frame(width: 22, height: 22)
25                .onTapGesture { configuration.isOn.toggle() }
26        }
27    }
28}
29struct EntitySelectorView: View {
30    @State var customArray: [Entity] = [Entity(name: "All entities", enabled: false),
31                                          Entity(name: "Boston Merchandising", enabled: false),
32                                             Entity(name: "Canns & Co", enabled: false),
33                                             Entity(name: "Sterling Auto Spares", enabled: false),
34                                             Entity(name: "Compendia Bioscience Life Technologies", enabled: true),
35                                             Entity(name: "Boston Consulting Group", enabled: false)]
36
37    var body: some View {
38        VStack(spacing: 0) {
39            ScrollView(.vertical, showsIndicators: false) {
40                    VStack{
41                        ForEach($customArray) { $entity in
42                            HStack {
43                                Toggle(isOn: $entity.enabled) {
44                                    Text(entity.name)
45                                        .foregroundColor(Colors.darkTextColor.color)
46                                        .font(.system(size: 16, weight: entity.name == "All entities" ? .bold : .regular, design: .default))
47                                }
48                                .toggleStyle(MyToggleStyle1())
49                                
50                                Spacer()
51                            }.padding(.top, 12)
52                        }
53                        
54                        Spacer()
55                    }
56                }
57
58            Spacer()
59        }.padding()
60    }
61}
62
63

enter image description here

ANSWER

Answered 2022-Feb-10 at 12:20

It is about model: 1st - it must be value, and 2nd - equatable should depend on any changed property.

Here is fixed part. Tested with Xcode 13.2 / iOS 15.2

demo

copy icondownload icon

1class Entity: Identifiable, Codable, Equatable {
2    
3    var id = UUID()
4    let name: String
5    var enabled: Bool
6    
7    init(name: String, enabled: Bool) {
8        self.name = name
9        self.enabled = enabled
10    }
11    
12    static func == (lhs: Entity, rhs: Entity) -> Bool {
13        lhs.id == rhs.id
14    }
15}
16
17struct MyToggleStyle1: ToggleStyle {
18    func makeBody(configuration: Configuration) -> some View {
19        return HStack {
20            configuration.label
21            Image(systemName: configuration.isOn ? "checkmark.square" : "square")
22                .resizable()
23                .foregroundColor(configuration.isOn ? .green : .red)
24                .frame(width: 22, height: 22)
25                .onTapGesture { configuration.isOn.toggle() }
26        }
27    }
28}
29struct EntitySelectorView: View {
30    @State var customArray: [Entity] = [Entity(name: "All entities", enabled: false),
31                                          Entity(name: "Boston Merchandising", enabled: false),
32                                             Entity(name: "Canns & Co", enabled: false),
33                                             Entity(name: "Sterling Auto Spares", enabled: false),
34                                             Entity(name: "Compendia Bioscience Life Technologies", enabled: true),
35                                             Entity(name: "Boston Consulting Group", enabled: false)]
36
37    var body: some View {
38        VStack(spacing: 0) {
39            ScrollView(.vertical, showsIndicators: false) {
40                    VStack{
41                        ForEach($customArray) { $entity in
42                            HStack {
43                                Toggle(isOn: $entity.enabled) {
44                                    Text(entity.name)
45                                        .foregroundColor(Colors.darkTextColor.color)
46                                        .font(.system(size: 16, weight: entity.name == "All entities" ? .bold : .regular, design: .default))
47                                }
48                                .toggleStyle(MyToggleStyle1())
49                                
50                                Spacer()
51                            }.padding(.top, 12)
52                        }
53                        
54                        Spacer()
55                    }
56                }
57
58            Spacer()
59        }.padding()
60    }
61}
62
63struct Entity: Identifiable, Codable, Equatable {   // << here !!
64
65    var id = UUID()
66    let name: String
67    var enabled: Bool
68
69    init(name: String, enabled: Bool) {
70        self.name = name
71        self.enabled = enabled
72    }
73
74    static func == (lhs: Entity, rhs: Entity) -> Bool {
75        lhs.id == rhs.id && lhs.enabled == rhs.enabled   // << here !!
76    }
77}
78

Source https://stackoverflow.com/questions/71063062

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Reactive Programming

Tutorials and Learning Resources are not available at this moment for Reactive Programming

Share this Page

share link

Get latest updates on Reactive Programming

  • © 2022 Open Weaver Inc.