Skip to content

Instantly share code, notes, and snippets.

@arashkashi
Created October 27, 2022 09:49
Show Gist options
  • Save arashkashi/b9c93e310bb3ece35dc9b7a41c6fe8ea to your computer and use it in GitHub Desktop.
Save arashkashi/b9c93e310bb3ece35dc9b7a41c6fe8ea to your computer and use it in GitHub Desktop.
//
// BMW Motorrad Connected App for iOS
//
// Copyright © 2022 BMW Group. All rights reserved.
//
import Foundation
import SwiftUI
import WidgetKit
struct WidgetSmallView: View {
class ViewModel: ObservableObject {
@Published var leftVM = WidgetMeasurementView
.ViewModel(image: Image(systemName: "person"),
value: 50,
unit: "km")
@Published var rightVM = WidgetMeasurementView
.ViewModel(image: Image(systemName: "person"),
value: 50,
unit: "km")
@Published var t11Font: Font = Font.title
@Published var t23Font: Font = Font.title
@Published var t11Content: String // on designs default valueof Urban
@Published var t23Content: String // on designs default value of connected.
init(leftVM: WidgetMeasurementView.ViewModel,
rightVM: WidgetMeasurementView.ViewModel,
t11Font: Font,
t23Font: Font,
t11Content: String,
t23Content: String
) {
self.leftVM = leftVM
self.rightVM = rightVM
self.t11Font = t11Font
self.t23Font = t23Font
self.t11Content = t11Content
self.t23Content = t23Content
}
static func getMock() -> ViewModel {
ViewModel(leftVM: .getMock(), rightVM: .getMock(), t11Font: Font.title, t23Font: Font.title, t11Content: "Urban", t23Content: "Connected")
}
}
@ObservedObject var vm: ViewModel
init(vm: ViewModel) {
self.vm = vm
}
var body: some View {
VStack(spacing: 0.0) {
HStack(alignment: .top, spacing: 0.0) {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Spacer()
VStack {
Text(vm.t11Content)
.multilineTextAlignment(.trailing)
.font(vm.t11Font)
Text(vm.t23Content)
.multilineTextAlignment(.trailing)
.font(vm.t23Font)
}
}
HStack(spacing: 0.0) {
Spacer()
WidgetMeasurementView(vm: vm.leftVM)
Spacer()
WidgetMeasurementView(vm: vm.rightVM)
Spacer()
}
.padding(.vertical, 12.0)
.border(.blue)
}
}
}
struct WidgetSmallPlaceholderView: View {
var body: some View {
WidgetSmallView(vm: WidgetSmallView.ViewModel.getMock())
.redacted(reason: .placeholder)
}
}
struct WidgetSmallEntryView: View {
var entry: SmallWidgetTimelineProvider.Entry
var body: some View {
if entry.widgeSharedStorageIsInitiated {
WidgetSmallView(vm: WidgetSmallView
.ViewModel(leftVM: WidgetMeasurementView.ViewModel(image: entry.leftViewModel.image,
value: entry.leftViewModel.value,
unit: entry.leftViewModel.unit),
rightVM: WidgetMeasurementView.ViewModel(image: entry.rightViewModel.image,
value: entry.rightViewModel.value,
unit: entry.rightViewModel.unit),
t11Font: entry.t11Font,
t23Font: entry.t23Font,
t11Content: entry.t11Content,
t23Content: entry.t23Content))
} else {
WidgetSmallPlaceholderView()
}
}
}
struct WidgetSmallView_Previews: PreviewProvider {
static var previews: some View {
WidgetSmallView(vm: WidgetSmallView.ViewModel.getMock())
}
}
extension WidgetMeasurementView.ViewModel {
static func getMock() -> WidgetMeasurementView.ViewModel {
WidgetMeasurementView.ViewModel(image: Image("person"), value: 10, unit: "km")
}
}
//
// BMW Motorrad Connected App for iOS
//
// Copyright © 2022 BMW Group. All rights reserved.
//
import Foundation
import SwiftUI
struct WidgetMeasurementView: View {
class ViewModel: ObservableObject {
@Published var image: SwiftUI.Image
@Published var value: Int
@Published var unit: String
init(image: SwiftUI.Image, value: Int, unit: String) {
self.image = image
self.value = value
self.unit = unit
}
}
@ObservedObject var vm: ViewModel
init(vm: ViewModel) {
self.vm = vm
}
var body: some View {
VStack(spacing: 0.0) {
vm.image
Text("\(vm.value)")
EmptyView().frame(height: 6.0)
Text("\(vm.unit)")
}
}
}
struct WidgetMeasurementView_Previews: PreviewProvider {
static var previews: some View {
WidgetMeasurementView(vm: WidgetMeasurementView.ViewModel(image: Image(systemName: "person"),
value: 50,
unit: "km")
)
}
}
//
// BMW Motorrad Connected App for iOS
//
// Copyright © 2022 BMW Group. All rights reserved.
//
import Foundation
import SwiftUI
struct WidgetMeasurementView: View {
class ViewModel: ObservableObject {
@Published var image: SwiftUI.Image
@Published var value: Int
@Published var unit: String
init(image: SwiftUI.Image, value: Int, unit: String) {
self.image = image
self.value = value
self.unit = unit
}
}
@ObservedObject var vm: ViewModel
init(vm: ViewModel) {
self.vm = vm
}
var body: some View {
VStack(spacing: 0.0) {
vm.image
Text("\(vm.value)")
EmptyView().frame(height: 6.0)
Text("\(vm.unit)")
}
}
}
struct WidgetMeasurementView_Previews: PreviewProvider {
static var previews: some View {
WidgetMeasurementView(vm: WidgetMeasurementView.ViewModel(image: Image(systemName: "person"),
value: 50,
unit: "km")
)
}
}
@arashkashi
Copy link
Author

struct LineDivider: View {
let color: Color
let width: CGFloat
var body: some View {
Rectangle()
.fill(color)
.frame(width: width, height: 50, alignment: .leading)
.cornerRadius(1.5)
.opacity(0.5)
.edgesIgnoringSafeArea(.vertical)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment