ContactTrickRootView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. @unknown default:
  61. Section {
  62. Text(
  63. "Access to contacts - unknown state"
  64. )
  65. }
  66. }
  67. Section {}
  68. footer: {
  69. Text(
  70. "A Contact Image can be used to get live updates from Trio to your Apple Watch Contact complication and/or your iPhone Contact widget."
  71. )
  72. .frame(maxWidth: .infinity, alignment: .center)
  73. }
  74. }
  75. .dynamicTypeSize(...DynamicTypeSize.xxLarge)
  76. .onAppear(perform: configureView)
  77. .navigationTitle("Contact Image")
  78. .navigationBarTitleDisplayMode(.automatic)
  79. .navigationBarItems(
  80. trailing: EditButton()
  81. )
  82. }
  83. private func contactSettings(for index: Int) -> some View {
  84. EntryView(entry: Binding(
  85. get: { state.items[index].entry },
  86. set: { newValue in state.update(index, newValue) }
  87. ), previewState: previewState)
  88. }
  89. var previewState: ContactTrickState {
  90. let units = state.units
  91. return ContactTrickState(
  92. glucose: units == .mmolL ? "6,8" : "127",
  93. trend: "↗︎",
  94. delta: units == .mmolL ? "+0,3" : "+7",
  95. lastLoopDate: .now,
  96. iob: 6.1,
  97. iobText: "6,1",
  98. cob: 27.0,
  99. cobText: "27",
  100. eventualBG: units == .mmolL ? "8,9" : "163",
  101. maxIOB: 12.0,
  102. maxCOB: 120.0
  103. )
  104. }
  105. private var list: some View {
  106. List {
  107. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  108. NavigationLink(destination: contactSettings(for: index)) {
  109. EntryListView(entry: .constant(item.entry), index: .constant(index), previewState: previewState)
  110. }
  111. .moveDisabled(true)
  112. }
  113. .onDelete(perform: onDelete)
  114. }
  115. }
  116. private var addButton: some View {
  117. AnyView(Button(action: onAdd) { Text("Add") })
  118. }
  119. func onAdd() {
  120. state.add()
  121. }
  122. func onRequestContactsAccess() {
  123. contactStore.requestAccess(for: .contacts) { _, _ in
  124. DispatchQueue.main.async {
  125. authorization = CNContactStore.authorizationStatus(for: .contacts)
  126. }
  127. }
  128. }
  129. private func onDelete(offsets: IndexSet) {
  130. state.remove(atOffsets: offsets)
  131. }
  132. }
  133. struct EntryListView: View {
  134. @Binding var entry: ContactTrickEntry
  135. @Binding var index: Int
  136. @State private var refreshKey = UUID()
  137. let previewState: ContactTrickState
  138. var body: some View {
  139. HStack {
  140. Text(
  141. NSLocalizedString("Contact", comment: "") + ": " + "Trio \(index + 1)"
  142. )
  143. .font(.body)
  144. .minimumScaleFactor(0.5)
  145. .lineLimit(1)
  146. Spacer()
  147. VStack {
  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. }
  168. .frame(maxWidth: .infinity)
  169. }
  170. }
  171. struct EntryView: View {
  172. @Binding var entry: ContactTrickEntry
  173. @State private var availableFonts: [String]? = nil
  174. let previewState: ContactTrickState
  175. private let fontSizes: [Int] = [100, 120, 130, 140, 160, 180, 200, 225, 250, 275, 300, 350, 400]
  176. private let ringWidths: [Int] = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  177. private let ringGaps: [Int] = [0, 1, 2, 3, 4, 5]
  178. var body: some View {
  179. Section {
  180. HStack {
  181. ZStack {
  182. Circle()
  183. .fill(entry.darkMode ? .black : .white)
  184. .foregroundColor(.white)
  185. Image(uiImage: ContactPicture.getImage(contact: entry, state: previewState))
  186. .resizable()
  187. .aspectRatio(1, contentMode: .fit)
  188. .frame(width: 64, height: 64)
  189. .clipShape(Circle())
  190. Circle()
  191. .stroke(lineWidth: 2)
  192. .foregroundColor(.white)
  193. }
  194. .frame(width: 64, height: 64)
  195. }
  196. }
  197. Form {
  198. Section {
  199. Picker(
  200. selection: $entry.layout,
  201. label: Text("Layout")
  202. ) {
  203. ForEach(ContactTrickLayout.allCases) { v in
  204. Text(v.displayName).tag(v)
  205. }
  206. }
  207. }
  208. Section {
  209. switch entry.layout {
  210. case .single:
  211. Picker(
  212. selection: $entry.primary,
  213. label: Text("Primary")
  214. ) {
  215. ForEach(ContactTrickValue.allCases) { v in
  216. Text(v.displayName).tag(v)
  217. }
  218. }
  219. Picker(
  220. selection: $entry.top,
  221. label: Text("Top")
  222. ) {
  223. ForEach(ContactTrickValue.allCases) { v in
  224. Text(v.displayName).tag(v)
  225. }
  226. }
  227. Picker(
  228. selection: $entry.bottom,
  229. label: Text("Bottom")
  230. ) {
  231. ForEach(ContactTrickValue.allCases) { v in
  232. Text(v.displayName).tag(v)
  233. }
  234. }
  235. case .split:
  236. Picker(
  237. selection: $entry.top,
  238. label: Text("Top")
  239. ) {
  240. ForEach(ContactTrickValue.allCases) { v in
  241. Text(v.displayName).tag(v)
  242. }
  243. }
  244. Picker(
  245. selection: $entry.bottom,
  246. label: Text("Bottom")
  247. ) {
  248. ForEach(ContactTrickValue.allCases) { v in
  249. Text(v.displayName).tag(v)
  250. }
  251. }
  252. }
  253. }
  254. Section(header: Text("Ring")) {
  255. Picker(
  256. selection: $entry.ring1,
  257. label: Text("Outer")
  258. ) {
  259. ForEach(ContactTrickLargeRing.allCases) { v in
  260. Text(v.displayName).tag(v)
  261. }
  262. }
  263. Picker(
  264. selection: $entry.ringWidth,
  265. label: Text("Width")
  266. ) {
  267. ForEach(ringWidths, id: \.self) { s in
  268. Text("\(s)").tag(s)
  269. }
  270. }
  271. Picker(
  272. selection: $entry.ringGap,
  273. label: Text("Gap")
  274. ) {
  275. ForEach(ringGaps, id: \.self) { s in
  276. Text("\(s)").tag(s)
  277. }
  278. }
  279. }
  280. Section(header: Text("Font")) {
  281. if availableFonts == nil {
  282. HStack {
  283. Spacer()
  284. Button {
  285. loadFonts()
  286. } label: {
  287. Text(entry.fontName)
  288. }
  289. }
  290. } else {
  291. Picker(
  292. selection: $entry.fontName,
  293. label: EmptyView()
  294. ) {
  295. ForEach(availableFonts!, id: \.self) { f in
  296. Text(f).tag(f)
  297. }
  298. }
  299. .pickerStyle(.navigationLink)
  300. .labelsHidden()
  301. }
  302. Picker(
  303. selection: $entry.fontSize,
  304. label: Text("Size")
  305. ) {
  306. ForEach(fontSizes, id: \.self) { s in
  307. Text("\(s)").tag(s)
  308. }
  309. }
  310. Picker(
  311. selection: $entry.secondaryFontSize,
  312. label: Text("Secondary size")
  313. ) {
  314. ForEach(fontSizes, id: \.self) { s in
  315. Text("\(s)").tag(s)
  316. }
  317. }
  318. Picker(
  319. selection: $entry.fontTracking,
  320. label: Text("Tracking")
  321. ) {
  322. ForEach(FontTracking.allCases) { w in
  323. Text(w.displayName).tag(w)
  324. }
  325. }
  326. if entry.isDefaultFont() {
  327. Picker(
  328. selection: $entry.fontWeight,
  329. label: Text("Weight")
  330. ) {
  331. ForEach(FontWeight.allCases) { w in
  332. Text(w.displayName).tag(w)
  333. }
  334. }
  335. }
  336. }
  337. Section {
  338. Toggle("Dark mode", isOn: $entry.darkMode)
  339. }
  340. }
  341. }
  342. private func loadFonts() {
  343. if availableFonts != nil {
  344. return
  345. }
  346. var data = [String]()
  347. data.append("Default Font")
  348. UIFont.familyNames.forEach { family in
  349. UIFont.fontNames(forFamilyName: family).forEach { font in
  350. data.append(font)
  351. }
  352. }
  353. availableFonts = data
  354. }
  355. }
  356. }