Skip to content

Commit

Permalink
Merge pull request #2 from CyberGlitch01/dev
Browse files Browse the repository at this point in the history
Saparate project file, event, WebFile into multiple files to reduce lag
  • Loading branch information
SyntaxGalaxy authored Dec 9, 2023
2 parents 0890d7f + a542dba commit 864ace5
Show file tree
Hide file tree
Showing 17 changed files with 660 additions and 275 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dragon.ide.listeners;

import com.dragon.ide.objects.Event;
import java.util.ArrayList;

public interface EventAddListener {
void onAdd(ArrayList<Event> events);
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/dragon/ide/listeners/TaskListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.dragon.ide.listeners;

public interface TaskListener {
public void onSuccess(Object result);
}
21 changes: 6 additions & 15 deletions app/src/main/java/com/dragon/ide/objects/WebFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class WebFile implements Serializable {
public static final long serialVersionUID = 428383835L;
private String filePath;
private int fileType;
private ArrayList<WebFile> fileList;
private ArrayList<Event> events;
private String rawCode;

Expand All @@ -28,14 +27,6 @@ public void setFileType(int fileType) {
this.fileType = fileType;
}

public ArrayList<WebFile> getFileList() {
return this.fileList;
}

public void setFileList(ArrayList<WebFile> fileList) {
this.fileList = fileList;
}

public ArrayList<Event> getEvents() {
if (events != null) {
return this.events;
Expand All @@ -58,24 +49,24 @@ public void setRawCode(String rawCode) {
this.rawCode = rawCode;
}

public String getCode() {
public String getCode(ArrayList<Event> events) {
String fileRawCode = new String(getRawCode());
if (!(getFileType() == WebFile.SupportedFileType.FOLDER)) {
for (int i = 0; i < getEvents().size(); ++i) {
for (int i = 0; i < events.size(); ++i) {
String formatter = "";
String[] lines = getRawCode().split("\n");
for (int i2 = 0; i2 < lines.length; ++i2) {
if (lines[i2].contains(getEvents().get(i).getEventReplacer())) {
if (lines[i2].contains(events.get(i).getEventReplacer())) {
formatter =
lines[i2].substring(
0,
lines[i2].indexOf(
CodeReplacer.getReplacer(getEvents().get(i).getEventReplacer())));
CodeReplacer.getReplacer(events.get(i).getEventReplacer())));
}
}

String eventCode = getEvents().get(i).getFormattedCode(formatter);
String eventReplacer = getEvents().get(i).getEventReplacer();
String eventCode = events.get(i).getFormattedCode(formatter);
String eventReplacer = events.get(i).getEventReplacer();
fileRawCode = fileRawCode.replaceAll(CodeReplacer.getReplacer(eventReplacer), eventCode);
}
}
Expand Down
177 changes: 50 additions & 127 deletions app/src/main/java/com/dragon/ide/ui/activities/EventEditorActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.dragon.ide.ui.activities;

import static com.dragon.ide.utils.Environments.PROJECTS;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
Expand All @@ -13,10 +11,12 @@
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.MainThread;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.dragon.ide.R;
import com.dragon.ide.databinding.ActivityEventEditorBinding;
import com.dragon.ide.listeners.TaskListener;
import com.dragon.ide.objects.Block;
import com.dragon.ide.objects.BlocksHolder;
import com.dragon.ide.objects.ComplexBlock;
Expand All @@ -28,31 +28,33 @@
import com.dragon.ide.ui.view.BlockDefaultView;
import com.dragon.ide.ui.view.ComplexBlockView;
import com.dragon.ide.utils.BlocksHandler;
import com.dragon.ide.utils.DeserializationException;
import com.dragon.ide.utils.DeserializerUtils;
import com.dragon.ide.utils.Utils;
import com.dragon.ide.utils.eventeditor.BlocksListLoader;
import editor.tsd.tools.Language;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class EventEditorActivity extends BaseActivity implements View.OnDragListener {
public ActivityEventEditorBinding binding;
private ArrayList<WebFile> fileList;
private WebFile file;
private ArrayList<BlocksHolder> blocksHolder;
private String projectName;
private String projectPath;
private String fileName;
private int fileType;
private String webFilePath;
private boolean isLoaded;
private String eventName;

// Event
private String eventFilePath;
private Event event;
private String language;

// Shadow
private LinearLayout blockShadow;

@Override
Expand All @@ -64,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(binding.getRoot());

// Initialize to avoid null error
fileList = new ArrayList<WebFile>();
blocksHolder = new ArrayList<BlocksHolder>();

// Setup toolbar.
Expand All @@ -82,16 +83,38 @@ public void onClick(View arg0) {

projectName = "";
projectPath = "";
fileName = "";
fileType = 0;
webFilePath = "";
eventFilePath = "";
isLoaded = false;

if (getIntent().hasExtra("projectName")) {
projectName = getIntent().getStringExtra("projectName");
projectPath = getIntent().getStringExtra("projectPath");
fileName = getIntent().getStringExtra("fileName");
fileType = getIntent().getIntExtra("fileType", 1);
eventName = getIntent().getStringExtra("eventName");
webFilePath = getIntent().getStringExtra("webFilePath");
eventFilePath = getIntent().getStringExtra("eventFilePath");
try {
DeserializerUtils.deserializeWebfile(
new File(webFilePath),
new TaskListener() {
@Override
public void onSuccess(Object mWebFile) {
file = (WebFile) mWebFile;
}
});
} catch (DeserializationException e) {
}

try {
DeserializerUtils.deserializeEvent(
new File(eventFilePath),
new TaskListener() {
@Override
public void onSuccess(Object mEvent) {
event = (Event) mEvent;
}
});
} catch (DeserializationException e) {
}
} else {
showSection(2);
binding.tvInfo.setText(getString(R.string.project_name_not_passed));
Expand All @@ -114,14 +137,23 @@ public void onClick(View arg0) {

/*
* Ask for storage permission if not granted.
* Load projects if storage permission is granted.
* Load event if storage permission is granted.
*/
if (!MainActivity.isStoagePermissionGranted(this)) {
showSection(2);
binding.tvInfo.setText(R.string.storage_permission_denied);
MainActivity.showStoragePermissionDialog(this);
} else {
loadFileList();
if (event != null && file != null) {
showSection(1);
isLoaded = true;
loadBlocks(event);
showSection(3);
} else {
showSection(2);
isLoaded = false;
binding.tvInfo.setText(getText(R.string.an_error_occured_while_parsing_event));
}
}

/*
Expand Down Expand Up @@ -168,114 +200,6 @@ public void showSection(int section) {
}
}

public void loadFileList() {
// List is loading, so it shows loading view.
showSection(1);

// Load project list in a saparate thread to avoid UI freeze.
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(
() -> {
if (PROJECTS.exists()) {
if (!new File(projectPath).exists()) {
showSection(2);
binding.tvInfo.setText(getString(R.string.project_not_found));
} else {
if (new File(new File(projectPath), "Files.txt").exists()) {
try {
FileInputStream fis =
new FileInputStream(new File(new File(projectPath), "Files.txt"));
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
if (obj instanceof ArrayList) {
fileList = (ArrayList<WebFile>) obj;

for (int i = 0; i < fileList.size(); ++i) {
if (fileList
.get(i)
.getFilePath()
.toLowerCase()
.equals(fileName.toLowerCase())) {
if (fileList.get(i).getFileType() == fileType) {
file = fileList.get(i);
}
}
}
if (file == null) {
runOnUiThread(
() -> {
showSection(2);
binding.tvInfo.setText("File not not found");
return;
});
}
} else {
runOnUiThread(
() -> {
showSection(2);
binding.tvInfo.setText("Not an instance of WebFile");
});
}

for (int i2 = 0; i2 < file.getEvents().size(); ++i2) {
Event loopEvent = file.getEvents().get(i2);
if (eventName.toLowerCase().equals(loopEvent.getName().toLowerCase())) {
event = file.getEvents().get(i2);
isLoaded = true;
switch (WebFile.getSupportedFileSuffix(file.getFileType())) {
case ".html":
language = Language.HTML;
break;
case ".css":
language = Language.CSS;
break;
case ".js":
language = Language.JavaScript;
break;
}
runOnUiThread(
() -> {
loadBlocks(loopEvent);
showSection(3);
});
}
}

if (!isLoaded) {
runOnUiThread(
() -> {
showSection(2);
binding.tvInfo.setText("Event not found");
});
}

fis.close();
ois.close();
} catch (Exception e) {
runOnUiThread(
() -> {
showSection(2);
binding.tvInfo.setText(e.getMessage());
});
}
} else {
runOnUiThread(
() -> {
showSection(2);
binding.tvInfo.setText(getString(R.string.no_files_yet));
});
}
}
} else {
runOnUiThread(
() -> {
showSection(2);
binding.tvInfo.setText(getString(R.string.project_not_found));
});
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down Expand Up @@ -780,10 +704,9 @@ public void saveFileList(boolean exitAfterSave) {
executor.execute(
() -> {
try {
FileOutputStream fos =
new FileOutputStream(new File(new File(projectPath), "Files.txt"));
FileOutputStream fos = new FileOutputStream(new File(eventFilePath));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(fileList);
oos.writeObject(event);
fos.close();
oos.close();
if (exitAfterSave) {
Expand Down
Loading

0 comments on commit 864ace5

Please sign in to comment.