Skip to content

Commit

Permalink
fix sum bugs, improve ui a lil bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatMG393 committed Jul 28, 2023
1 parent db332dc commit 99773f7
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.imageview.ShapeableImageView;

import com.google.android.material.textview.MaterialTextView;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
Expand All @@ -25,6 +21,7 @@
import com.thatmg393.esmanager.adapters.ModListAdapter;
import com.thatmg393.esmanager.managers.rpc.impl.RPCSocketClient;
import com.thatmg393.esmanager.models.ModPropertiesModel;
import com.thatmg393.esmanager.utils.ActivityUtils;

import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -64,42 +61,51 @@ public void init() {

modsLoadingLayout = requireView().findViewById(R.id.fragment_mod_loading_container);
modsEmptyLayout = requireView().findViewById(R.id.fragment_mod_empty_container);

((MaterialTextView)modsEmptyLayout.findViewById(R.id.list_empty_desc)).setText("No mods/s found");
}

private void populateModsList() {
Executors.newSingleThreadExecutor().execute(() -> {
DocumentFileCompat modFolder = DocumentFileCompat.fromTreeUri(requireContext(), GlobalConstants.ES_MOD_FOLDER);
List<DocumentFileCompat> modFolders = modFolder.listFiles();
try {
DocumentFileCompat modFolder = DocumentFileCompat.fromTreeUri(requireContext(), GlobalConstants.ES_MOD_FOLDER);
List<DocumentFileCompat> modFolders = modFolder.listFiles();

if (modsRecyclerAdapter.getDataList().size() > 0) modsRecyclerView.post(() -> modsRecyclerAdapter.clearData());
if (modFolders != null || modFolders.size() > 0) {
for (DocumentFileCompat folder : modFolders) {
if (folder.isFile()) continue;
if (folder.getName().toLowerCase().equals("tools")) continue;
if (modsRecyclerAdapter.getDataList().size() > 0) modsRecyclerView.post(() -> modsRecyclerAdapter.clearData());
if (modFolders != null || modFolders.size() > 0) {
for (DocumentFileCompat folder : modFolders) {
if (folder.isFile()) continue;
if (folder.getName().toLowerCase().equals("tools")) continue;

DocumentFileCompat jsonFile = DocumentFileCompat.fromSingleUri(requireContext(), Uri.parse(folder.getUri().toString() + "%2F" + modInfoJson));
if (jsonFile.exists() && jsonFile.isFile()) {
try (InputStream jsonIS = requireContext().getContentResolver().openInputStream(jsonFile.getUri())) {
JsonObject j = RPCSocketClient.GSON.fromJson(IOUtils.toString(jsonIS, StandardCharsets.UTF_8), JsonObject.class);
DocumentFileCompat jsonFile = DocumentFileCompat.fromSingleUri(requireContext(), Uri.parse(folder.getUri().toString() + "%2F" + modInfoJson));
if (jsonFile.exists() && jsonFile.isFile()) {
try (InputStream jsonIS = requireContext().getContentResolver().openInputStream(jsonFile.getUri())) {
JsonObject j = RPCSocketClient.GSON.fromJson(IOUtils.toString(jsonIS, StandardCharsets.UTF_8), JsonObject.class);

String modName = j.get("name").getAsString();
String modDesc = j.get("description").getAsString();
String modAuthor = j.get("author").getAsString();
String modVersion = j.get("version").getAsString();
String modPreview = folder.getUri().toString() + "%2F" + j.get("preview").getAsString().replace("/", "%2F");
String modPath = folder.getUri().toString();
String modName = j.get("name").getAsString();
String modDesc = j.get("description").getAsString();
String modAuthor = j.get("author").getAsString();
String modVersion = j.get("version").getAsString();
String modPreview = folder.getUri().toString() + "%2F" + j.get("preview").getAsString().replace("/", "%2F");
String modPath = folder.getUri().toString();

modsRecyclerView.post(() -> modsRecyclerAdapter.addData(new ModPropertiesModel(modName, modDesc, modAuthor, modVersion, modPreview, modPath)));
} catch (IOException | JsonSyntaxException e) {
modsRecyclerView.post(() -> modsRecyclerAdapter.addData(new ModPropertiesModel(folder.getName(), null, null, null, null, folder.getUri().toString())));
modsRecyclerView.post(() -> modsRecyclerAdapter.addData(new ModPropertiesModel(modName, modDesc, modAuthor, modVersion, modPreview, modPath)));
} catch (IOException | JsonSyntaxException e) {
modsRecyclerView.post(() -> modsRecyclerAdapter.addData(new ModPropertiesModel(folder.getName(), null, null, null, null, folder.getUri().toString())));
}
}
}
}
modsRecyclerView.post(() -> modsRecyclerView.setVisibility(View.VISIBLE));
modsRecyclerView.post(() -> modsLoadingLayout.setVisibility(View.GONE));
} else {
}
modsRecyclerView.post(() -> modsRecyclerView.setVisibility(View.VISIBLE));
modsRecyclerView.post(() -> modsLoadingLayout.setVisibility(View.GONE));
} else {
modsRecyclerView.post(() -> modsLoadingLayout.setVisibility(View.GONE));
modsRecyclerView.post(() -> modsEmptyLayout.setVisibility(View.VISIBLE));
}
} catch (Exception e) {
modsRecyclerView.post(() -> modsLoadingLayout.setVisibility(View.GONE));
modsRecyclerView.post(() -> modsEmptyLayout.setVisibility(View.VISIBLE));

modsRecyclerView.post(() -> ActivityUtils.getInstance().showToast("Failed to load mods\n" + e.getClass().getName() + "\n" + e.getMessage(), Toast.LENGTH_SHORT));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.thatmg393.esmanager.fragments.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textview.MaterialTextView;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.lazygeniouz.dfc.file.DocumentFileCompat;
import com.thatmg393.esmanager.GlobalConstants;
import com.thatmg393.esmanager.R;
import com.thatmg393.esmanager.activities.ProjectActivity;
Expand All @@ -25,8 +24,8 @@
import com.thatmg393.esmanager.models.ModPropertiesModel;
import com.thatmg393.esmanager.models.ProjectModel;
import com.thatmg393.esmanager.utils.ActivityUtils;

import com.thatmg393.esmanager.utils.FileUtils;

import org.apache.commons.io.IOUtils;

import java.io.File;
Expand Down Expand Up @@ -68,6 +67,8 @@ public void init() {
projectsLoadingLayout = requireView().findViewById(R.id.fragment_project_loading_container);
projectsEmptyLayout = requireView().findViewById(R.id.fragment_project_empty_container);

((MaterialTextView)projectsEmptyLayout.findViewById(R.id.list_empty_desc)).setText("No project/s found");

projectsRecyclerAdapter.setItemClickListener((v, pos) -> {
ActivityUtils.getInstance().showPopupMenuAt(
v,
Expand All @@ -88,10 +89,10 @@ public void init() {
File project = new File(projectsRecyclerAdapter.getDataList().get(pos).getModPath());
FileUtils.deleteRecursively(project);
if (!project.exists()) {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_success), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_success), Toast.LENGTH_SHORT);
populateProjectList();
} else {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_failed), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_failed), Toast.LENGTH_SHORT);
}
return true;
}
Expand All @@ -103,36 +104,43 @@ public void init() {

private void populateProjectList() {
Executors.newSingleThreadExecutor().execute(() -> {
File[] projectFolders = new File(GlobalConstants.ESM_ROOT_FOLDER, "Projects").listFiles((file, name) -> {
return file.isDirectory();
});
try {
File[] projectFolders = new File(GlobalConstants.ESM_ROOT_FOLDER, "Projects").listFiles((file, name) -> {
return file.isDirectory();
});

if (projectsRecyclerAdapter.getDataList().size() > 0) projectsRecyclerView.post(() -> projectsRecyclerAdapter.clearData());
if (projectFolders != null && projectFolders.length > 0) {
for (File folder : projectFolders) {
File jsonFile = new File(folder.getAbsolutePath(), modInfoJson);
if (jsonFile.exists() && jsonFile.isFile()) {
try (InputStream jsonIS = new FileInputStream(jsonFile)) {
JsonObject j = RPCSocketClient.GSON.fromJson(IOUtils.toString(jsonIS, StandardCharsets.UTF_8), JsonObject.class);
if (projectsRecyclerAdapter.getDataList().size() > 0) projectsRecyclerView.post(() -> projectsRecyclerAdapter.clearData());
if (projectFolders != null && projectFolders.length > 0) {
for (File folder : projectFolders) {
File jsonFile = new File(folder.getAbsolutePath(), modInfoJson);
if (jsonFile.exists() && jsonFile.isFile()) {
try (InputStream jsonIS = new FileInputStream(jsonFile)) {
JsonObject j = RPCSocketClient.GSON.fromJson(IOUtils.toString(jsonIS, StandardCharsets.UTF_8), JsonObject.class);

String projectName = j.get("name").getAsString();
String projectDesc = j.get("description").getAsString();
String projectAuthor = j.get("author").getAsString();
String projectVersion = j.get("version").getAsString();
String projectPreview = new File(folder.getAbsolutePath(), j.get("preview").getAsString()).getAbsolutePath();
String projectPath = folder.getAbsolutePath();
String projectName = j.get("name").getAsString();
String projectDesc = j.get("description").getAsString();
String projectAuthor = j.get("author").getAsString();
String projectVersion = j.get("version").getAsString();
String projectPreview = new File(folder.getAbsolutePath(), j.get("preview").getAsString()).getAbsolutePath();
String projectPath = folder.getAbsolutePath();

projectsRecyclerView.post(() -> projectsRecyclerAdapter.addData(new ModPropertiesModel(projectName, projectDesc, projectAuthor, projectVersion, projectPreview, projectPath)));
} catch (IOException | JsonSyntaxException e) {
projectsRecyclerView.post(() -> projectsRecyclerAdapter.addData(new ModPropertiesModel(folder.getName(), null, null, null, null, folder.getAbsolutePath())));
projectsRecyclerView.post(() -> projectsRecyclerAdapter.addData(new ModPropertiesModel(projectName, projectDesc, projectAuthor, projectVersion, projectPreview, projectPath)));
} catch (IOException | JsonSyntaxException e) {
projectsRecyclerView.post(() -> projectsRecyclerAdapter.addData(new ModPropertiesModel(folder.getName(), null, null, null, null, folder.getAbsolutePath())));
}
}
}
}
projectsRecyclerView.post(() -> projectsRecyclerView.setVisibility(View.VISIBLE));
projectsRecyclerView.post(() -> projectsLoadingLayout.setVisibility(View.GONE));
} else {
}
projectsRecyclerView.post(() -> projectsRecyclerView.setVisibility(View.VISIBLE));
projectsRecyclerView.post(() -> projectsLoadingLayout.setVisibility(View.GONE));
} else {
projectsRecyclerView.post(() -> projectsLoadingLayout.setVisibility(View.GONE));
projectsRecyclerView.post(() -> projectsEmptyLayout.setVisibility(View.VISIBLE));
}
} catch (Exception e) {
projectsRecyclerView.post(() -> projectsLoadingLayout.setVisibility(View.GONE));
projectsRecyclerView.post(() -> projectsEmptyLayout.setVisibility(View.VISIBLE));

projectsRecyclerView.post(() -> ActivityUtils.getInstance().showToast("Failed to load projects\n" + e.getClass().getName() + "\n" + e.getMessage(), Toast.LENGTH_SHORT));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ private void init() {
String newNameOfTarget = name.getText().toString();

if (FileUtils.moveTo(target, requireContext(), new File(target.getParent(), newNameOfTarget)) == null) {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_failed), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_failed), Toast.LENGTH_SHORT);
} else {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_success), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_success), Toast.LENGTH_SHORT);
refreshOrPopulateTreeView();
}
})
Expand All @@ -157,9 +157,9 @@ private void init() {
new Pair<>(getString(R.string.file_drawer_popup_cancel), (dialog, which) -> dialog.dismiss()),
new Pair<>(getString(R.string.file_drawer_popup_create), (dialog, which) -> {
if (!FileUtils.forceDelete(new File((String) node.getValue()))) {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_failed), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_failed), Toast.LENGTH_SHORT);
} else {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_success), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_success), Toast.LENGTH_SHORT);
refreshOrPopulateTreeView();
}
})
Expand All @@ -184,9 +184,9 @@ private void init() {
File newFile = new File((String) node.getValue(), name.getText().toString());

if (!FileUtils.createNewFileIfPossible(newFile)) {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_failed), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_failed), Toast.LENGTH_SHORT);
} else {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_success), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_success), Toast.LENGTH_SHORT);
refreshOrPopulateTreeView();
}
})
Expand All @@ -202,9 +202,9 @@ private void init() {
File file = new File((String) node.getValue(), name.getText().toString());

if (FileUtils.makeFolder(new File((String) node.getValue()), requireContext(), name.getText().toString(), CreateMode.SKIP_IF_EXISTS) == null) {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_failed), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_failed), Toast.LENGTH_SHORT);
} else {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_success), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_success), Toast.LENGTH_SHORT);
refreshOrPopulateTreeView();
}
})
Expand All @@ -223,9 +223,9 @@ private void init() {
String newNameOfTarget = name.getText().toString();

if (FileUtils.moveTo(target, requireContext(), new File(target.getParent(), newNameOfTarget)) == null) {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_failed), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_failed), Toast.LENGTH_SHORT);
} else {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_success), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_success), Toast.LENGTH_SHORT);
refreshOrPopulateTreeView();
}
})
Expand All @@ -238,9 +238,9 @@ private void init() {
new Pair<>(getString(R.string.file_drawer_popup_cancel), (dialog, which) -> dialog.dismiss()),
new Pair<>(getString(R.string.file_drawer_popup_create), (dialog, which) -> {
if (!FileUtils.forceDelete(new File((String) node.getValue()))) {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_failed), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_failed), Toast.LENGTH_SHORT);
} else {
ActivityUtils.getInstance().showToast(getString(R.string.file_drawer_toast_success), Toast.LENGTH_SHORT);
ActivityUtils.getInstance().showToast(getString(R.string.toast_success), Toast.LENGTH_SHORT);
refreshOrPopulateTreeView();
}
})
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/res/layout/list_empty_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
android:layout_width="match_parent">

