当前位置:网站首页>Swiftui network request and data loading

Swiftui network request and data loading

2022-06-09 00:01:00 Loneliness under fireworks

Preface

SwiftUI It's just UI, Network requests and Swift It's all the same , As for the network request encapsulation, I will not repeat it one by one , If you want to see it, please see swift Use Moya Make a network request
Let me talk about the network request method , I am also a beginner , There are all kinds of loose places. Please leave a message , I will also add slowly in the follow-up study ,

1 List Data loading

Model This and Swift There's no difference , I won't let it go
Only one load is done here

1、ViewModel Code

  • @Published Modifier attribute inform Swift Keep an eye on this variable . If anything changes , All views that use this variable body Will be updated .
  • We assign the data received from the network request to model attribute , This will trigger us to step 1 The actions mentioned in .
  • init Execute once every time , If in tabBar page , Please be there. init Set limits in , Otherwise, each switch will be executed init Code
// Here I encapsulate the network request component 
import YLNetTool

class YLStudyVM: ObservableObject {
    
    @Published var model: YLStudyModel? = nil
    
    init() {
    
        getList()
    }
    
    func getList() {
    
        YLNetTool.request(target: YLStudyApi.study_list,
                          modelType: YLStudyModel.self) {
     [weak self]code, model in
            self?.model = model
            print("666")
        } failureBlock: {
     code, msg in
            print("???")
        }
    }
}

2、row In the code

This is mainly to add model, Personal feelings and Swift There's not much difference in , Negligible

struct YLStudyRow: View {
    var model: YLStudyModel.resultModel? = nil
    
    var body: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text(model?.title ?? "") 
                .font(.system(size: 14, weight: .medium))
                .padding(EdgeInsets(top: 10, leading: 15, bottom: 0, trailing: 10))
            Text(" Last learned : Do you know where your money went ?")
                .foregroundColor(RGB(102))
                .font(.system(size: 13, weight: .regular))
                .padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))
            HStack {
                Text(" Have learned :0%")
                    .foregroundColor(.red)
                    .font(.system(size: 12, weight: .regular))
                Spacer()
                Text(" Enter learning ")
                    .foregroundColor(.white)
                    .font(.system(size: 12, weight: .medium))
                    .frame(width: 70, height: 24)
                    .background(
                        LinearGradient(gradient: Gradient(colors:[RGB(255, 101, 86), RGB(249, 0, 0)]),
                                       startPoint: .top,
                                       endPoint: .bottom
                                      )
                    )
                    .cornerRadius(12)
            }
            .padding(EdgeInsets(top: 16, leading: 10, bottom: 0, trailing: 10))
        }
        .padding(EdgeInsets(top: 0, leading: 10, bottom: 10, trailing: 10))
        .background(Color.white)
        .cornerRadius(10)
    }
}

struct YLStudyRow_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            YLStudyRow()
                .previewLayout(.fixed(width: 414, height: 120))
        }
    }
}

3、 Main page get network request 、 Refresh

struct YLStudy: View { 
    @ObservedObject var viewModel = YLStudyVM()
    
    init() {
        UITableView.appearance().backgroundColor = .clear
    }
    
    var body: some View {
        List() {
            Section(header: YLStudyHeader()) {
                ForEach(0 ..< (viewModel.model?.getUserBuyMicroCourseResults.count ?? 0)) { index in
                    YLStudyRow(model: viewModel.model?.getUserBuyMicroCourseResults[index])
                        .listRowSeparator(.hidden) 
                }
            }
        }
        .listStyle(.grouped)
        .background(RGB(245))
        .navigationTitle(" Study ")
        .navigationBarTitleDisplayMode(.inline)
    }
}

struct YLStudy_Previews: PreviewProvider {
    static var previews: some View {
        YLStudy()
    }
}

Draw lessons from articles
Use SwiftUI To develop a APP - List view & Network request

原网站

版权声明
本文为[Loneliness under fireworks]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/159/202206082329074861.html