ContactTrickRootView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. Text(
  148. NSLocalizedString("Contact", comment: "") + ": " + "Trio \(index + 1)"
  149. )
  150. .font(.body)
  151. .minimumScaleFactor(0.5)
  152. .lineLimit(1)
  153. Spacer()
  154. VStack {
  155. GeometryReader { geometry in
  156. ZStack {
  157. Circle()
  158. .fill(entry.darkMode ? .black : .white)
  159. .foregroundColor(.white)
  160. Image(uiImage: ContactPicture.getImage(contact: entry, state: previewState))
  161. .resizable()
  162. .aspectRatio(1, contentMode: .fit)
  163. .frame(width: geometry.size.height, height: geometry.size.height)
  164. .clipShape(Circle())
  165. Circle()
  166. .stroke(lineWidth: 2)
  167. .foregroundColor(.white)
  168. }
  169. .frame(width: geometry.size.height, height: geometry.size.height)
  170. }
  171. }
  172. .fixedSize(horizontal: true, vertical: false)
  173. .padding(.horizontal, 30)
  174. }
  175. .frame(maxWidth: .infinity)
  176. }
  177. }
  178. struct EntryView: View {
  179. @Binding var entry: ContactTrickEntry
  180. @State private var availableFonts: [String]? = nil
  181. let previewState: ContactTrickState
  182. private let ringWidths: [Int] = [5, 10, 15]
  183. private let ringGaps: [Int] = [0, 2, 4]
  184. var body: some View {
  185. VStack {
  186. Section {
  187. HStack {
  188. ZStack {
  189. Circle()
  190. .fill(entry.darkMode ? .black : .white)
  191. Image(uiImage: ContactPicture.getImage(contact: entry, state: previewState))
  192. .resizable()
  193. .aspectRatio(1, contentMode: .fit)
  194. .frame(width: 64, height: 64)
  195. .clipShape(Circle())
  196. Circle()
  197. .stroke(lineWidth: 2)
  198. .foregroundColor(.white)
  199. }
  200. .frame(width: 64, height: 64)
  201. }
  202. }
  203. Form {
  204. Section {
  205. Picker(
  206. selection: $entry.layout,
  207. label: Text("Layout")
  208. ) {
  209. ForEach(ContactTrickLayout.allCases, id: \.self) { layout in
  210. Text(layout.displayName).tag(layout)
  211. }
  212. }
  213. }
  214. layoutSpecificSection
  215. Section(header: Text("Ring")) {
  216. Picker(
  217. selection: $entry.ring1,
  218. label: Text("Outer")
  219. ) {
  220. ForEach(ContactTrickLargeRing.allCases, id: \.self) { ring in
  221. Text(ring.displayName).tag(ring)
  222. }
  223. }
  224. Picker(
  225. selection: $entry.ringWidth,
  226. label: Text("Width")
  227. ) {
  228. ForEach(ringWidths, id: \.self) { width in
  229. Text("\(width)").tag(width)
  230. }
  231. }
  232. Picker(
  233. selection: $entry.ringGap,
  234. label: Text("Gap")
  235. ) {
  236. ForEach(ringGaps, id: \.self) { gap in
  237. Text("\(gap)").tag(gap)
  238. }
  239. }
  240. }
  241. Section(header: Text("Font")) {
  242. Picker(
  243. selection: $entry.fontSize,
  244. label: Text("Size")
  245. ) {
  246. ForEach(
  247. [
  248. ContactTrickEntry.FontSize.tiny,
  249. ContactTrickEntry.FontSize.small,
  250. ContactTrickEntry.FontSize.regular,
  251. ContactTrickEntry.FontSize.large
  252. ],
  253. id: \.self
  254. ) { size in
  255. Text("\(size)").tag(size)
  256. }
  257. }
  258. Picker(
  259. selection: $entry.secondaryFontSize,
  260. label: Text("Secondary size")
  261. ) {
  262. ForEach(
  263. [
  264. ContactTrickEntry.FontSize.tiny,
  265. ContactTrickEntry.FontSize.small,
  266. ContactTrickEntry.FontSize.regular,
  267. ContactTrickEntry.FontSize.large
  268. ],
  269. id: \.self
  270. ) { size in
  271. Text("\(size)").tag(size)
  272. }
  273. }
  274. Picker(
  275. selection: $entry.fontWidth,
  276. label: Text("Tracking")
  277. ) {
  278. ForEach(
  279. [Font.Width.standard, Font.Width.condensed, Font.Width.expanded],
  280. id: \.self
  281. ) { width in
  282. Text(width.displayName).tag(width)
  283. }
  284. }
  285. Picker(
  286. selection: $entry.fontWeight,
  287. label: Text("Weight")
  288. ) {
  289. ForEach(
  290. [Font.Weight.regular, Font.Weight.bold, Font.Weight.black],
  291. id: \.self
  292. ) { weight in
  293. Text(weight.displayName).tag(weight)
  294. }
  295. }
  296. }
  297. Section {
  298. Toggle("Dark mode", isOn: $entry.darkMode)
  299. }
  300. }
  301. }
  302. }
  303. private var layoutSpecificSection: some View {
  304. Section {
  305. if entry.layout == .single {
  306. Picker(
  307. selection: $entry.primary,
  308. label: Text("Primary")
  309. ) {
  310. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  311. Text(value.displayName).tag(value)
  312. }
  313. }
  314. Picker(
  315. selection: $entry.top,
  316. label: Text("Top")
  317. ) {
  318. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  319. Text(value.displayName).tag(value)
  320. }
  321. }
  322. Picker(
  323. selection: $entry.bottom,
  324. label: Text("Bottom")
  325. ) {
  326. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  327. Text(value.displayName).tag(value)
  328. }
  329. }
  330. } else if entry.layout == .split {
  331. Picker(
  332. selection: $entry.top,
  333. label: Text("Top")
  334. ) {
  335. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  336. Text(value.displayName).tag(value)
  337. }
  338. }
  339. Picker(
  340. selection: $entry.bottom,
  341. label: Text("Bottom")
  342. ) {
  343. ForEach(ContactTrickValue.allCases, id: \.self) { value in
  344. Text(value.displayName).tag(value)
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }