Closed iphone201988 closed 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?
Rebase. There are conflicts.
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?
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.
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.
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.
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 ?
It doesn't look like [any CopyCellProtocol]
is possible yet sadly.
Perhaps some type eraser method is possible.
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()
}
}
Actually can probably just have the enum. Don't even need the type erasing.
Let's minimize the review to just what's supposed to be in it.