Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit a580283

Browse files
committed
mods removing button
1 parent c74fec8 commit a580283

File tree

8 files changed

+104
-19
lines changed

8 files changed

+104
-19
lines changed

project/vcmi-app/src/main/java/eu/vcmi/vcmi/ActivityMods.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package eu.vcmi.vcmi;
22

3+
import android.content.DialogInterface;
34
import android.os.AsyncTask;
45
import android.os.Bundle;
56

67
import androidx.annotation.Nullable;
78
import com.google.android.material.snackbar.Snackbar;
9+
10+
import androidx.appcompat.app.AlertDialog;
811
import androidx.recyclerview.widget.DefaultItemAnimator;
912
import androidx.recyclerview.widget.DividerItemDecoration;
1013
import androidx.recyclerview.widget.LinearLayoutManager;
@@ -24,6 +27,7 @@
2427
import java.util.ArrayList;
2528
import java.util.Collections;
2629
import java.util.List;
30+
import java.util.Locale;
2731

2832
import eu.vcmi.vcmi.content.ModBaseViewHolder;
2933
import eu.vcmi.vcmi.content.ModsAdapter;
@@ -251,20 +255,46 @@ public void onDownloadPressed(final ModsAdapter.ModItem mod, final RecyclerView.
251255
@Override
252256
public void onTogglePressed(final ModsAdapter.ModItem item, final ModBaseViewHolder holder)
253257
{
254-
if(item.mMod.mInstalled)
258+
if(!item.mMod.mSystem && item.mMod.mInstalled)
255259
{
256260
item.mMod.mActive = !item.mMod.mActive;
257261
mModsAdapter.notifyItemChanged(holder.getAdapterPosition());
258262
saveModSettingsToFile();
259263
}
260264
}
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+
}
261288
}
262289

263290
private void installModAsync(ModsAdapter.ModItem mod){
264291
File dataDir = Storage.getVcmiDataDir(this);
292+
File modFolder = new File(
293+
new File(dataDir, "Mods"),
294+
mod.mMod.mId.toLowerCase(Locale.US));
265295

266296
InstallModAsync modInstaller = new InstallModAsync(
267-
new File(dataDir, "Mods"),
297+
modFolder,
268298
this,
269299
new InstallModCallback(mod)
270300
);

project/vcmi-app/src/main/java/eu/vcmi/vcmi/content/ModsAdapter.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,30 @@ public void onBindViewHolder(final ModBaseViewHolder holder, final int position)
7777

7878
modHolder.mDownloadBtn.setVisibility(View.GONE);
7979
modHolder.mDownloadProgress.setVisibility(View.GONE);
80+
modHolder.mUninstall.setVisibility(View.GONE);
8081

81-
if(item.mDownloadProgress != null)
82+
if(!item.mMod.mSystem)
8283
{
83-
modHolder.mDownloadProgress.setText(item.mDownloadProgress);
84-
modHolder.mDownloadProgress.setVisibility(View.VISIBLE);
85-
}
86-
else if(!item.mMod.mInstalled){
87-
modHolder.mDownloadBtn.setVisibility(View.VISIBLE);
84+
if (item.mDownloadProgress != null)
85+
{
86+
modHolder.mDownloadProgress.setText(item.mDownloadProgress);
87+
modHolder.mDownloadProgress.setVisibility(View.VISIBLE);
88+
}
89+
else if (!item.mMod.mInstalled)
90+
{
91+
modHolder.mDownloadBtn.setVisibility(View.VISIBLE);
92+
}
93+
else if (item.mMod.installationFolder != null)
94+
{
95+
modHolder.mUninstall.setVisibility(View.VISIBLE);
96+
}
97+
98+
modHolder.itemView.setOnClickListener(v -> mItemListener.onItemPressed(item, holder));
99+
modHolder.mStatusIcon.setOnClickListener(v -> mItemListener.onTogglePressed(item, holder));
100+
modHolder.mDownloadBtn.setOnClickListener(v -> mItemListener.onDownloadPressed(item, holder));
101+
modHolder.mUninstall.setOnClickListener(v -> mItemListener.onUninstall(item, holder));
88102
}
89103

90-
modHolder.itemView.setOnClickListener(v -> mItemListener.onItemPressed(item, holder));
91-
modHolder.mStatusIcon.setOnClickListener(v -> mItemListener.onTogglePressed(item, holder));
92-
modHolder.mDownloadBtn.setOnClickListener(v -> mItemListener.onDownloadPressed(item, holder));
93104
break;
94105
case VIEWTYPE_FAILED_MOD:
95106
holder.mModName.setText(ctx.getString(R.string.mods_failed_mod_loading, item.mMod.mName));
@@ -166,6 +177,7 @@ public void modInstalled(ModItem mod, File modFolder)
166177
mod.mMod.mLoadedCorrectly = true;
167178
mod.mMod.mActive = true; // active by default
168179
mod.mMod.mInstalled = true;
180+
mod.mMod.installationFolder = modFolder;
169181
mod.mDownloadProgress = null;
170182
notifyItemChanged(mDataset.indexOf(mod));
171183
} catch (Exception ex)
@@ -180,13 +192,32 @@ public void downloadProgress(ModItem mod, String progress)
180192
notifyItemChanged(mDataset.indexOf(mod));
181193
}
182194

