I have a black holes in my code , i have finished it with help of my friend …so i need to disassemble my project step by step ,line by line to finalise my meagre knowledge
Hi ! Thank you for the response ! i have not done it by myself , i mean i do understand how does it work partially :) for example i have been trying to understand how this block of code works
I am trying to understand every line in code . Below , my attempts
It’s hard to read images. You can use codeblocks to share and format code.
Type this:
```javascript
code here
```
To get this:
code here
Did you have a specific question? Is there a specific line you don’t understand? Or are you asking someone to go through a codebase and explain each and every line? That’s a pretty big ask!
Project is not big :) i so understand a lot from project , but anyway i do not understand some blocks of code ( like above ) .
import SwiftUI
import UIKit
import Combine
struct NetworkImageView<Placeholder: View, ProgressBlock: View>: View {
// Here we describe 2 generics Placeholder and ProgressBlock and subscribe them to the view protocol so that we can put everything that conforms to the View protocol for reusability
@Environment(\.horizontalSizeClass) var horizontalSizeClass
//@Environment It is needed to understand the user settings and adapt the code to them
// In SwiftUI, we can switch between a HStack and VStack based on the horizontalSizeClass that we get from the @Environment:
@StateObject var viewModel: ImageViewModel
//@StateObject makes sure that only one instance is created per a view structure. This enables us to use @StateObject property wrappers for class type view models which eliminates the need of managing the view model’s lifecycle ourselves.
let placeholder: Placeholder
let progressBlock: ProgressBlock
//In the constants above, you can put everything that is subscribed to the View protocol
init(
//In Swift, an initializer is a special function that we use to create objects of a particular class, struct, or enum. Initializers are sometimes called constructors because they "construct" objects.
imageUrlString: String,
@ViewBuilder placeholder: () -> Placeholder,
@ViewBuilder progressBlock: () -> ProgressBlock
) {
_viewModel = .init(wrappedValue: ImageViewModel(imageUrlString: imageUrlString))
//_viewModel
// designate private variables , a view model is created, and the view subscribes to its changes,
// this is how you initialize @StateObject, this is its initializer, look towards @properyWrapper, how it works
self.placeholder = placeholder()
self.progressBlock = progressBlock()
}
var body: some View {
ZStack {
switch viewModel.state {
case .loading:
progressBlock
case .error:
placeholder
case .success(let image):
Image(uiImage: image)
.resizable()
}
}
}
}
The comments say it’s a generic which is subscribed to the View protocol. That means, it can be type which implements the View functions.
You’d want to search the rest of the code for this object name to see where this object is used. The shared snippet accesses the View calls on the data passed in, which may trigger any other parts which implement those functions.