-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathFabriclikeInstallFragment.java
302 lines (269 loc) · 11.6 KB
/
FabriclikeInstallFragment.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package net.kdt.pojavlaunch.fragments;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import net.kdt.pojavlaunch.PojavApplication;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.extra.ExtraCore;
import net.kdt.pojavlaunch.modloaders.FabriclikeDownloadTask;
import net.kdt.pojavlaunch.modloaders.FabriclikeUtils;
import net.kdt.pojavlaunch.modloaders.FabricVersion;
import net.kdt.pojavlaunch.modloaders.ModloaderDownloadListener;
import net.kdt.pojavlaunch.modloaders.ModloaderListenerProxy;
import net.kdt.pojavlaunch.modloaders.modpacks.SelfReferencingFuture;
import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Future;
public abstract class FabriclikeInstallFragment extends Fragment implements ModloaderDownloadListener, CompoundButton.OnCheckedChangeListener {
private final FabriclikeUtils mFabriclikeUtils;
private final String mExtraTag;
private Spinner mGameVersionSpinner;
private FabricVersion[] mGameVersionArray;
private Future<?> mGameVersionFuture;
private String mSelectedGameVersion;
private Spinner mLoaderVersionSpinner;
private FabricVersion[] mLoaderVersionArray;
private Future<?> mLoaderVersionFuture;
private String mSelectedLoaderVersion;
private ProgressBar mProgressBar;
private Button mStartButton;
private View mRetryView;
private CheckBox mOnlyStableCheckbox;
protected FabriclikeInstallFragment(FabriclikeUtils mFabriclikeUtils, String mFragmentTag) {
super(R.layout.fragment_fabric_install);
this.mFabriclikeUtils = mFabriclikeUtils;
this.mExtraTag = mFragmentTag + "_proxy";
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mStartButton = view.findViewById(R.id.fabric_installer_start_button);
mStartButton.setOnClickListener(this::onClickStart);
mGameVersionSpinner = view.findViewById(R.id.fabric_installer_game_ver_spinner);
mGameVersionSpinner.setOnItemSelectedListener(new GameVersionSelectedListener());
mLoaderVersionSpinner = view.findViewById(R.id.fabric_installer_loader_ver_spinner);
mLoaderVersionSpinner.setOnItemSelectedListener(new LoaderVersionSelectedListener());
mProgressBar = view.findViewById(R.id.fabric_installer_progress_bar);
mRetryView = view.findViewById(R.id.fabric_installer_retry_layout);
mOnlyStableCheckbox = view.findViewById(R.id.fabric_installer_only_stable_checkbox);
mOnlyStableCheckbox.setOnCheckedChangeListener(this);
view.findViewById(R.id.fabric_installer_retry_button).setOnClickListener(this::onClickRetry);
((TextView)view.findViewById(R.id.fabric_installer_label_loader_ver)).setText(getString(R.string.fabric_dl_loader_version, mFabriclikeUtils.getName()));
ModloaderListenerProxy proxy = getListenerProxy();
if(proxy != null) {
mStartButton.setEnabled(false);
proxy.attachListener(this);
}
updateGameVersions();
}
@Override
public void onStop() {
cancelFutureChecked(mGameVersionFuture);
cancelFutureChecked(mLoaderVersionFuture);
ModloaderListenerProxy proxy = getListenerProxy();
if(proxy != null) {
proxy.detachListener();
}
super.onStop();
}
private void onClickStart(View v) {
if(ProgressKeeper.hasOngoingTasks()) {
Toast.makeText(v.getContext(), R.string.tasks_ongoing, Toast.LENGTH_LONG).show();
return;
}
ModloaderListenerProxy proxy = new ModloaderListenerProxy();
FabriclikeDownloadTask fabricDownloadTask = new FabriclikeDownloadTask(proxy, mFabriclikeUtils,
mSelectedGameVersion, mSelectedLoaderVersion, true);
proxy.attachListener(this);
setListenerProxy(proxy);
mStartButton.setEnabled(false);
new Thread(fabricDownloadTask).start();
}
private void onClickRetry(View v) {
mStartButton.setEnabled(false);
mRetryView.setVisibility(View.GONE);
mLoaderVersionSpinner.setAdapter(null);
if(mGameVersionArray == null) {
mGameVersionSpinner.setAdapter(null);
updateGameVersions();
return;
}
updateLoaderVersions();
}
@Override
public void onDownloadFinished(File downloadedFile) {
Tools.runOnUiThread(()->{
getListenerProxy().detachListener();
setListenerProxy(null);
mStartButton.setEnabled(true);
// This works because the due to the fact that we have transitioned here
// without adding a transaction to the back stack, which caused the previous
// transaction to be amended (i guess?? thats how the back stack dump looks like)
// we can get back to the main fragment with just one back stack pop.
// For some reason that amendment causes the transaction to lose its tag
// so we cant use the tag here.
getParentFragmentManager().popBackStackImmediate();
});
}
@Override
public void onDataNotAvailable() {
Tools.runOnUiThread(()->{
Context context = requireContext();
getListenerProxy().detachListener();
setListenerProxy(null);
mStartButton.setEnabled(true);
Tools.dialog(context,
context.getString(R.string.global_error),
context.getString(R.string.fabric_dl_cant_read_meta, mFabriclikeUtils.getName()));
});
}
@Override
public void onDownloadError(Exception e) {
Tools.runOnUiThread(()-> {
Context context = requireContext();
getListenerProxy().detachListener();
setListenerProxy(null);
mStartButton.setEnabled(true);
Tools.showError(context, e);
});
}
private void cancelFutureChecked(Future<?> future) {
if(future != null && !future.isCancelled()) future.cancel(true);
}
private void startLoading() {
mProgressBar.setVisibility(View.VISIBLE);
mStartButton.setEnabled(false);
}
private void stopLoading() {
mProgressBar.setVisibility(View.GONE);
// The "visibility on" is managed by the spinners
}
private ArrayAdapter<FabricVersion> createAdapter(FabricVersion[] fabricVersions, boolean onlyStable) {
ArrayList<FabricVersion> filteredVersions = new ArrayList<>(fabricVersions.length);
for(FabricVersion fabricVersion : fabricVersions) {
if(!onlyStable || fabricVersion.stable) filteredVersions.add(fabricVersion);
}
filteredVersions.trimToSize();
return new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_dropdown_item, filteredVersions);
}
private void onException(Future<?> myFuture, Exception e) {
Tools.runOnUiThread(()->{
if(myFuture.isCancelled()) return;
stopLoading();
if(e != null) Tools.showError(requireContext(), e);
mRetryView.setVisibility(View.VISIBLE);
});
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
updateGameSpinner();
updateLoaderSpinner();
}
class LoaderVersionSelectedListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
mSelectedLoaderVersion = ((FabricVersion) adapterView.getAdapter().getItem(i)).version;
mStartButton.setEnabled(mSelectedGameVersion != null);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
mSelectedLoaderVersion = null;
mStartButton.setEnabled(false);
}
}
class LoadLoaderVersionsTask implements SelfReferencingFuture.FutureInterface {
@Override
public void run(Future<?> myFuture) {
Log.i("LoadLoaderVersions", "Starting...");
try {
mLoaderVersionArray = mFabriclikeUtils.downloadLoaderVersions(mSelectedGameVersion);
if(mLoaderVersionArray != null) onFinished(myFuture);
else onException(myFuture, null);
}catch (IOException e) {
onException(myFuture, e);
}
}
private void onFinished(Future<?> myFuture) {
Tools.runOnUiThread(()->{
if(myFuture.isCancelled()) return;
stopLoading();
updateLoaderSpinner();
});
}
}
private void updateLoaderVersions() {
startLoading();
mLoaderVersionFuture = new SelfReferencingFuture(new LoadLoaderVersionsTask()).startOnExecutor(PojavApplication.sExecutorService);
}
private void updateLoaderSpinner() {
if(mLoaderVersionArray == null) return;
mLoaderVersionSpinner.setAdapter(createAdapter(mLoaderVersionArray, mOnlyStableCheckbox.isChecked()));
}
class GameVersionSelectedListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
mSelectedGameVersion = ((FabricVersion) adapterView.getAdapter().getItem(i)).version;
cancelFutureChecked(mLoaderVersionFuture);
updateLoaderVersions();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
mSelectedGameVersion = null;
if(mLoaderVersionFuture != null) mLoaderVersionFuture.cancel(true);
adapterView.setAdapter(null);
}
}
class LoadGameVersionsTask implements SelfReferencingFuture.FutureInterface {
@Override
public void run(Future<?> myFuture) {
try {
mGameVersionArray = mFabriclikeUtils.downloadGameVersions();
if(mGameVersionArray != null) onFinished(myFuture);
else onException(myFuture, null);
}catch (IOException e) {
onException(myFuture, e);
}
}
private void onFinished(Future<?> myFuture) {
Tools.runOnUiThread(()->{
if(myFuture.isCancelled()) return;
stopLoading();
updateGameSpinner();
});
}
}
private void updateGameVersions() {
startLoading();
mGameVersionFuture = new SelfReferencingFuture(new LoadGameVersionsTask()).startOnExecutor(PojavApplication.sExecutorService);
}
private void updateGameSpinner() {
if(mGameVersionArray == null) return;
mGameVersionSpinner.setAdapter(createAdapter(mGameVersionArray, mOnlyStableCheckbox.isChecked()));
}
private ModloaderListenerProxy getListenerProxy() {
return (ModloaderListenerProxy) ExtraCore.getValue(mExtraTag);
}
private void setListenerProxy(ModloaderListenerProxy listenerProxy) {
ExtraCore.setValue(mExtraTag, listenerProxy);
}
}