Skip to content

Commit

Permalink
feat: Add tags in Blocks for filtering blocks
Browse files Browse the repository at this point in the history
* Built in block with developerOnly tag will never be shown to user
* Developer can use any block everywhere if it is tagged with developer
  • Loading branch information
SyntaxGalaxy committed Dec 29, 2023
1 parent 1dbc7d9 commit bd143ec
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/src/main/java/builtin/blocks/BuiltInBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
holder1.setName("Operators");
holder1.setTags(
new String[] {
Language.HTML, Language.CSS, Language.JavaScript, "developer", "developerOnly"
"developer", "developerOnly"
});

ArrayList<Block> blockList = new ArrayList<Block>();
Expand All @@ -32,6 +32,7 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
blockInHolder1.setBlockType(Block.BlockType.defaultBlock);
blockInHolder1.setName("addSource");
blockInHolder1.setRawCode("DevKumar DragonIDE parameter DevKumar");
blockInHolder1.setTags(new String[] {"developer", "developerOnly"});

ArrayList<BlockContent> block1ContentList = new ArrayList<BlockContent>();

Expand All @@ -51,6 +52,7 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
blockInHolder2.setName("Test");
blockInHolder2.setEnableSideAttachableBlock(true);
blockInHolder2.setRawCode("I am block code DevKumar DragonIDE parameter DevKumar ");
blockInHolder2.setTags(new String[] {"developer", "developerOnly"});

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

Expand All @@ -75,6 +77,7 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
blockInHolder3StringBuilder.append(CodeReplacer.getReplacer("complexBlockContent"));
blockInHolder3StringBuilder.append("\n}");
blockInHolder3.setRawCode(blockInHolder3StringBuilder.toString());
blockInHolder3.setTags(new String[] {"developer", "developerOnly"});

ArrayList<BlockContent> block3ContentList = new ArrayList<BlockContent>();

Expand All @@ -93,6 +96,7 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
blockInHolder4.setName("true");
blockInHolder4.setRawCode("true");
blockInHolder4.setReturns("boolean");
blockInHolder4.setTags(new String[] {"developer", "developerOnly"});

ArrayList<BlockContent> block4ContentList = new ArrayList<BlockContent>();

Expand All @@ -108,6 +112,7 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
blockInHolder5.setName("false");
blockInHolder5.setRawCode("false");
blockInHolder5.setReturns("boolean");
blockInHolder5.setTags(new String[] {"developer", "developerOnly"});

ArrayList<BlockContent> block5ContentList = new ArrayList<BlockContent>();

Expand All @@ -123,6 +128,7 @@ public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
blockInHolder6.setName("Test");
blockInHolder6.setEnableSideAttachableBlock(true);
blockInHolder6.setRawCode("I am block code DevKumar DragonIDE parameter DevKumar ");
blockInHolder6.setTags(new String[] {"developer", "developerOnly"});

ArrayList<BlockContent> block6ContentList = new ArrayList<BlockContent>();

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/dragon/ide/objects/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Block implements Serializable, Cloneable {
private String returns;
private boolean enableSideAttachableBlock;
private ArrayList<Block> sideAttachableBlock;
private String[] tags;

public String getColor() {
if (this.color != null) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
});
}
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/com/dragon/ide/utils/FilterBlocks.java
Original file line number Diff line number Diff line change
@@ -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<Block> filterBlocksWithTag(ArrayList<Block> blocks, String language) {
ArrayList<Block> filteredBlocks = new ArrayList<Block>();
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;
}
}

0 comments on commit bd143ec

Please sign in to comment.