From 0bfed19cb0f4b5dcc87fc1141b2c03bf62c15581 Mon Sep 17 00:00:00 2001 From: Andrew Skurlatov Date: Thu, 18 Jan 2024 00:36:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(DynamicTable.swift):=20?= =?UTF-8?q?extract=20Headers()=20function=20for=20reusability=20?= =?UTF-8?q?=F0=9F=94=A8=20refactor(WordDetailView.swift):=20remove=20unuse?= =?UTF-8?q?d=20functions=20and=20clean=20up=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/Views/DynamicTable.swift | 119 +++++++----------------------- Source/Views/WordDetailView.swift | 82 -------------------- 2 files changed, 28 insertions(+), 173 deletions(-) diff --git a/Source/Views/DynamicTable.swift b/Source/Views/DynamicTable.swift index 7d53253..1568bf6 100644 --- a/Source/Views/DynamicTable.swift +++ b/Source/Views/DynamicTable.swift @@ -3,6 +3,29 @@ import SwiftUI struct DynamicTable: View { let word: Word + + func Headers() -> some View { + HStack(spacing: 0) { + Text("") + .frame(minWidth: 87, minHeight: 0, maxHeight: .infinity, alignment: .center) + .padding(.vertical, 10) + .background(Color.gray.opacity(0.2)) + .border(Color.black, width: 1) + + ForEach(Number.allCases, id: \.rawValue) { num in + if word.shouldShowNumber(number: num) { + let headerWidthValue = headerWidth(for: num) + Text(num.rawValue.capitalized) + .frame(minWidth: headerWidthValue, minHeight: 0, maxHeight: .infinity, alignment: .center) + .padding(.vertical, 10) + .background(Color.gray.opacity(0.2)) + .border(Color.black, width: 1) + } + } + } + } + + var body: some View { ScrollView(.horizontal, showsIndicators: true) { VStack(alignment: .leading) { @@ -22,25 +45,7 @@ struct DynamicTable: View { } } - // Main Headers - HStack(spacing: 0) { - Text("") - .frame(minWidth: 87, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - - ForEach(Number.allCases, id: \.rawValue) { num in - if word.shouldShowNumber(number: num) { - let headerWidthValue = headerWidth(for: num) - Text(num.rawValue.capitalized) - .frame(minWidth: headerWidthValue, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - } - } - } + Headers() // Sub-Headers HStack(spacing: 0) { @@ -116,24 +121,7 @@ struct DynamicTable: View { } // Main Headers - HStack(spacing: 0) { - Text("") - .frame(minWidth: 87, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - - ForEach(Number.allCases, id: \.rawValue) { num in - if word.shouldShowNumber(number: num) { - let headerWidthValue = headerWidth(for: num) - Text(num.rawValue.capitalized) - .frame(minWidth: headerWidthValue, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - } - } - } + Headers() // Sub-Headers HStack(spacing: 0) { @@ -216,24 +204,7 @@ struct DynamicTable: View { } // Main Headers - HStack(spacing: 0) { - Text("") - .frame(minWidth: 87, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - - ForEach(Number.allCases, id: \.rawValue) { num in - if word.shouldShowNumber(number: num) { - let headerWidthValue = headerWidth(for: num) - Text(num.rawValue.capitalized) - .frame(minWidth: headerWidthValue, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - } - } - } + Headers() // Sub-Headers HStack(spacing: 0) { @@ -316,24 +287,7 @@ struct DynamicTable: View { } // Main Headers - HStack(spacing: 0) { - Text("") - .frame(minWidth: 87, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - - ForEach(Number.allCases, id: \.rawValue) { num in - if word.shouldShowNumber(number: num) { - let headerWidthValue = headerWidth(for: num) - Text(num.rawValue.capitalized) - .frame(minWidth: headerWidthValue, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - } - } - } + Headers() // Sub-Headers HStack(spacing: 0) { @@ -407,24 +361,7 @@ struct DynamicTable: View { } // Main Headers - HStack(spacing: 0) { - Text("") - .frame(minWidth: 87, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - - ForEach(Number.allCases, id: \.rawValue) { num in - if word.shouldShowNumber(number: num) { - let headerWidthValue = headerWidth(for: num) - Text(num.rawValue.capitalized) - .frame(minWidth: headerWidthValue, minHeight: 0, maxHeight: .infinity, alignment: .center) - .padding(.vertical, 10) - .background(Color.gray.opacity(0.2)) - .border(Color.black, width: 1) - } - } - } + Headers() // Sub-Headers HStack(spacing: 0) { diff --git a/Source/Views/WordDetailView.swift b/Source/Views/WordDetailView.swift index 4c52865..3794b6b 100644 --- a/Source/Views/WordDetailView.swift +++ b/Source/Views/WordDetailView.swift @@ -11,88 +11,6 @@ struct WordDetailView: View { let word: Word let searchDirection: SearchDirection - func gridHeader() -> some View { - GridRow { - - Text("") - - ForEach(Number.allCases, id: \.rawValue) { num in - if word.shouldShowNumber(number: num) { - Section { - Text(num.rawValue.capitalized) - }.frame(height: 10).padding(8) - .gridCellColumns(3) - } - } - } - } - - func genderHeader() -> some View { - GridRow { - Color.clear.frame(width: 100) - - ForEach(Number.allCases, id: \.rawValue) { num in - if word.shouldShowNumber(number: num) { - ForEach(Gender.allCases, id: \.rawValue) { gen in - if word.shouldShowGender(number: num, gen: gen) { - Section { - Text(gen.rawValue.capitalized) - }.frame(height: 10).padding(8) - } - } - } - } - } - } - - func nounceRow(c: Case, num: Number) -> some View { - return Section { - if word.shouldShowNumber(number: num) { - if let wordWithCase = word.generateNounCase(nounCase: c, number: num, article: false) { - Text("\(wordWithCase)" + withArticle(c: c, num: num)!) - .font(.subheadline) - .foregroundColor(.secondary) - } - } - }.frame(height: 10).padding(5) - } - - - func nounDetailViewContent() -> some View { - Grid() { - gridHeader() - - Divider() - - genderHeader() - - ForEach(Case.allCases, id: \.rawValue) { c in - GridRow { - Section { - Text("\(c.rawValue.capitalized):") - .font(.subheadline) - .fontWeight(.bold) - }.frame(height: 10).padding(5) - - }.padding(5) - - Divider() - } - }.border(.white) - } - - - - func withArticle(c: Case, num: Number) -> String? { - if word.type == .noun { - if let wordWithCase = word.generateNounCase(nounCase: c, number: num, article: true) { - return " (\(wordWithCase))" - } - } - - return "" - } - var body: some View { ScrollView { VStack(alignment: .leading) {