@@ -10,27 +10,25 @@ import SwiftUI
1010struct CreateDatabaseSheet : View {
1111 @Environment ( \. dismiss) private var dismiss
1212
13+ let databaseType : DatabaseType
1314 let onCreate : ( String , String , String ? ) async throws -> Void
1415
1516 @State private var databaseName = " "
16- @State private var charset = " utf8mb4 "
17- @State private var collation = " utf8mb4_unicode_ci "
17+ @State private var charset : String
18+ @State private var collation : String
1819 @State private var isCreating = false
1920 @State private var errorMessage : String ?
2021
21- private let charsets = [
22- " utf8mb4 " ,
23- " utf8 " ,
24- " latin1 " ,
25- " ascii "
26- ]
22+ private let config : CreateDatabaseOptions . Config
2723
28- private let collations : [ String : [ String ] ] = [
29- " utf8mb4 " : [ " utf8mb4_unicode_ci " , " utf8mb4_general_ci " , " utf8mb4_bin " ] ,
30- " utf8 " : [ " utf8_unicode_ci " , " utf8_general_ci " , " utf8_bin " ] ,
31- " latin1 " : [ " latin1_swedish_ci " , " latin1_general_ci " , " latin1_bin " ] ,
32- " ascii " : [ " ascii_general_ci " , " ascii_bin " ]
33- ]
24+ init ( databaseType: DatabaseType , onCreate: @escaping ( String , String , String ? ) async throws -> Void ) {
25+ self . databaseType = databaseType
26+ self . onCreate = onCreate
27+ let cfg = CreateDatabaseOptions . config ( for: databaseType)
28+ self . config = cfg
29+ self . _charset = State ( initialValue: cfg. defaultCharset)
30+ self . _collation = State ( initialValue: cfg. defaultCollation)
31+ }
3432
3533 var body : some View {
3634 VStack ( spacing: 0 ) {
@@ -54,36 +52,38 @@ struct CreateDatabaseSheet: View {
5452 . font ( . system( size: ThemeEngine . shared. activeTheme. typography. body) )
5553 }
5654
57- // Charset
58- VStack ( alignment: . leading, spacing: 6 ) {
59- Text ( " Character Set " )
60- . font ( . system( size: ThemeEngine . shared. activeTheme. typography. small, weight: . medium) )
61- . foregroundStyle ( . secondary)
62-
63- Picker ( " " , selection: $charset) {
64- ForEach ( charsets, id: \. self) { cs in
65- Text ( cs) . tag ( cs)
55+ if config. showOptions {
56+ // Charset / Encoding
57+ VStack ( alignment: . leading, spacing: 6 ) {
58+ Text ( config. charsetLabel)
59+ . font ( . system( size: ThemeEngine . shared. activeTheme. typography. small, weight: . medium) )
60+ . foregroundStyle ( . secondary)
61+
62+ Picker ( " " , selection: $charset) {
63+ ForEach ( config. charsets, id: \. self) { cs in
64+ Text ( cs) . tag ( cs)
65+ }
6666 }
67+ . labelsHidden ( )
68+ . pickerStyle ( . menu)
69+ . font ( . system( size: ThemeEngine . shared. activeTheme. typography. body) )
6770 }
68- . labelsHidden ( )
69- . pickerStyle ( . menu)
70- . font ( . system( size: ThemeEngine . shared. activeTheme. typography. body) )
71- }
7271
73- // Collation
74- VStack ( alignment: . leading, spacing: 6 ) {
75- Text ( " Collation " )
76- . font ( . system( size: ThemeEngine . shared. activeTheme. typography. small, weight: . medium) )
77- . foregroundStyle ( . secondary)
72+ // Collation / LC_COLLATE
73+ VStack ( alignment: . leading, spacing: 6 ) {
74+ Text ( config . collationLabel )
75+ . font ( . system( size: ThemeEngine . shared. activeTheme. typography. small, weight: . medium) )
76+ . foregroundStyle ( . secondary)
7877
79- Picker ( " " , selection: $collation) {
80- ForEach ( collations [ charset] ?? [ ] , id: \. self) { col in
81- Text ( col) . tag ( col)
78+ Picker ( " " , selection: $collation) {
79+ ForEach ( config. collations [ charset] ?? [ ] , id: \. self) { col in
80+ Text ( col) . tag ( col)
81+ }
8282 }
83+ . labelsHidden ( )
84+ . pickerStyle ( . menu)
85+ . font ( . system( size: ThemeEngine . shared. activeTheme. typography. body) )
8386 }
84- . labelsHidden ( )
85- . pickerStyle ( . menu)
86- . font ( . system( size: ThemeEngine . shared. activeTheme. typography. body) )
8787 }
8888
8989 // Error message
@@ -116,14 +116,12 @@ struct CreateDatabaseSheet: View {
116116 }
117117 . frame ( width: 380 )
118118 . onExitCommand {
119- // Prevent dismissing the sheet via ESC while a database is being created
120119 if !isCreating {
121120 dismiss ( )
122121 }
123122 }
124123 . onChange ( of: charset) { _, newCharset in
125- // Update collation when charset changes
126- if let firstCollation = collations [ newCharset] ? . first {
124+ if let firstCollation = config. collations [ newCharset] ? . first {
127125 collation = firstCollation
128126 }
129127 }
@@ -137,7 +135,11 @@ struct CreateDatabaseSheet: View {
137135
138136 Task {
139137 do {
140- try await onCreate ( databaseName, charset, collation)
138+ if config. showOptions {
139+ try await onCreate ( databaseName, charset, collation)
140+ } else {
141+ try await onCreate ( databaseName, " " , nil )
142+ }
141143 await MainActor . run {
142144 dismiss ( )
143145 }
0 commit comments