ContactTrickRootView.swift 15 KB

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