195+
public void modRemoved(ModItem item)
196+
{
197+
int itemIndex = mDataset.indexOf(item);
198+
199+
if(item.mMod.mArchiveUrl != null && item.mMod.mArchiveUrl != "")
200+
{
201+
item.mMod.mInstalled = false;
202+
item.mMod.installationFolder = null;
203+
204+
notifyItemChanged(itemIndex);
205+
}
206+
else{
207+
mDataset.remove(item);
208+
notifyItemRemoved(itemIndex);
209+
}
210+
}
211+
183212
public interface IOnItemAction
184213
{
185214
void onItemPressed(final ModItem mod, final RecyclerView.ViewHolder vh);
186215

187216
void onDownloadPressed(final ModItem mod, final RecyclerView.ViewHolder vh);
188217

189218
void onTogglePressed(ModItem item, ModBaseViewHolder holder);
219+
220+
void onUninstall(ModItem item, ModBaseViewHolder holder);
190221
}
191222

192223
public static class ModItem

project/vcmi-app/src/main/java/eu/vcmi/vcmi/content/ModsViewHolder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class ModsViewHolder extends ModBaseViewHolder
1919
final ImageView mStatusIcon;
2020
final View mDownloadBtn;
2121
final TextView mDownloadProgress;
22+
final View mUninstall;
2223

2324
ModsViewHolder(final View parentView)
2425
{
@@ -29,5 +30,6 @@ public class ModsViewHolder extends ModBaseViewHolder
2930
mDownloadBtn = itemView.findViewById(R.id.mods_adapter_item_btn_download);
3031
mStatusIcon = (ImageView) itemView.findViewById(R.id.mods_adapter_item_status);
3132
mDownloadProgress = (TextView) itemView.findViewById(R.id.mods_adapter_item_install_progress);
33+
mUninstall = itemView.findViewById(R.id.mods_adapter_item_btn_uninstall);
3234
}
3335
}

project/vcmi-app/src/main/java/eu/vcmi/vcmi/mods/VCMIMod.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class VCMIMod
3434
public String mModType;
3535
public String mArchiveUrl;
3636
public long mSize;
37+
public File installationFolder;
3738

3839
// config values
3940
public boolean mActive;
@@ -43,6 +44,7 @@ public class VCMIMod
4344

4445
// internal
4546
public boolean mLoadedCorrectly;
47+
public boolean mSystem;
4648

4749
protected VCMIMod()
4850
{
@@ -85,6 +87,8 @@ public static VCMIMod buildFromModInfo(final File modPath) throws IOException, J
8587
mod.mLoadedCorrectly = true;
8688
mod.mActive = true; // active by default
8789
mod.mInstalled = true;
90+
mod.installationFolder = modPath;
91+
8892
return mod;
8993
}
9094

@@ -113,6 +117,11 @@ protected static Map<String, VCMIMod> loadSubmods(final List<File> modsList) thr
113117

