From 2ca72022e94008bf052cfe9b7c944c68cd443c8a Mon Sep 17 00:00:00 2001 From: DevKumar00001 Date: Sat, 28 Oct 2023 11:33:38 +0530 Subject: [PATCH] feat: New features update in EventEditorActivity --- .../java/builtin/blocks/BuiltInBlocks.java | 20 ++ .../java/com/dragon/ide/MyApplication.java | 7 +- .../java/com/dragon/ide/objects/Event.java | 13 ++ .../java/com/dragon/ide/objects/WebFile.java | 14 +- .../objects/blockcontent/SourceContent.java | 2 +- .../ui/activities/EventEditorActivity.java | 218 +++++++++++++++++- .../ide/ui/activities/EventListActivity.java | 17 +- .../ide/ui/adapters/BlockListAdapter.java | 81 +++++++ .../BlocksHolderEventEditorListItem.java | 19 +- .../dragon/ide/ui/view/BlockDefaultView.java | 49 ++-- .../main/res/layout/activity_event_editor.xml | 13 +- 11 files changed, 411 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/com/dragon/ide/ui/adapters/BlockListAdapter.java diff --git a/app/src/main/java/builtin/blocks/BuiltInBlocks.java b/app/src/main/java/builtin/blocks/BuiltInBlocks.java index e8502c8..4c42987 100644 --- a/app/src/main/java/builtin/blocks/BuiltInBlocks.java +++ b/app/src/main/java/builtin/blocks/BuiltInBlocks.java @@ -35,8 +35,28 @@ public static ArrayList 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 block2ContentList = new ArrayList(); + + 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); diff --git a/app/src/main/java/com/dragon/ide/MyApplication.java b/app/src/main/java/com/dragon/ide/MyApplication.java index f55f800..ca98e55 100644 --- a/app/src/main/java/com/dragon/ide/MyApplication.java +++ b/app/src/main/java/com/dragon/ide/MyApplication.java @@ -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(); diff --git a/app/src/main/java/com/dragon/ide/objects/Event.java b/app/src/main/java/com/dragon/ide/objects/Event.java index d545b75..349e135 100644 --- a/app/src/main/java/com/dragon/ide/objects/Event.java +++ b/app/src/main/java/com/dragon/ide/objects/Event.java @@ -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; } diff --git a/app/src/main/java/com/dragon/ide/objects/WebFile.java b/app/src/main/java/com/dragon/ide/objects/WebFile.java index a59cfe5..cbde0b4 100644 --- a/app/src/main/java/com/dragon/ide/objects/WebFile.java +++ b/app/src/main/java/com/dragon/ide/objects/WebFile.java @@ -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); } diff --git a/app/src/main/java/com/dragon/ide/objects/blockcontent/SourceContent.java b/app/src/main/java/com/dragon/ide/objects/blockcontent/SourceContent.java index 4d1651f..6342267 100644 --- a/app/src/main/java/com/dragon/ide/objects/blockcontent/SourceContent.java +++ b/app/src/main/java/com/dragon/ide/objects/blockcontent/SourceContent.java @@ -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(); } diff --git a/app/src/main/java/com/dragon/ide/ui/activities/EventEditorActivity.java b/app/src/main/java/com/dragon/ide/ui/activities/EventEditorActivity.java index 95e8a88..670b879 100644 --- a/app/src/main/java/com/dragon/ide/ui/activities/EventEditorActivity.java +++ b/app/src/main/java/com/dragon/ide/ui/activities/EventEditorActivity.java @@ -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; @@ -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 fileList; + private WebFile file; private ArrayList 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) { @@ -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)); @@ -81,7 +112,8 @@ public void onClick(View arg0) { public void onCompleteLoading(ArrayList holder) { binding.blocksHolderList.setAdapter( new BlocksHolderEventEditorListItem(holder, EventEditorActivity.this)); - binding.blocksHolderList.setLayoutManager(new LinearLayoutManager(EventEditorActivity.this)); + binding.blocksHolderList.setLayoutManager( + new LinearLayoutManager(EventEditorActivity.this)); } }); @@ -133,17 +165,40 @@ public void loadFileList() { Object obj = ois.readObject(); if (obj instanceof ArrayList) { fileList = (ArrayList) 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 { @@ -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 arr = new ArrayList(); + 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); + } + } } diff --git a/app/src/main/java/com/dragon/ide/ui/activities/EventListActivity.java b/app/src/main/java/com/dragon/ide/ui/activities/EventListActivity.java index 81ac3be..45219fc 100644 --- a/app/src/main/java/com/dragon/ide/ui/activities/EventListActivity.java +++ b/app/src/main/java/com/dragon/ide/ui/activities/EventListActivity.java @@ -134,14 +134,17 @@ private void showEventList() { projectPath, fileName, fileType)); - binding.list.setLayoutManager(new LinearLayoutManager(EventListActivity.this)); - showSection(3); + runOnUiThread( + () -> { + binding.list.setLayoutManager( + new LinearLayoutManager(EventListActivity.this)); + 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 { @@ -243,4 +246,10 @@ public boolean onOptionsItemSelected(MenuItem arg0) { } return super.onOptionsItemSelected(arg0); } + + @Override + protected void onResume() { + showEventList(); + super.onResume(); + } } diff --git a/app/src/main/java/com/dragon/ide/ui/adapters/BlockListAdapter.java b/app/src/main/java/com/dragon/ide/ui/adapters/BlockListAdapter.java new file mode 100644 index 0000000..54a1615 --- /dev/null +++ b/app/src/main/java/com/dragon/ide/ui/adapters/BlockListAdapter.java @@ -0,0 +1,81 @@ +package com.dragon.ide.ui.adapters; + +import android.app.Activity; +import android.content.ClipData; +import android.os.Build; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import androidx.recyclerview.widget.RecyclerView; +import com.dragon.ide.objects.Block; +import com.dragon.ide.objects.ComplexBlock; +import com.dragon.ide.objects.DoubleComplexBlock; +import com.dragon.ide.ui.activities.EventEditorActivity; +import com.dragon.ide.ui.view.BlockDefaultView; +import java.util.ArrayList; + +public class BlockListAdapter extends RecyclerView.Adapter { + + public ArrayList list; + public Activity activity; + + public BlockListAdapter(ArrayList _arr, Activity activity) { + list = _arr; + this.activity = activity; + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + LinearLayout _v = new LinearLayout(activity); + RecyclerView.LayoutParams _lp = + new RecyclerView.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + _v.setLayoutParams(_lp); + return new ViewHolder(_v); + } + + @Override + public void onBindViewHolder(ViewHolder _holder, int _position) { + LinearLayout v = (LinearLayout) _holder.itemView; + v.setPadding(8, 8, 8, 8); + if (!(list.get(_position) instanceof DoubleComplexBlock) + && !(list.get(_position) instanceof ComplexBlock)) { + if (list.get(_position) instanceof Block) { + if (list.get(_position).getBlockType() == Block.BlockType.defaultBlock) { + BlockDefaultView blockView = new BlockDefaultView(activity); + blockView.setBlock(list.get(_position)); + v.addView(blockView); + blockView.setOnLongClickListener( + (view) -> { + ClipData data = ClipData.newPlainText("", ""); + View.DragShadowBuilder shadow = new View.DragShadowBuilder(blockView); + + ((EventEditorActivity) activity) + .binding.blockListEditorArea.setOnDragListener( + ((EventEditorActivity) activity)); + + if (Build.VERSION.SDK_INT >= 24) { + blockView.startDragAndDrop(data, shadow, blockView, 1); + } else { + blockView.startDrag(data, shadow, blockView, 1); + } + return true; + }); + } else { + ((EventEditorActivity) activity).binding.blockListEditorArea.setOnDragListener(null); + } + } + } + } + + @Override + public int getItemCount() { + return list.size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder { + public ViewHolder(View v) { + super(v); + } + } +} diff --git a/app/src/main/java/com/dragon/ide/ui/adapters/BlocksHolderEventEditorListItem.java b/app/src/main/java/com/dragon/ide/ui/adapters/BlocksHolderEventEditorListItem.java index 72c55e2..ba6b0a5 100644 --- a/app/src/main/java/com/dragon/ide/ui/adapters/BlocksHolderEventEditorListItem.java +++ b/app/src/main/java/com/dragon/ide/ui/adapters/BlocksHolderEventEditorListItem.java @@ -1,24 +1,23 @@ package com.dragon.ide.ui.adapters; -import android.app.Activity; -import android.content.Intent; import android.graphics.Color; import android.view.View; import android.view.ViewGroup; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.dragon.ide.R; -import com.dragon.ide.databinding.LayoutBlocksHolderListItemBinding; import com.dragon.ide.databinding.LayoutEventBlocksHolderListItemBinding; import com.dragon.ide.objects.BlocksHolder; -import com.dragon.ide.ui.activities.BlockManagerActivity; +import com.dragon.ide.ui.activities.EventEditorActivity; import java.util.ArrayList; -public class BlocksHolderEventEditorListItem extends RecyclerView.Adapter { +public class BlocksHolderEventEditorListItem + extends RecyclerView.Adapter { public ArrayList list; - public Activity activity; + public EventEditorActivity activity; - public BlocksHolderEventEditorListItem(ArrayList _arr, Activity activity) { + public BlocksHolderEventEditorListItem( + ArrayList _arr, EventEditorActivity activity) { list = _arr; this.activity = activity; } @@ -46,7 +45,9 @@ public void onBindViewHolder(ViewHolder _holder, int _position) { .getRoot() .setOnClickListener( (view) -> { - + activity.binding.blockList.setAdapter( + new BlockListAdapter(list.get(_position).getBlocks(), activity)); + activity.binding.blockList.setLayoutManager(new LinearLayoutManager(activity)); }); } diff --git a/app/src/main/java/com/dragon/ide/ui/view/BlockDefaultView.java b/app/src/main/java/com/dragon/ide/ui/view/BlockDefaultView.java index 1d5f6fe..4433463 100644 --- a/app/src/main/java/com/dragon/ide/ui/view/BlockDefaultView.java +++ b/app/src/main/java/com/dragon/ide/ui/view/BlockDefaultView.java @@ -16,6 +16,7 @@ public class BlockDefaultView extends LinearLayout { public String returns; + public Block block; public BlockDefaultView(Context context) { super(context); @@ -23,6 +24,8 @@ public BlockDefaultView(Context context) { } public void setBlock(Block block) { + this.block = block; + returns = block.getReturns(); if (!(block instanceof DoubleComplexBlock) && !(block instanceof ComplexBlock)) { @@ -41,29 +44,45 @@ public void setBlock(Block block) { if (block.getBlockContent().get(i) instanceof ComplexBlockContent) { if (block.getBlockContent().get(i) instanceof SourceContent) { LinearLayout ll_source = new LinearLayout(getContext()); - ll_source.setPadding(2, 2, 2, 2); - setBackgroundColor(Color.WHITE); + ll_source.setPadding(25, 0, 25, 0); + ll_source.setBackgroundColor(Color.WHITE); TextView tvTextContent = new TextView(getContext()); tvTextContent.setText(((SourceContent) block.getBlockContent().get(i)).getValue()); - ll_source.addView(tvTextContent, getChildCount()); - addView(ll_source, getChildCount()); - } + ll_source.addView(tvTextContent, getChildCount() - 1); + + LinearLayout.LayoutParams layoutParams = + new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); + int startMargin = 8; + int endMargin = 8; + layoutParams.setMarginStart(startMargin); + layoutParams.setMarginEnd(endMargin); + ll_source.setLayoutParams(layoutParams); - if (block.getBlockContent().get(i) instanceof SourceContent) { - LinearLayout ll_source = new LinearLayout(getContext()); - ll_source.setPadding(2, 2, 2, 2); - setBackgroundColor(Color.WHITE); - TextView tvTextContent = new TextView(getContext()); - tvTextContent.setText(((SourceContent) block.getBlockContent().get(i)).getValue()); - ll_source.addView(tvTextContent, getChildCount()); addView(ll_source, getChildCount()); } } else if (block.getBlockContent().get(i) instanceof BlockContent) { TextView tvTextContent = new TextView(getContext()); tvTextContent.setText(((BlockContent) block.getBlockContent().get(i)).getText()); - addView(tvTextContent, getChildCount()); + + int backgroundColor = Color.parseColor(block.getColor()); + int red = Color.red(backgroundColor); + int green = Color.green(backgroundColor); + int blue = Color.blue(backgroundColor); + + double luminance = (0.2126 * red + 0.7152 * green + 0.0722 * blue) / 255; + double contrastRatioThreshold = 1.4; + int textColor = (luminance > 0.5 * 255) ? Color.BLACK : Color.WHITE; + double contrastRatio = (luminance + 0.05) / (0.05 + 0.05); + + if (contrastRatio < contrastRatioThreshold) { + tvTextContent.setTextColor(textColor); + } + + addView(tvTextContent, getChildCount() - 1); } } + invalidate(); } public String getReturns() { @@ -72,4 +91,8 @@ public String getReturns() { } return ""; } + + public Block getBlock() { + return this.block; + } } diff --git a/app/src/main/res/layout/activity_event_editor.xml b/app/src/main/res/layout/activity_event_editor.xml index c774d93..94a82d9 100644 --- a/app/src/main/res/layout/activity_event_editor.xml +++ b/app/src/main/res/layout/activity_event_editor.xml @@ -74,9 +74,10 @@ android:paddingStart="8dp"> + android:layout_height="match_parent" + android:layout_width="match_parent" + android:orientation="vertical" + android:id="@+id/blockListEditorArea"> @@ -129,9 +130,9 @@ + android:id="@+id/blocksHolderList" />