Skip to content

Commit

Permalink
feat: New features update in EventEditorActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxGalaxy committed Oct 28, 2023
1 parent a9d2d73 commit 2ca7202
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 42 deletions.
20 changes: 20 additions & 0 deletions app/src/main/java/builtin/blocks/BuiltInBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,28 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {

blockInHolder1.setBlockContent(block1ContentList);

Block blockInHolder2 = new Block();
blockInHolder2.setColor("#ff0000");
blockInHolder2.setBlockType(Block.BlockType.defaultBlock);
blockInHolder2.setName("Test");
blockInHolder2.setRawCode("I am block code %%%% DragonIDE param1 %%%%");

ArrayList<Object> block2ContentList = new ArrayList<Object>();

BlockContent block2Content1 = new BlockContent();
block2Content1.setText("Test block code");
block2ContentList.add(block2Content1);

SourceContent block2Content2 = new SourceContent();
block2Content2.setId("param1");
block2ContentList.add(block2Content2);

blockInHolder2.setBlockContent(block2ContentList);

blockList.add(blockInHolder1);

blockList.add(blockInHolder2);

holder1.setBlocks(blockList);

blocksHolder.add(holder1);
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/dragon/ide/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

public class MyApplication extends Application {
private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
private static Context mApplicationContext;

public static Context getContext() {
return mApplicationContext;
}

@Override
public void onCreate() {

mApplicationContext = getApplicationContext();
// Initiate all static imports of Environments
Environments.init();

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/dragon/ide/objects/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public String getCode() {
return new String(eventFinalCode.toString());
}

public String getFormattedCode(String s) {
StringBuilder sb = new StringBuilder();
String[] lines = getCode().split("\n");
for (int i = 0; i < lines.length; ++i) {
if (i != 0) {
sb.append(s);
}
sb.append(lines[i]);
sb.append("\n");
}
return sb.toString();
}

public String getReplacer() {
return this.replacer;
}
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/dragon/ide/objects/WebFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ public String getCode() {
String fileRawCode = new String(getRawCode());
if (!(getFileType() == WebFile.SupportedFileType.FOLDER)) {
for (int i = 0; i < getEvents().size(); ++i) {
String eventCode = getEvents().get(i).getCode();
String formatter = "";
String[] lines = getRawCode().split("\n");
for (int i2 = 0; i2 < lines.length; ++i2) {
if (lines[i2].contains(getEvents().get(i).getEventReplacer())) {
formatter =
lines[i2].substring(
0,
lines[i2].indexOf(
CodeReplacer.getReplacer(getEvents().get(i).getEventReplacer())));
}
}

String eventCode = getEvents().get(i).getFormattedCode(formatter);
String eventReplacer = getEvents().get(i).getEventReplacer();
fileRawCode = fileRawCode.replaceAll(CodeReplacer.getReplacer(eventReplacer), eventCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SourceContent() {
public String getValue() {
StringBuilder value = new StringBuilder();
value.append(new String(getSurrounder()));
value.append(new String(getValue()));
value.append(new String(super.getValue()));
value.append(new String(getSurrounder()));
return value.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.dragon.ide.ui.activities;

import android.view.DragEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.MainThread;
import com.dragon.ide.objects.Block;
import com.dragon.ide.objects.Event;
import com.dragon.ide.ui.dialogs.eventList.ShowSourceCodeDialog;
import com.dragon.ide.ui.view.BlockDefaultView;
import static com.dragon.ide.utils.Environments.PROJECTS;

import android.os.Bundle;
Expand All @@ -11,19 +22,30 @@
import com.dragon.ide.objects.WebFile;
import com.dragon.ide.ui.adapters.BlocksHolderEventEditorListItem;
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 {
private ActivityEventEditorBinding binding;
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 boolean isLoaded;
private String eventName;
private Event event;

// private View DraggingView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -50,9 +72,18 @@ public void onClick(View arg0) {
}
});

projectName = "";
projectPath = "";
fileName = "";
fileType = 0;
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");
} else {
showSection(2);
binding.tvInfo.setText(getString(R.string.project_name_not_passed));
Expand Down Expand Up @@ -81,7 +112,8 @@ public void onClick(View arg0) {
public void onCompleteLoading(ArrayList<BlocksHolder> holder) {
binding.blocksHolderList.setAdapter(
new BlocksHolderEventEditorListItem(holder, EventEditorActivity.this));
binding.blocksHolderList.setLayoutManager(new LinearLayoutManager(EventEditorActivity.this));
binding.blocksHolderList.setLayoutManager(
new LinearLayoutManager(EventEditorActivity.this));
}
});

Expand Down Expand Up @@ -133,17 +165,40 @@ public void loadFileList() {
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);
}
}
}
}

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;
runOnUiThread(
() -> {
loadBlocks(loopEvent);
showSection(3);
});
}
}

