-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_screen.sh
More file actions
executable file
·194 lines (156 loc) · 7.2 KB
/
Copy pathgenerate_screen.sh
File metadata and controls
executable file
·194 lines (156 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# Paths
current_path="$(pwd)"
app_folder_path="${current_path}/sampleAppShared/src/commonMain/kotlin/com/sampleApp/app"
package_name="com.sampleApp.app"
# Check if both feature and screen arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <feature> <screen>"
exit 1
fi
# Inputs
feature="$1"
screen="$2"
# Convert "screen" to lowercase
screen_lower=$(echo "$screen" | tr '[:upper:]' '[:lower:]')
#####################################################################
# Create Contract file
#####################################################################
# Prepare file values
contract_file_name="${screen}Contract.kt"
contract_file_path="${app_folder_path}/presentation/${feature}/${screen_lower}"
contract_file_content="package ${package_name}.presentation.${feature}.${screen_lower}
import com.metacto.core.presentation.base.ViewEvent
import com.metacto.core.presentation.base.ViewSideEffect
import com.metacto.core.presentation.base.ViewState
class ${screen}Contract {
data class State(
val isInitialized: Boolean = false
) : ViewState
sealed class Event : ViewEvent {
data object Init : Event()
}
sealed class Effect : ViewSideEffect
}"
# Create the directory and the create the file
mkdir -p "$contract_file_path"
echo "$contract_file_content" >"${contract_file_path}/${contract_file_name}"
# Display message to the user
echo "File generated at: ${contract_file_path}/${contract_file_name}"
#####################################################################
# Create ViewModel file
#####################################################################
# Prepare file values
viewmodel_file_name="${screen}ViewModel.kt"
viewmodel_file_path="${app_folder_path}/presentation/${feature}/${screen_lower}"
viewmodel_file_content="package ${package_name}.presentation.${feature}.${screen_lower}
import ${package_name}.presentation.base.BaseViewModel
import ${package_name}.presentation.${feature}.${screen_lower}.${screen}Contract.Effect
import ${package_name}.presentation.${feature}.${screen_lower}.${screen}Contract.Event
import ${package_name}.presentation.${feature}.${screen_lower}.${screen}Contract.State
class ${screen}ViewModel : BaseViewModel<State, Event, Effect>() {
override fun setInitialState() = State()
override fun handleEvents(event: Event): Any = when (event) {
Event.Init -> init()
}
private fun init() {
// Validate if already initialized
if (currentState.isInitialized) return
// Init
// Update the flag
setState { copy(isInitialized = true) }
}
}"
# Create the directory and the create the file
mkdir -p "$viewmodel_file_path"
echo "$viewmodel_file_content" >"${viewmodel_file_path}/${viewmodel_file_name}"
# Display message to the user
echo "File generated at: ${viewmodel_file_path}/${viewmodel_file_name}"
#####################################################################
# Create Screen file
#####################################################################
# Prepare file values
screen_file_name="${screen}Screen.kt"
screen_file_path="${app_folder_path}/presentation/${feature}/${screen_lower}"
screen_file_content="package ${package_name}.presentation.${feature}.${screen_lower}
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import ${package_name}.presentation.${feature}.${screen_lower}.${screen}Contract.Event
import ${package_name}.presentation.${feature}.${screen_lower}.components.${screen}Content
import com.metacto.core.presentation.base.BaseScreen
import com.metacto.core.presentation.base.SIDE_EFFECTS_KEY
import com.metacto.core.presentation.base.rememberViewModel
internal object ${screen}Screen : BaseScreen<${screen}ViewModel>() {
@Composable
override fun Content() {
// Get the view model
val viewModel = rememberViewModel<${screen}ViewModel>()
// Init view model
LaunchedEffect(SIDE_EFFECTS_KEY) {
viewModel.setEvent(Event.Init)
}
// Render content
${screen}Content(
state = viewModel.viewState.value,
onEvent = viewModel::setEvent
)
}
}"
# Create the directory and the create the file
mkdir -p "$screen_file_path"
echo "$screen_file_content" >"${screen_file_path}/${screen_file_name}"
# Display message to the user
echo "File generated at: ${screen_file_path}/${screen_file_name}"
#####################################################################
# Create Content file
#####################################################################
# Prepare file values
content_file_name="${screen}Content.kt"
content_file_path="${app_folder_path}/presentation/${feature}/${screen_lower}/components"
content_file_content="package ${package_name}.presentation.${feature}.${screen_lower}.components
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import com.metacto.core.presentation.components.containers.ScreenColumn
import ${package_name}.presentation.theme.AppTheme.colors
import ${package_name}.presentation.theme.AppTheme.typography
import ${package_name}.presentation.theme.AppTheme.shapes
import ${package_name}.presentation.theme.AppTheme.spacings
import ${package_name}.presentation.${feature}.${screen_lower}.${screen}Contract.State
import ${package_name}.presentation.${feature}.${screen_lower}.${screen}Contract.Event
@Composable
internal fun ${screen}Content(
state: State,
onEvent: (Event) -> Unit
) {
// Container column
ScreenColumn(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = \"${screen} Screen\"
)
}
}"
# Create the directory and the create the file
mkdir -p "$content_file_path"
echo "$content_file_content" >"${content_file_path}/${content_file_name}"
# Display message to the user
echo "File generated at: ${content_file_path}/${content_file_name}"
#####################################################################
# Add the view model DI into DI module
#####################################################################
# Prepare file values
viewmodels_di_module_file_path="${app_folder_path}/di"
viewmodels_di_module_file_name="ViewModelsModule.kt"
viewmodels_di_module_full_file_name="${viewmodels_di_module_file_path}/${viewmodels_di_module_file_name}"
# Then append the screen di module to the main feature module
viewmodel_import_code="import ${package_name}.presentation.${feature}.${screen_lower}.${screen}ViewModel"
awk -v text="$viewmodel_import_code" '/\/\/ MARK: Add imports/ { print; print text; next } 1' "$viewmodels_di_module_full_file_name" >tmpfile && mv tmpfile "$viewmodels_di_module_full_file_name"
echo "Screen ViewModel DI appended to ${viewmodels_di_module_full_file_name}"
# Then append the screen di module to the main feature module
di_include_code=" commonViewModel { ${screen}ViewModel() }"
awk -v text="$di_include_code" '/\/\/ MARK: Add view model definitions/ { print; print text; next } 1' "$viewmodels_di_module_full_file_name" >tmpfile && mv tmpfile "$viewmodels_di_module_full_file_name"
echo "Screen ViewModel DI appended to ${viewmodels_di_module_full_file_name}"