Skip to content

Commit

Permalink
🔨 refactor(DynamicTable.swift): extract Headers() function for reusab…
Browse files Browse the repository at this point in the history
…ility

🔨 refactor(WordDetailView.swift): remove unused functions and clean up code
  • Loading branch information
andskur committed Jan 17, 2024
1 parent 3cc65f2 commit 0bfed19
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 173 deletions.
119 changes: 28 additions & 91 deletions Source/Views/DynamicTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
82 changes: 0 additions & 82 deletions Source/Views/WordDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0bfed19

Please sign in to comment.