fis.close();
ois.close();

showSection(3);
} catch (Exception e) {
runOnUiThread(
() -> {
showSection(2);
binding.tvInfo.setText(
getString(R.string.an_error_occured_while_parsing_file_list));
binding.tvInfo.setText(e.getMessage());
});
}
} else {
Expand All @@ -169,4 +224,153 @@ protected void onDestroy() {
super.onDestroy();
binding = null;
}

@Override
public boolean onDrag(View v, DragEvent dragEvent) {
final int action = dragEvent.getAction();
View dragView = (View) dragEvent.getLocalState();
int index = 0;
float dropX = dragEvent.getX();
float dropY = dragEvent.getY();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_ENTERED:
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
return true;
case DragEvent.ACTION_DRAG_EXITED:
return true;
case DragEvent.ACTION_DROP:
for (int i = 0; i < ((ViewGroup) v).getChildCount(); i++) {
View child = ((ViewGroup) v).getChildAt(i);
if (dropY > child.getY() + child.getHeight() / 2) {
index = i + 1;
} else {
break;
}
}

if ((dragView instanceof BlockDefaultView)) {
BlockDefaultView blockView = new BlockDefaultView(this);
blockView.setBlock(((BlockDefaultView) dragView).getBlock());
((LinearLayout) v).addView(blockView, index);
if (blockView.getLayoutParams() != null) {
((LinearLayout.LayoutParams) blockView.getLayoutParams()).setMargins(0, -26, 0, 0);
((LinearLayout.LayoutParams) blockView.getLayoutParams()).width =
LinearLayout.LayoutParams.WRAP_CONTENT;
}
}

v.invalidate();
return true;
case DragEvent.ACTION_DRAG_ENDED:
v.invalidate();

return true;
default:
break;
}
return false;
}

public void loadBlocks(Event e) {
for (int i = 0; i < e.getBlocks().size(); ++i) {
if (e.getBlocks().get(i) instanceof Block) {
if (e.getBlocks().get(i).getBlockType() == Block.BlockType.defaultBlock) {
BlockDefaultView blockView = new BlockDefaultView(this);
blockView.setBlock(e.getBlocks().get(i));
binding.blockListEditorArea.addView(blockView, i + 1);
if (blockView.getLayoutParams() != null) {
((LinearLayout.LayoutParams) blockView.getLayoutParams()).setMargins(0, -26, 0, 0);
((LinearLayout.LayoutParams) blockView.getLayoutParams()).width =
LinearLayout.LayoutParams.WRAP_CONTENT;
}
}
}
}
}

public void saveFileList() {
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(
() -> {
try {
FileOutputStream fos =
new FileOutputStream(new File(new File(projectPath), "Files.txt"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(fileList);
fos.close();
oos.close();
finish();
} catch (Exception e) {
// Toast.makeText(this, e.getMessage(), 0).show();
}
});
}

@Override
@MainThread
public void onBackPressed() {
if (isLoaded) {
updateBlocks();
saveFileList();
}
}

@Override
protected void onPause() {
if (isLoaded) {
updateBlocks();
saveFileList();
}
super.onPause();
}

// Handle option menu
@Override
public boolean onCreateOptionsMenu(Menu arg0) {
super.onCreateOptionsMenu(arg0);
getMenuInflater().inflate(R.menu.activity_event_list_menu, arg0);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem arg0) {
if (arg0.getItemId() == R.id.show_source_code) {
updateBlocks();
if (isLoaded) {
String language = "";
switch (WebFile.getSupportedFileSuffix(file.getFileType())) {
case ".html":
language = Language.HTML;
break;
case ".css":
language = Language.CSS;
break;
case ".js":
language = Language.JavaScript;
break;
}
ShowSourceCodeDialog showSourceCodeDialog =
new ShowSourceCodeDialog(this, event.getCode(), language);
showSourceCodeDialog.show();
}
}
Toast.makeText(this, String.valueOf(event.getBlocks().size()), 0).show();
return super.onOptionsItemSelected(arg0);
}

public void updateBlocks() {
if (isLoaded) {
ArrayList<Block> arr = new ArrayList<Block>();
for (int i = 0; i < binding.blockListEditorArea.getChildCount(); ++i) {
if (binding.blockListEditorArea.getChildAt(i) instanceof BlockDefaultView) {
arr.add(((BlockDefaultView) binding.blockListEditorArea.getChildAt(i)).getBlock());
}
}
event.setBlocks(arr);
}
}
}
Loading

0 comments on commit 2ca7202

Please sign in to comment.