1
1
package eu .vcmi .vcmi ;
2
2
3
+ import android .content .DialogInterface ;
3
4
import android .os .AsyncTask ;
4
5
import android .os .Bundle ;
5
- import android . os . Environment ;
6
+
6
7
import androidx .annotation .Nullable ;
7
8
import com .google .android .material .snackbar .Snackbar ;
9
+
10
+ import androidx .appcompat .app .AlertDialog ;
8
11
import androidx .recyclerview .widget .DefaultItemAnimator ;
9
12
import androidx .recyclerview .widget .DividerItemDecoration ;
10
13
import androidx .recyclerview .widget .LinearLayoutManager ;
11
14
import androidx .recyclerview .widget .RecyclerView ;
12
- import androidx . appcompat . widget . Toolbar ;
15
+
13
16
import android .view .Menu ;
14
17
import android .view .MenuInflater ;
15
18
import android .view .MenuItem ;
24
27
import java .util .ArrayList ;
25
28
import java .util .Collections ;
26
29
import java .util .List ;
27
- import java .util .stream .Stream ;
28
- import java .util .stream .Collectors ;
30
+ import java .util .Locale ;
29
31
30
32
import eu .vcmi .vcmi .content .ModBaseViewHolder ;
31
33
import eu .vcmi .vcmi .content .ModsAdapter ;
32
- import eu .vcmi .vcmi .content . ModsViewHolder ;
34
+ import eu .vcmi .vcmi .mods . VCMIMod ;
33
35
import eu .vcmi .vcmi .mods .VCMIModContainer ;
34
36
import eu .vcmi .vcmi .mods .VCMIModsRepo ;
37
+ import eu .vcmi .vcmi .util .InstallModAsync ;
35
38
import eu .vcmi .vcmi .util .FileUtil ;
36
39
import eu .vcmi .vcmi .util .Log ;
40
+ import eu .vcmi .vcmi .util .ServerResponse ;
37
41
38
42
/**
39
43
* @author F
40
44
*/
41
45
public class ActivityMods extends ActivityWithToolbar
42
46
{
43
- private static final boolean ENABLE_REPO_DOWNLOADING = false ;
44
- private static final String REPO_URL = "http ://download.vcmi.eu/mods/ repository/repository .json" ;
47
+ private static final boolean ENABLE_REPO_DOWNLOADING = true ;
48
+ private static final String REPO_URL = "https ://raw.githubusercontent.com/vcmi/vcmi-mods- repository/develop/github .json" ;
45
49
private VCMIModsRepo mRepo ;
46
50
private RecyclerView mRecycler ;
47
51
@@ -70,15 +74,18 @@ protected void onCreate(@Nullable final Bundle savedInstanceState)
70
74
mRecycler .addItemDecoration (new DividerItemDecoration (this , DividerItemDecoration .VERTICAL ));
71
75
mRecycler .setVisibility (View .GONE );
72
76
77
+ mModsAdapter = new ModsAdapter (new OnAdapterItemAction ());
78
+ mRecycler .setAdapter (mModsAdapter );
79
+
73
80
new AsyncLoadLocalMods ().execute ((Void ) null );
74
81
}
75
82
76
83
private void loadLocalModData () throws IOException , JSONException
77
84
{
78
- final String dataRoot = getDataDir () + "/" + Const . VCMI_DATA_ROOT_FOLDER_NAME ;
85
+ final File dataRoot = Storage . getVcmiDataDir ( this ) ;
79
86
final String internalDataRoot = getFilesDir () + "/" + Const .VCMI_DATA_ROOT_FOLDER_NAME ;
80
87
81
- final File modsRoot = new File (dataRoot + "/Mods" );
88
+ final File modsRoot = new File (dataRoot , "/Mods" );
82
89
final File internalModsRoot = new File (internalDataRoot + "/Mods" );
83
90
if (!modsRoot .exists () && !internalModsRoot .exists ())
84
91
{
@@ -98,8 +105,7 @@ private void loadLocalModData() throws IOException, JSONException
98
105
}
99
106
mModContainer = VCMIModContainer .createContainer (topLevelModsFolders );
100
107
101
- final String configPath = dataRoot + "/config/modSettings.json" ;
102
- final File modConfigFile = new File (configPath );
108
+ final File modConfigFile = new File (dataRoot , "config/modSettings.json" );
103
109
if (!modConfigFile .exists ())
104
110
{
105
111
Log .w (this , "We don't have mods config" );
@@ -129,6 +135,7 @@ public boolean onOptionsItemSelected(final MenuItem item)
129
135
Log .i (this , "Should download repo now..." );
130
136
if (ENABLE_REPO_DOWNLOADING )
131
137
{
138
+ mProgress .setVisibility (View .VISIBLE );
132
139
mRepo .init (REPO_URL , new OnModsRepoInitialized ()); // disabled because the json is broken anyway
133
140
}
134
141
else
@@ -150,16 +157,21 @@ private void handleNoData()
150
157
151
158
private void saveModSettingsToFile ()
152
159
{
153
- mModContainer .saveToFile (new File (getDataDir (), Const .VCMI_DATA_ROOT_FOLDER_NAME + "/config/modSettings.json" ));
160
+ mModContainer .saveToFile (
161
+ new File (
162
+ Storage .getVcmiDataDir (this ),
163
+ "config/modSettings.json" ));
154
164
}
155
165
156
166
private class OnModsRepoInitialized implements VCMIModsRepo .IOnModsRepoDownloaded
157
167
{
158
168
@ Override
159
- public void onSuccess ()
169
+ public void onSuccess (ServerResponse < List < VCMIMod >> response )
160
170
{
161
171
Log .i (this , "Initialized mods repo" );
162
- // TODO update dataset
172
+ mModContainer .updateFromRepo (response .mContent );
173
+ mModsAdapter .updateModsList (mModContainer .submods ());
174
+ mProgress .setVisibility (View .GONE );
163
175
}
164
176
165
177
@ Override
@@ -206,13 +218,7 @@ protected void onPostExecute(final Void aVoid)
206
218
{
207
219
mProgress .setVisibility (View .GONE );
208
220
mRecycler .setVisibility (View .VISIBLE );
209
- mModsAdapter = new ModsAdapter (
210
- mModContainer .submods ()
211
- .stream ()
212
- .map (ModsAdapter .ModItem ::new )
213
- .collect (Collectors .toList ()),
214
- new OnAdapterItemAction ());
215
- mRecycler .setAdapter (mModsAdapter );
221
+ mModsAdapter .updateModsList (mModContainer .submods ());
216
222
}
217
223
}
218
224
}
@@ -242,14 +248,84 @@ public void onItemPressed(final ModsAdapter.ModItem mod, final RecyclerView.View
242
248
public void onDownloadPressed (final ModsAdapter .ModItem mod , final RecyclerView .ViewHolder vh )
243
249
{
244
250
Log .i (this , "Mod download pressed: " + mod );
251
+ mModsAdapter .downloadProgress (mod , "0%" );
252
+ installModAsync (mod );
245
253
}
246
254
247
255
@ Override
248
256
public void onTogglePressed (final ModsAdapter .ModItem item , final ModBaseViewHolder holder )
249
257
{
250
- item .mMod .mActive = !item .mMod .mActive ;
251
- mModsAdapter .notifyItemChanged (holder .getAdapterPosition ());
252
- saveModSettingsToFile ();
258
+ if (!item .mMod .mSystem && item .mMod .mInstalled )
259
+ {
260
+ item .mMod .mActive = !item .mMod .mActive ;
261
+ mModsAdapter .notifyItemChanged (holder .getAdapterPosition ());
262
+ saveModSettingsToFile ();
263
+ }
264
+ }
265
+
266
+ @ Override
267
+ public void onUninstall (ModsAdapter .ModItem item , ModBaseViewHolder holder )
268
+ {
269
+ File installationFolder = item .mMod .installationFolder ;
270
+ ActivityMods activity = ActivityMods .this ;
271
+
272
+ if (installationFolder != null ){
273
+ new AlertDialog .Builder (activity )
274
+ .setTitle (activity .getString (R .string .mods_removal_title , item .mMod .mName ))
275
+ .setMessage (activity .getString (R .string .mods_removal_confirmation , item .mMod .mName ))
276
+ .setIcon (android .R .drawable .ic_dialog_alert )
277
+ .setNegativeButton (android .R .string .no , null )
278
+ .setPositiveButton (android .R .string .yes , (dialog , whichButton ) ->
279
+ {
280
+ FileUtil .clearDirectory (installationFolder );
281
+ installationFolder .delete ();
282
+
283
+ mModsAdapter .modRemoved (item );
284
+ })
285
+ .show ();
286
+ }
287
+ }
288
+ }
289
+
290
+ private void installModAsync (ModsAdapter .ModItem mod ){
291
+ File dataDir = Storage .getVcmiDataDir (this );
292
+ File modFolder = new File (
293
+ new File (dataDir , "Mods" ),
294
+ mod .mMod .mId .toLowerCase (Locale .US ));
295
+
296
+ InstallModAsync modInstaller = new InstallModAsync (
297
+ modFolder ,
298
+ this ,
299
+ new InstallModCallback (mod )
300
+ );
301
+
302
+ modInstaller .execute (mod .mMod .mArchiveUrl );
303
+ }
304
+
305
+ public class InstallModCallback implements InstallModAsync .PostDownload
306
+ {
307
+ private ModsAdapter .ModItem mod ;
308
+
309
+ public InstallModCallback (ModsAdapter .ModItem mod )
310
+ {
311
+ this .mod = mod ;
312
+ }
313
+
314
+ @ Override
315
+ public void downloadDone (Boolean succeed , File modFolder )
316
+ {
317
+ if (succeed ){
318
+ mModsAdapter .modInstalled (mod , modFolder );
319
+ }
320
+ }
321
+
322
+ @ Override
323
+ public void downloadProgress (String ... progress )
324
+ {
325
+ if (progress .length > 0 )
326
+ {
327
+ mModsAdapter .downloadProgress (mod , progress [0 ]);
328
+ }
253
329
}
254
330
}
255
331
}
0 commit comments