2
2
3
3
import android .os .AsyncTask ;
4
4
import android .os .Bundle ;
5
- import android . os . Environment ;
5
+
6
6
import androidx .annotation .Nullable ;
7
7
import com .google .android .material .snackbar .Snackbar ;
8
8
import androidx .recyclerview .widget .DefaultItemAnimator ;
9
9
import androidx .recyclerview .widget .DividerItemDecoration ;
10
10
import androidx .recyclerview .widget .LinearLayoutManager ;
11
11
import androidx .recyclerview .widget .RecyclerView ;
12
- import androidx . appcompat . widget . Toolbar ;
12
+
13
13
import android .view .Menu ;
14
14
import android .view .MenuInflater ;
15
15
import android .view .MenuItem ;
24
24
import java .util .ArrayList ;
25
25
import java .util .Collections ;
26
26
import java .util .List ;
27
- import java .util .stream .Stream ;
28
- import java .util .stream .Collectors ;
29
27
30
28
import eu .vcmi .vcmi .content .ModBaseViewHolder ;
31
29
import eu .vcmi .vcmi .content .ModsAdapter ;
32
- import eu .vcmi .vcmi .content . ModsViewHolder ;
30
+ import eu .vcmi .vcmi .mods . VCMIMod ;
33
31
import eu .vcmi .vcmi .mods .VCMIModContainer ;
34
32
import eu .vcmi .vcmi .mods .VCMIModsRepo ;
33
+ import eu .vcmi .vcmi .util .InstallModAsync ;
35
34
import eu .vcmi .vcmi .util .FileUtil ;
36
35
import eu .vcmi .vcmi .util .Log ;
36
+ import eu .vcmi .vcmi .util .ServerResponse ;
37
37
38
38
/**
39
39
* @author F
40
40
*/
41
41
public class ActivityMods extends ActivityWithToolbar
42
42
{
43
- private static final boolean ENABLE_REPO_DOWNLOADING = false ;
44
- private static final String REPO_URL = "http ://download.vcmi.eu/mods/ repository/repository .json" ;
43
+ private static final boolean ENABLE_REPO_DOWNLOADING = true ;
44
+ private static final String REPO_URL = "https ://raw.githubusercontent.com/vcmi/vcmi-mods- repository/develop/github .json" ;
45
45
private VCMIModsRepo mRepo ;
46
46
private RecyclerView mRecycler ;
47
47
@@ -70,15 +70,18 @@ protected void onCreate(@Nullable final Bundle savedInstanceState)
70
70
mRecycler .addItemDecoration (new DividerItemDecoration (this , DividerItemDecoration .VERTICAL ));
71
71
mRecycler .setVisibility (View .GONE );
72
72
73
+ mModsAdapter = new ModsAdapter (new OnAdapterItemAction ());
74
+ mRecycler .setAdapter (mModsAdapter );
75
+
73
76
new AsyncLoadLocalMods ().execute ((Void ) null );
74
77
}
75
78
76
79
private void loadLocalModData () throws IOException , JSONException
77
80
{
78
- final String dataRoot = getDataDir () + "/" + Const . VCMI_DATA_ROOT_FOLDER_NAME ;
81
+ final File dataRoot = Storage . getVcmiDataDir ( this ) ;
79
82
final String internalDataRoot = getFilesDir () + "/" + Const .VCMI_DATA_ROOT_FOLDER_NAME ;
80
83
81
- final File modsRoot = new File (dataRoot + "/Mods" );
84
+ final File modsRoot = new File (dataRoot , "/Mods" );
82
85
final File internalModsRoot = new File (internalDataRoot + "/Mods" );
83
86
if (!modsRoot .exists () && !internalModsRoot .exists ())
84
87
{
@@ -98,8 +101,7 @@ private void loadLocalModData() throws IOException, JSONException
98
101
}
99
102
mModContainer = VCMIModContainer .createContainer (topLevelModsFolders );
100
103
101
- final String configPath = dataRoot + "/config/modSettings.json" ;
102
- final File modConfigFile = new File (configPath );
104
+ final File modConfigFile = new File (dataRoot , "config/modSettings.json" );
103
105
if (!modConfigFile .exists ())
104
106
{
105
107
Log .w (this , "We don't have mods config" );
@@ -129,6 +131,7 @@ public boolean onOptionsItemSelected(final MenuItem item)
129
131
Log .i (this , "Should download repo now..." );
130
132
if (ENABLE_REPO_DOWNLOADING )
131
133
{
134
+ mProgress .setVisibility (View .VISIBLE );
132
135
mRepo .init (REPO_URL , new OnModsRepoInitialized ()); // disabled because the json is broken anyway
133
136
}
134
137
else
@@ -150,16 +153,21 @@ private void handleNoData()
150
153
151
154
private void saveModSettingsToFile ()
152
155
{
153
- mModContainer .saveToFile (new File (getDataDir (), Const .VCMI_DATA_ROOT_FOLDER_NAME + "/config/modSettings.json" ));
156
+ mModContainer .saveToFile (
157
+ new File (
158
+ Storage .getVcmiDataDir (this ),
159
+ "config/modSettings.json" ));
154
160
}
155
161
156
162
private class OnModsRepoInitialized implements VCMIModsRepo .IOnModsRepoDownloaded
157
163
{
158
164
@ Override
159
- public void onSuccess ()
165
+ public void onSuccess (ServerResponse < List < VCMIMod >> response )
160
166
{
161
167
Log .i (this , "Initialized mods repo" );
162
- // TODO update dataset
168
+ mModContainer .updateFromRepo (response .mContent );
169
+ mModsAdapter .updateModsList (mModContainer .submods ());
170
+ mProgress .setVisibility (View .GONE );
163
171
}
164
172
165
173
@ Override
@@ -206,13 +214,7 @@ protected void onPostExecute(final Void aVoid)
206
214
{
207
215
mProgress .setVisibility (View .GONE );
208
216
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 );
217
+ mModsAdapter .updateModsList (mModContainer .submods ());
216
218
}
217
219
}
218
220
}
@@ -242,14 +244,58 @@ public void onItemPressed(final ModsAdapter.ModItem mod, final RecyclerView.View
242
244
public void onDownloadPressed (final ModsAdapter .ModItem mod , final RecyclerView .ViewHolder vh )
243
245
{
244
246
Log .i (this , "Mod download pressed: " + mod );
247
+ mModsAdapter .downloadProgress (mod , "0%" );
248
+ installModAsync (mod );
245
249
}
246
250
247
251
@ Override
248
252
public void onTogglePressed (final ModsAdapter .ModItem item , final ModBaseViewHolder holder )
249
253
{
250
- item .mMod .mActive = !item .mMod .mActive ;
251
- mModsAdapter .notifyItemChanged (holder .getAdapterPosition ());
252
- saveModSettingsToFile ();
254
+ if (item .mMod .mInstalled )
255
+ {
256
+ item .mMod .mActive = !item .mMod .mActive ;
257
+ mModsAdapter .notifyItemChanged (holder .getAdapterPosition ());
258
+ saveModSettingsToFile ();
259
+ }
260
+ }
261
+ }
262
+
263
+ private void installModAsync (ModsAdapter .ModItem mod ){
264
+ File dataDir = Storage .getVcmiDataDir (this );
265
+
266
+ InstallModAsync modInstaller = new InstallModAsync (
267
+ new File (dataDir , "Mods" ),
268
+ this ,
269
+ new InstallModCallback (mod )
270
+ );
271
+
272
+ modInstaller .execute (mod .mMod .mArchiveUrl );
273
+ }
274
+
275
+ public class InstallModCallback implements InstallModAsync .PostDownload
276
+ {
277
+ private ModsAdapter .ModItem mod ;
278
+
279
+ public InstallModCallback (ModsAdapter .ModItem mod )
280
+ {
281
+ this .mod = mod ;
282
+ }
283
+
284
+ @ Override
285
+ public void downloadDone (Boolean succeed , File modFolder )
286
+ {
287
+ if (succeed ){
288
+ mModsAdapter .modInstalled (mod , modFolder );
289
+ }
290
+ }
291
+
292
+ @ Override
293
+ public void downloadProgress (String ... progress )
294
+ {
295
+ if (progress .length > 0 )
296
+ {
297
+ mModsAdapter .downloadProgress (mod , progress [0 ]);
298
+ }
253
299
}
254
300
}
255
301
}
0 commit comments