<com.google.android.material.imageview.ShapeableImageView
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_width="48dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_danger_outline"
android:id="@+id/list_empty_icon" />

<com.google.android.material.textview.MaterialTextView
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_below="@id/list_empty_icon"
android:layout_centerInParent="true"
android:textColor="@color/colorPrimary"
android:textSize="16sp"
android:layout_below="@id/list_empty_icon"
android:layout_marginTop="16dp"
android:id="@+id/list_empty_desc" />

</RelativeLayout>
7 changes: 5 additions & 2 deletions app/src/main/res/layout/list_loading_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

<com.google.android.material.textview.MaterialTextView
android:layout_height="wrap_content"
android:layout_below="@id/list_loading_loading_bar"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_name" />
android:textColor="@color/colorPrimary"
android:textSize="16sp"
android:layout_below="@id/list_loading_loading_bar"
android:layout_marginTop="16dp"
android:text="@string/layout_list_bar_loading" />

</RelativeLayout>
8 changes: 6 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<string name="nav_drawer_open">Open Navigation Drawer</string>
<string name="nav_drawer_close">Open Navigation Drawer</string>

<string name="toast_success">Success!</string>
<string name="toast_failed">Failed!</string>

<!-- File Drawer -->
<string name="file_drawer_no_files">Folder does not exist</string>
<string name="file_drawer_popup_new_file">New file</string>
Expand All @@ -14,7 +17,8 @@
<string name="file_drawer_popup_delete_confirmation">Are you sure?</string>
<string name="file_drawer_popup_create">Create</string>
<string name="file_drawer_popup_cancel">Cancel</string>
<string name="file_drawer_toast_success">Success!</string>
<string name="file_drawer_toast_failed">Failed!</string>

<!-- List layout -->
<string name="layout_list_bar_loading">Loading...</string>

</resources>

0 comments on commit 99773f7

Please sign in to comment.