ContactTrickRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import Contacts
  2. import ContactsUI
  3. import SwiftUI
  4. import Swinject
  5. extension ContactTrick {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. @State private var contactStore = CNContactStore()
  10. @State private var authorization = CNContactStore.authorizationStatus(for: .contacts)
  11. @State private var contactTrickEntries = [ContactTrickEntry]()
  12. var body: some View {
  13. Form {
  14. switch authorization {
  15. case .authorized:
  16. Section(header: Text("Contacts")) {
  17. list
  18. addButton
  19. }
  20. Section(
  21. header: state.changed ?
  22. Text("Don't forget to save your changes.")
  23. .frame(maxWidth: .infinity, alignment: .center)
  24. .foregroundStyle(.primary) : nil
  25. ) {
  26. HStack {
  27. if state.syncInProgress {
  28. ProgressView().padding(.trailing, 10)
  29. }
  30. Button { state.save() }
  31. label: {
  32. Text(state.syncInProgress ? "Saving..." : "Save")
  33. }
  34. .disabled(state.syncInProgress || !state.changed)
  35. .frame(maxWidth: .infinity, alignment: .center)
  36. }
  37. }
  38. case .notDetermined:
  39. Section {
  40. Text(
  41. "Trio needs access to your contacts for this feature to work"
  42. )
  43. }
  44. Section {
  45. Button(action: onRequestContactsAccess) {
  46. Text("Grant Trio access to contacts")
  47. }
  48. }
  49. case .denied:
  50. Section {
  51. Text(
  52. "Access to contacts denied"
  53. )
  54. }
  55. case .restricted:
  56. Section {
  57. Text(
  58. "Access to contacts is restricted (parental control?)"
  59. )
  60. }
  61. case .limited:
  62. Section {
  63. Text(
  64. "Access to contacts is limited. Trio needs full access to contacts for this feature to work"
  65. )
  66. }
  67. @unknown default:
  68. Section {
  69. Text(
  70. "Access to contacts - unknown state"
  71. )
  72. }
  73. }
  74. Section {}
  75. footer: {
  76. Text(
  77. "A Contact Image can be used to get live updates from Trio to your Apple Watch Contact complication and/or your iPhone Contact widget."
  78. )
  79. .frame(maxWidth: .infinity, alignment: .center)
  80. }
  81. }
  82. .dynamicTypeSize(...DynamicTypeSize.xxLarge)
  83. .onAppear(perform: configureView)
  84. .navigationTitle("Contact Image")
  85. .navigationBarTitleDisplayMode(.automatic)
  86. .navigationBarItems(
  87. trailing: EditButton()
  88. )
  89. }
  90. private func contactSettings(for index: Int) -> some View {
  91. EntryView(entry: Binding(
  92. get: { state.items[index].entry },
  93. set: { newValue in state.update(index, newValue) }
  94. ), previewState: previewState)
  95. }
  96. var previewState: ContactTrickState {
  97. let units = state.units
  98. return ContactTrickState(
  99. glucose: units == .mmolL ? "6,8" : "127",
  100. trend: "↗︎",
  101. delta: units == .mmolL ? "+0,3" : "+7",
  102. lastLoopDate: .now,
  103. iob: 6.1,
  104. iobText: "6,1",
  105. cob: 27.0,
  106. cobText: "27",
  107. eventualBG: units == .mmolL ? "8,9" : "163",
  108. maxIOB: 12.0,
  109. maxCOB: 120.0
  110. )
  111. }
  112. private var list: some View {
  113. List {
  114. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  115. NavigationLink(destination: contactSettings(for: index)) {
  116. EntryListView(entry: .constant(item.entry), index: .constant(index), previewState: previewState)
  117. }
  118. .moveDisabled(true)
  119. }
  120. .onDelete(perform: onDelete)
  121. }
  122. }
  123. private var addButton: some View {
  124. AnyView(Button(action: onAdd) { Text("Add") })
  125. }
  126. func onAdd() {
  127. state.add()
  128. }
  129. func onRequestContactsAccess() {
  130. contactStore.requestAccess(for: .contacts) { _, _ in
  131. DispatchQueue.main.async {
  132. authorization = CNContactStore.authorizationStatus(for: .contacts)
  133. }
  134. }
  135. }
  136. private func onDelete(offsets: IndexSet) {
  137. state.remove(atOffsets: offsets)
  138. }
  139. }
  140. struct EntryListView: View {
  141. @Binding var entry: ContactTrickEntry
  142. @Binding var index: Int
  143. @State private var refreshKey = UUID()
  144. let previewState: ContactTrickState
  145. var body: some View {
  146. HStack {
  147. VStack(alignment: .leading) {
  148. GeometryReader { geometry in
  149. ZStack {
  150. Circle()
  151. .fill(entry.darkMode ? .black : .white)
  152. .foregroundColor(.white)
  153. Image(uiImage: ContactPicture.getImage(contact: entry, state: previewState))
  154. .resizable()
  155. .aspectRatio(1, contentMode: .fit)
  156. .frame(width: geometry.size.height, height: geometry.size.height)
  157. .clipShape(Circle())
  158. Circle()
  159. .stroke(lineWidth: 2)
  160. .foregroundColor(.white)
  161. }
  162. .frame(width: geometry.size.height, height: geometry.size.height)
  163. }
  164. }
  165. .fixedSize(horizontal: true, vertical: false)
  166. .padding(.horizontal, 30)
  167. Spacer()
  168. VStack {
  169. Text("Contact: Trio \(index + 1)").bold()
  170. // HStack {
  171. // Text("Layout: \(entry.layout.displayName)")
  172. // Text("\(entry.ring.displayName)")
  173. // if entry.layout == .single {
  174. // Text("\(entry.primary.displayName)")
  175. // }
  176. // Text("\(entry.top.displayName), \(entry.bottom.displayName)")
  177. // }.foregroundStyle(.secondary)
  178. // HStack {
  179. // Text("Font Size \(entry.fontSize.displayName)")
  180. // Text("Font Width \(entry.fontWidth.displayName)")
  181. // Text("Font Weight \(entry.fontWeight.displayName)")
  182. // }.foregroundStyle(.secondary)
  183. }
  184. }
  185. .frame(maxWidth: .infinity)
  186. }
  187. }
  188. struct EntryView: View {
  189. @Binding var entry: ContactTrickEntry
  190. @State private var availableFonts: [String]? = nil
  191. let previewState: ContactTrickState
  192. private let ringWidths: [Int] = [5, 10, 15]
  193. private let ringGaps: [Int] = [0, 2, 4]
  194. var body: some View {
  195. VStack {
  196. Section {
  197. HStack {
  198. ZStack {
  199. Circle()
  200. .fill(entry.darkMode ? .black : .white)
  201. Image(uiImage: ContactPicture.getImage(contact: entry, state: previewState))
  202. .resizable()
  203. .aspectRatio(1, contentMode: .fit)
  204. .frame(width: 64, height: 64)
  205. .clipShape(Circle())
  206. Circle()
  207. .stroke(lineWidth: 2)
  208. .foregroundColor(.white)
  209. }
  210. .frame(width: 64, height: 64)
  211. }
  212. }
  213. Form {
  214. Section {
  215. Picker(
  216. selection: $entry.layout,
  217. label: Text("Layout")
  218. ) {
  219. ForEach(ContactTrickLayout.allCases, id: \.self) { layout in
  220. Text(layout.displayName).tag(layout)
  221. }
  222. }
  223. }
  224. layoutSpecificSection
  225. Section(header: Text("Ring")) {
  226. Picker(
  227. selection: $entry.ring,
  228. label: Text("Outer")
  229. ) {
  230. ForEach(ContactTrickLargeRing.allCases, id: \.self) { ring in
  231. Text(ring.displayName).tag(ring)
  232. }
  233. }
  234. if entry.ring != .none {
  235. Picker(
  236. selection: $entry.ringWidth,
  237. label: Text("Width")
  238. ) {
  239. ForEach(
  240. [
  241. ContactTrickEntry.RingWidth.tiny,
  242. ContactTrickEntry.RingWidth.small,
  243. ContactTrickEntry.RingWidth.regular,
  244. ContactTrickEntry.RingWidth.medium,
  245. ContactTrickEntry.RingWidth.large
  246. ],
  247. id: \.self
  248. ) { width in
  249. Text(width.displayName).tag(width)
  250. }
  251. }
  252. Picker(
  253. selection: $entry.ringGap,
  254. label: Text("Gap")
  255. ) {
  256. ForEach(
  257. [
  258. ContactTrickEntry.RingGap.tiny,
  259. ContactTrickEntry.RingGap.small,
  260. ContactTrickEntry.RingGap.regular,
  261. ContactTrickEntry.RingGap.medium,
  262. ContactTrickEntry.RingGap.large
  263. ],
  264. id: \.self
  265. ) { gap in
  266. Text(gap.displayName).tag(gap)
  267. }
  268. }
  269. }
  270. }
  271. Section(header: Text("Font")) {
  272. Picker(
  273. selection: $entry.fontSize,
  274. label: Text("Size")
  275. ) {
  276. ForEach(
  277. [
  278. ContactTrickEntry.FontSize.tiny,
  279. ContactTrickEntry.FontSize.small,
  280. ContactTrickEntry.FontSize.regular,
  281. ContactTrickEntry.FontSize.large
  282. ],
  283. id: \.self
  284. ) { size in
  285. Text(size.displayName).tag(size)
  286. }
  287. }
  288. Picker(
  289. selection: $entry.secondaryFontSize,
  290. label: Text("Secondary size")
  291. ) {
  292. ForEach(
  293. [
  294. ContactTrickEntry.FontSize.tiny,
  295. ContactTrickEntry.FontSize.small,
  296. ContactTrickEntry.FontSize.regular,
  297. ContactTrickEntry.FontSize.large
  298. ],
  299. id: \.self
  300. ) { size in
  301. Text(size.displayName).tag(size)
  302. }
  303. }
  304. Picker(
  305. selection: $entry.fontWidth,
  306. label: Text("Width")
  307. ) {
  308. ForEach(
  309. [Font.Width.standard, Font.Width.condensed, Font.Width.expanded],
  310. id: \.self
  311. ) { width in
  312. Text(width.displayName).tag(width)
  313. }
  314. }
  315. Picker(
  316. selection: $entry.fontWeight,
  317. label: Text("Weight")
  318. ) {
  319. ForEach(
  320. [Font.Weight.light, Font.Weight.regular, Font.Weight.medium, Font.Weight.bold, Font.Weight.black],
  321. id: \.self
  322. ) { weight in
  323. Text(weight.displayName).tag(weight)
  324. }
  325. }
  326. }
  327. Section {
  328. Toggle("Dark mode", isOn: $entry.darkMode)
  329. }
  330. }
  331. }
  332. }
  333. private var layoutSpecificSection: some View {
  334. Section {
  335. if entry.layout == .single {
  336. Picker(
  337. selection: $entry.primary,
  338. label: Text("Primary")
  339. ) {
  340. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  341. Text(value.displayName).tag(value)
  342. }
  343. }
  344. Picker(
  345. selection: $entry.top,
  346. label: Text("Top")
  347. ) {
  348. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  349. Text(value.displayName).tag(value)
  350. }
  351. }
  352. Picker(
  353. selection: $entry.bottom,
  354. label: Text("Bottom")
  355. ) {
  356. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  357. Text(value.displayName).tag(value)
  358. }
  359. }
  360. } else if entry.layout == .split {
  361. Picker(
  362. selection: $entry.top,
  363. label: Text("Top")
  364. ) {
  365. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  366. Text(value.displayName).tag(value)
  367. }
  368. }
  369. Picker(
  370. selection: $entry.bottom,
  371. label: Text("Bottom")
  372. ) {
  373. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  374. Text(value.displayName).tag(value)
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }