twodayslate / NetUtils

WHOIS, DNS, ping, view source, and more!
https://itunes.apple.com/us/app/netutils/id1434360325?mt=8
Other
34 stars 6 forks source link

Host section model return generic branch #127

Closed iphone201988 closed 1 year ago

twodayslate commented 1 year ago

Let's minimize the review to just what's supposed to be in it.

twodayslate commented 1 year ago

I assume this is for https://github.com/twodayslate/NetUtils/issues/125

This isn't what I want exactly. I want the content to be more generic https://github.com/twodayslate/NetUtils/blob/master/ec3730/Models/HostSectionModel.swift#L8

Is [any CopyCellProtocol] possible?

iphone201988 commented 1 year ago

![01] (https://user-images.githubusercontent.com/3666723/209292143-a8bd8b78-9045-443c-ba36-e301dfc6973a.png) ![02] (https://user-images.githubusercontent.com/3666723/209292152-1f64a9d0-46a2-4143-8011-1839ed277e66.png) ![03] (https://user-images.githubusercontent.com/3666723/209292156-238e103a-7010-4a7b-8c85-0b9c9bd3175e.png) ![04] (https://user-images.githubusercontent.com/3666723/209292161-1132b014-4aaa-4d9a-a173-2dfc8a5c348c.png)

twodayslate commented 1 year ago

Rebase. There are conflicts.

twodayslate commented 1 year ago

What are you trying to show me? What you have now doesn't seem ideal. How does it help us show images instead of text for certain cells?

iphone201988 commented 1 year ago

I am sorry, I don't understand the exact requirement. Can you please rephrase the requirements in your words, so I can understand the exact requirements and make changes accordingly.

Because as per last changes, CopyCellProtocol is generic type ( it can be handle any data type ).

Regards.

twodayslate commented 1 year ago

The type of contents is CopyCellView. I want it to be more generic. Can it be CopyCellProtocol instead?

Also you need to rebase since there is a merge conflict.

iphone201988 commented 1 year ago

The type of contents is CopyCellView. I want it to be more generic. Can it be CopyCellProtocol instead?

Also you need to rebase since there is a merge conflict.

Ok, I'll make changes accordingly.

iphone201988 commented 1 year ago

CopyCellView (which is struct datatype) is inheriting CopyCellProtocol. So it was earlier with it. Now I made exact copy of CopyCellProtocol with name of ContentToShareProtocol. So technically if we use this then it is exact same CopyCellProtocol and I used it earlier which you asked me to make it generic, so if we do with ContentToShareProtocol then also it will be generic reason is it is copy of CopyCellProtcol. ContentToShareProtocol can be used via computed property or generic member function. I hope this explains the scenario well enough this time. If any confusion please ask me. So please suggest me if I can use ContentToShareProtocol like last time to full-fill the requirements or not ?

twodayslate commented 1 year ago

It doesn't look like [any CopyCellProtocol] is possible yet sadly.

Perhaps some type eraser method is possible.

twodayslate commented 1 year ago

Thoughts on something like this?

//
//  ContentView.swift
//  generic-test
//
//  Created by Zachary Gorak on 12/28/22.
//

import SwiftUI

protocol MyProtocol: View, Hashable, Identifiable {
    var data: TypeErasing { get }
}

protocol TypeErasing {
    var data: Any { get }
}

struct TypeEraser<V: Codable>: TypeErasing {
    let orinal: V
    var data: Any {
        return self.orinal
    }
}

enum ProtocolViewTypes: View {
    case row(title: String, content: String)

    var body: some View {
        switch self {
        case .row(let title, let content):
            HStack {
                Text(title)
                    .bold()
                Spacer()
                Text(content).foregroundColor(.gray)
            }
            .padding()
        }
    }

    var data: Codable {
        switch self {
        case .row(let title, let content):
            return "{\(title): \(content)}"
        }
    }
}

struct ProtocolView: MyProtocol {

    static func == (lhs: ProtocolView, rhs: ProtocolView) -> Bool {
        lhs.id == rhs.id
    }

    var id: String {
        "\(data.data)"
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }

    var data: TypeErasing

    var wrappedView: AnyView

    init<V>(_ wrappedView: some View, shareable: V) where V: Codable {
        self.wrappedView = AnyView(wrappedView)
        self.data = TypeEraser(orinal: shareable)
    }

    init<V>(share: V, content: () -> some View) where V: Codable {
        self.init(content(), shareable: share)
    }

    init(_ type: ProtocolViewTypes) {
        self.init(type, shareable: type.data)
    }

    @State var presentSheet = false

    var body: some View{
        wrappedView
            .contextMenu {
                Button {
                    self.presentSheet.toggle()
                } label: {
                    Text("share")
                }
            }
            .sheet(isPresented: $presentSheet) {
                let string: String = "Sharing \(data.data) which is a \(type(of: data.data))"
                 Text(string)
            }
    }
}

class Model: ObservableObject {
    @Published var items = [ProtocolView]()
}

struct ContentView: View {
    @StateObject var model = Model()

    var body: some View {
        ScrollView {
            VStack {
                ForEach(model.items) { item in
                    item
                }
            }
        }
        .task {
            Task {
                for i in 0..<5 {
                    try await Task.sleep(for: .seconds(2))
                    model.items.append(ProtocolView(Text("Testing item \(i)"), shareable: "\(i)"))
                    model.items.append(
                        ProtocolView(share: "Test \(i)") {
                            Text("Test \(i)")
                        })
                    model.items.append(ProtocolView(.row(title: "Title", content: "\(i)")))
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
twodayslate commented 1 year ago

Actually can probably just have the enum. Don't even need the type erasing.