From bd143ec5ddeb1d4e7c44d3ae05cfe5ff5168ed14 Mon Sep 17 00:00:00 2001 From: DevKumar00001 Date: Fri, 29 Dec 2023 06:57:32 +0530 Subject: [PATCH] feat: Add tags in Blocks for filtering blocks * Built in block with developerOnly tag will never be shown to user * Developer can use any block everywhere if it is tagged with developer --- .../java/builtin/blocks/BuiltInBlocks.java | 8 +- .../java/com/dragon/ide/objects/Block.java | 9 +++ .../ui/activities/EventEditorActivity.java | 8 ++ .../BlocksHolderEventEditorListItem.java | 6 +- .../com/dragon/ide/utils/FilterBlocks.java | 75 +++++++++++++++++++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/dragon/ide/utils/FilterBlocks.java diff --git a/app/src/main/java/builtin/blocks/BuiltInBlocks.java b/app/src/main/java/builtin/blocks/BuiltInBlocks.java index fa6b38f..95e8ef5 100644 --- a/app/src/main/java/builtin/blocks/BuiltInBlocks.java +++ b/app/src/main/java/builtin/blocks/BuiltInBlocks.java @@ -22,7 +22,7 @@ public static ArrayList getBuiltInBlocksHolder() { holder1.setName("Operators"); holder1.setTags( new String[] { - Language.HTML, Language.CSS, Language.JavaScript, "developer", "developerOnly" + "developer", "developerOnly" }); ArrayList blockList = new ArrayList(); @@ -32,6 +32,7 @@ public static ArrayList getBuiltInBlocksHolder() { blockInHolder1.setBlockType(Block.BlockType.defaultBlock); blockInHolder1.setName("addSource"); blockInHolder1.setRawCode("DevKumar DragonIDE parameter DevKumar"); + blockInHolder1.setTags(new String[] {"developer", "developerOnly"}); ArrayList block1ContentList = new ArrayList(); @@ -51,6 +52,7 @@ public static ArrayList getBuiltInBlocksHolder() { blockInHolder2.setName("Test"); blockInHolder2.setEnableSideAttachableBlock(true); blockInHolder2.setRawCode("I am block code DevKumar DragonIDE parameter DevKumar "); + blockInHolder2.setTags(new String[] {"developer", "developerOnly"}); ArrayList block2ContentList = new ArrayList(); @@ -75,6 +77,7 @@ public static ArrayList getBuiltInBlocksHolder() { blockInHolder3StringBuilder.append(CodeReplacer.getReplacer("complexBlockContent")); blockInHolder3StringBuilder.append("\n}"); blockInHolder3.setRawCode(blockInHolder3StringBuilder.toString()); + blockInHolder3.setTags(new String[] {"developer", "developerOnly"}); ArrayList block3ContentList = new ArrayList(); @@ -93,6 +96,7 @@ public static ArrayList getBuiltInBlocksHolder() { blockInHolder4.setName("true"); blockInHolder4.setRawCode("true"); blockInHolder4.setReturns("boolean"); + blockInHolder4.setTags(new String[] {"developer", "developerOnly"}); ArrayList block4ContentList = new ArrayList(); @@ -108,6 +112,7 @@ public static ArrayList getBuiltInBlocksHolder() { blockInHolder5.setName("false"); blockInHolder5.setRawCode("false"); blockInHolder5.setReturns("boolean"); + blockInHolder5.setTags(new String[] {"developer", "developerOnly"}); ArrayList block5ContentList = new ArrayList(); @@ -123,6 +128,7 @@ public static ArrayList getBuiltInBlocksHolder() { blockInHolder6.setName("Test"); blockInHolder6.setEnableSideAttachableBlock(true); blockInHolder6.setRawCode("I am block code DevKumar DragonIDE parameter DevKumar "); + blockInHolder6.setTags(new String[] {"developer", "developerOnly"}); ArrayList block6ContentList = new ArrayList(); diff --git a/app/src/main/java/com/dragon/ide/objects/Block.java b/app/src/main/java/com/dragon/ide/objects/Block.java index 0d55981..cd31bcd 100644 --- a/app/src/main/java/com/dragon/ide/objects/Block.java +++ b/app/src/main/java/com/dragon/ide/objects/Block.java @@ -16,6 +16,7 @@ public class Block implements Serializable, Cloneable { private String returns; private boolean enableSideAttachableBlock; private ArrayList sideAttachableBlock; + private String[] tags; public String getColor() { if (this.color != null) { @@ -188,4 +189,12 @@ public Block clone() throws CloneNotSupportedException { mBlock.setSideAttachableBlock(mSideAttachableBlock); return mBlock; } + + public String[] getTags() { + return this.tags; + } + + public void setTags(String[] tags) { + this.tags = tags; + } } 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 aa72b91..f28489f 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 @@ -885,4 +885,12 @@ public void updateBlocks(ViewGroup view) { event.setBlocks(BlocksHandler.loadBlocksIntoObject(view)); } } + + public String getLanguage() { + return this.language; + } + + public void setLanguage(String language) { + this.language = language; + } } 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 ba6b0a5..7f799d5 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 @@ -8,6 +8,7 @@ import com.dragon.ide.databinding.LayoutEventBlocksHolderListItemBinding; import com.dragon.ide.objects.BlocksHolder; import com.dragon.ide.ui.activities.EventEditorActivity; +import com.dragon.ide.utils.FilterBlocks; import java.util.ArrayList; public class BlocksHolderEventEditorListItem @@ -46,7 +47,10 @@ public void onBindViewHolder(ViewHolder _holder, int _position) { .setOnClickListener( (view) -> { activity.binding.blockList.setAdapter( - new BlockListAdapter(list.get(_position).getBlocks(), activity)); + new BlockListAdapter( + FilterBlocks.filterBlocksWithTag( + list.get(_position).getBlocks(), activity.getLanguage()), + activity)); activity.binding.blockList.setLayoutManager(new LinearLayoutManager(activity)); }); } diff --git a/app/src/main/java/com/dragon/ide/utils/FilterBlocks.java b/app/src/main/java/com/dragon/ide/utils/FilterBlocks.java new file mode 100644 index 0000000..c31d06a --- /dev/null +++ b/app/src/main/java/com/dragon/ide/utils/FilterBlocks.java @@ -0,0 +1,75 @@ +package com.dragon.ide.utils; + +import com.dragon.ide.BuildConfig; +import com.dragon.ide.objects.Block; +import editor.tsd.tools.Language; +import java.util.ArrayList; + +public class FilterBlocks { + public static ArrayList filterBlocksWithTag(ArrayList blocks, String language) { + ArrayList filteredBlocks = new ArrayList(); + for (int i = 0; i < blocks.size(); ++i) { + boolean addHolder = false; + boolean forHtml = false; + boolean forCss = false; + boolean forJavaScript = false; + boolean forDeveloper = false; + boolean forDeveloperOnly = false; + + for (int blockTagIndex = 0; blockTagIndex < blocks.get(i).getTags().length; ++blockTagIndex) { + if (blocks.get(i).getTags()[blockTagIndex].equals("developerOnly")) { + forDeveloperOnly = true; + } + if (blocks.get(i).getTags()[blockTagIndex].equals("developer")) { + forDeveloper = true; + } + if (blocks.get(i).getTags()[blockTagIndex].equals(Language.HTML)) { + forHtml = true; + } + if (blocks.get(i).getTags()[blockTagIndex].equals(Language.CSS)) { + forCss = true; + } + if (blocks.get(i).getTags()[blockTagIndex].equals(Language.JavaScript)) { + forJavaScript = true; + } + } + + if (forHtml) { + if (language.equals(Language.HTML)) { + addHolder = true; + } + } + + if (forCss) { + if (language.equals(Language.CSS)) { + addHolder = true; + } + } + + if (forJavaScript) { + if (language.equals(Language.JavaScript)) { + addHolder = true; + } + } + + if (forDeveloper) { + if (BuildConfig.enableDeveloperBlocks) { + addHolder = true; + } + } + + if (forDeveloperOnly) { + if (BuildConfig.enableDeveloperBlocks) { + addHolder = true; + } else { + addHolder = false; + } + } + + if (addHolder) { + filteredBlocks.add(blocks.get(i)); + } + } + return filteredBlocks; + } +}