This repository was archived by the owner on Oct 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathWizardViewModel.java
218 lines (194 loc) · 7.89 KB
/
WizardViewModel.java
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.itsaky.androidide.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.blankj.utilcode.util.CollectionUtils;
import com.itsaky.androidide.R;
import com.itsaky.androidide.interfaces.ProjectWriterCallback;
import com.itsaky.androidide.models.NewProjectDetails;
import com.itsaky.androidide.models.ProjectTemplate;
import com.itsaky.androidide.tasks.TaskExecutor;
import com.itsaky.androidide.tasks.callables.ProjectCreatorCallable;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
public class WizardViewModel extends AndroidViewModel {
private final MutableLiveData<List<ProjectTemplate>> mProjectTemplatesList =
new MutableLiveData<>(CollectionUtils.newArrayList());
private final MutableLiveData<Boolean> mLoadingState = new MutableLiveData<>(true);
private final MutableLiveData<String> mErrorMessageState = new MutableLiveData<>();
private final MutableLiveData<String> mMessageState = new MutableLiveData<>();
private final MutableLiveData<File> mCreatedProject = new MutableLiveData<>(null);
public WizardViewModel(@NonNull Application application) {
super(application);
}
public void createTemplatesList() {
TaskExecutor.executeAsync(this::createTemplates);
}
private Void createTemplates() {
List<ProjectTemplate> mTemplates = new ArrayList<>();
ProjectTemplate
empty =
new ProjectTemplate()
.setId(0)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(getApplication().getApplicationContext(), R.string.template_empty)
.setDescription(
getApplication().getApplicationContext(), R.string.template_description_empty)
.setImageId(R.drawable.template_empty_activity),
basic =
new ProjectTemplate()
.setId(1)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(getApplication().getApplicationContext(), R.string.template_basic)
.setDescription(
getApplication().getApplicationContext(), R.string.template_description_basic)
.setImageId(R.drawable.template_basic_activity),
drawer =
new ProjectTemplate()
.setId(2)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(
getApplication().getApplicationContext(), R.string.template_navigation_drawer)
.setDescription(
getApplication().getApplicationContext(),
R.string.template_description_navigation_drawer)
.setImageId(R.drawable.template_blank_activity_drawer),
bottomTabs =
new ProjectTemplate()
.setId(3)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(
getApplication().getApplicationContext(), R.string.template_navigation_tabs)
.setImageId(R.drawable.template_bottom_navigation_activity),
tabs =
new ProjectTemplate()
.setId(4)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(getApplication().getApplicationContext(), R.string.template_tabs)
.setImageId(R.drawable.template_blank_activity_tabs),
fragmentViewModel =
new ProjectTemplate()
.setId(5)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(
getApplication().getApplicationContext(),
R.string.template_fragment_and_viewmodel)
.setImageId(R.drawable.template_empty_activity),
cppWizard =
new ProjectTemplate()
.setId(6)
.setSupportJava(true)
.setSupportKotlin(true)
.setIsCpp(true)
.setName(getApplication().getApplicationContext(), R.string.template_cpp)
.setImageId(R.drawable.template_cpp_configure),
compose =
new ProjectTemplate()
.setId(7)
.setSupportKotlin(true)
.setName(getApplication().getApplicationContext(), R.string.template_compose)
.setDescription(
getApplication().getApplicationContext(), R.string.template_description_compose)
.setImageId(R.drawable.template_compose_empty_activity),
libgdx =
new ProjectTemplate()
.setId(8)
.setSupportJava(true)
.setName(getApplication().getApplicationContext(), R.string.template_libgdx)
.setDescription(
getApplication().getApplicationContext(), R.string.template_description_libgdx)
.setImageId(R.drawable.template_game_activity),
noActivity =
new ProjectTemplate()
.setId(9)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(getApplication().getApplicationContext(), R.string.template_no_activity)
.setDescription(
getApplication().getApplicationContext(), R.string.template_no_activity_description)
.setImageId(R.drawable.template_no_activity),
androidLibrary =
new ProjectTemplate()
.setId(10)
.setSupportJava(true)
.setSupportKotlin(true)
.setName(getApplication().getApplicationContext(), R.string.template_android_library)
.setDescription(
getApplication().getApplicationContext(), R.string.template_android_library_description)
.setImageId(R.drawable.template_android_library);
mTemplates.add(noActivity);
mTemplates.add(empty);
mTemplates.add(basic);
mTemplates.add(drawer);
mTemplates.add(bottomTabs);
mTemplates.add(tabs);
mTemplates.add(fragmentViewModel);
mTemplates.add(cppWizard);
mTemplates.add(compose);
mTemplates.add(libgdx);
mTemplates.add(androidLibrary);
mProjectTemplatesList.postValue(mTemplates);
mLoadingState.postValue(false);
return null;
}
public LiveData<List<ProjectTemplate>> getProjects() {
return mProjectTemplatesList;
}
public LiveData<Boolean> getLoadingState() {
return mLoadingState;
}
public LiveData<String> getErrorState() {
return mErrorMessageState;
}
public LiveData<File> getFileCreatedState() {
return mCreatedProject;
}
public LiveData<String> getStatusMessage() {
return mMessageState;
}
public void createProject(
ProjectTemplate currentTemplate,
String appName,
String packageName,
int minSdk,
int targetSdk,
String language,
String cppFlags,
String savePath) {
final var details =
new NewProjectDetails(
appName, packageName, minSdk, targetSdk, language, cppFlags, savePath);
final var callable = new ProjectCreatorCallable(currentTemplate, details, createCallback());
TaskExecutor.executeAsync(callable);
}
private ProjectWriterCallback createCallback() {
return new ProjectWriterCallback() {
@Override
public void beforeBegin() {
mMessageState.setValue(getApplication().getString(R.string.msg_begin_project_write));
}
@Override
public void onProcessTask(String taskName) {
mMessageState.setValue(taskName);
}
@Override
public void onSuccess(File rootDir) {
mCreatedProject.setValue(rootDir);
}
@Override
public void onFailed(String reason) {
mErrorMessageState.setValue(reason);
}
};
}
}