114118
public void updateFromConfigJson(final String id, final JSONObject obj) throws JSONException
115119
{
120+
if(mSystem)
121+
{
122+
return;
123+
}
124+
116125
mId = id.toLowerCase(Locale.US);
117126
mActive = obj.optBoolean("active");
118127
mValidated = obj.optBoolean("validated");
@@ -161,6 +170,7 @@ public boolean updateFromModInfo(final File modPath) throws IOException, JSONExc
161170
mAuthor = modInfoContent.optString("author");
162171
mContact = modInfoContent.optString("contact");
163172
mModType = modInfoContent.optString("modType");
173+
mSystem = mId.equals("vcmi");
164174

165175
final File submodsDir = new File(modPath, "Mods");
166176
if (submodsDir.exists())

project/vcmi-app/src/main/java/eu/vcmi/vcmi/mods/VCMIModsRepo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public interface IOnModsRepoDownloaded
3838

3939
private class AsyncLoadRepo extends AsyncRequest<List<VCMIMod>>
4040
{
41-
4241
@Override
4342
protected ServerResponse<List<VCMIMod>> doInBackground(final String... params)
4443
{

project/vcmi-app/src/main/java/eu/vcmi/vcmi/util/InstallModAsync.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class InstallModAsync
2424
private PostDownload callback;
2525
private File downloadLocation;
2626
private File extractLocation;
27-
private File modResultFolder;
2827
private Context context;
2928
private int totalFiles;
3029
private int unpackedFiles;
@@ -43,7 +42,11 @@ protected Boolean doInBackground(String... args)
4342

4443
try
4544
{
46-
this.downloadLocation = File.createTempFile("tmp", ".zip", extractLocation);
45+
File modsFolder = extractLocation.getParentFile();
46+
47+
if (!modsFolder.exists()) modsFolder.mkdir();
48+
49+
this.downloadLocation = File.createTempFile("tmp", ".zip", modsFolder);
4750

4851
URL url = new URL(args[0]);
4952
URLConnection connection = url.openConnection();
@@ -91,10 +94,11 @@ protected Boolean doInBackground(String... args)
9194
output.close();
9295
input.close();
9396

94-
File tempDir = File.createTempFile("tmp", "", extractLocation);
97+
File tempDir = File.createTempFile("tmp", "", modsFolder);
9598

9699
tempDir.delete();
97100
tempDir.mkdir();
101+
98102
if (!extractLocation.exists()) extractLocation.mkdir();
99103

100104
try
@@ -129,7 +133,7 @@ protected void onProgressUpdate(String... values)
129133
@Override
130134
protected void onPostExecute(Boolean result)
131135
{
132-
if (callback != null) callback.downloadDone(result, modResultFolder);
136+
if (callback != null) callback.downloadDone(result, extractLocation);
133137
}
134138

135139
private boolean moveModToExtractLocation(File tempDir)
@@ -151,11 +155,10 @@ public boolean accept(File file)
151155
if (modJson != null && modJson.length > 0)
152156
{
153157
File modFolder = modJson[0].getParentFile();
154-
modResultFolder = new File(extractLocation, modFolder.getName());
155158

156-
if (!modFolder.renameTo(modResultFolder))
159+
if (!modFolder.renameTo(extractLocation))
157160
{
158-
FileUtil.copyDir(modFolder, modResultFolder);
161+
FileUtil.copyDir(modFolder, extractLocation);
159162
}
160163

161164
return true;

project/vcmi-app/src/main/res/layout/mods_adapter_item.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@
8383
android:padding="@dimen/side_margin"
8484
android:src="@android:drawable/stat_sys_download" />
8585

86+
<androidx.appcompat.widget.AppCompatImageButton
87+
android:id="@+id/mods_adapter_item_btn_uninstall"
88+
android:layout_width="wrap_content"
89+
android:layout_height="wrap_content"
90+
android:background="?attr/selectableItemBackgroundBorderless"
91+
android:padding="@dimen/side_margin"
92+
android:src="@android:drawable/ic_menu_delete" />
93+
8694
<TextView
8795
android:layout_width="wrap_content"
8896
android:layout_height="wrap_content"

project/vcmi-app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
<string name="mods_title">Detected mods</string>
5050
<string name="mods_failed_mod_loading">Could not load the mod in \'%1$s\' folder</string>
51+
<string name="mods_removal_title">Removing %1$s</string>
52+
<string name="mods_removal_confirmation">Are you sure you want to remove %1$s</string>
5153

5254
<string name="about_title">About application</string>
5355
<string name="about_version_app">App version: %1$s</string>

0 commit comments

Comments
 (0)