Skip to content

Initial Idea

Iyxan23 edited this page Feb 23, 2021 · 9 revisions

So this library is basically a standard to communicate between an openblocks module and the openblocks app itself.

Example Code:

public class IyxanProjectManager implements OpenBlocksModule.ProjectManager {
    @Override
    public int getType() {
       /* This function is used to indicate what type of module is this?
        * OpenBlocks need at least 1 module for each type for it to work
        *
        * Initial ideas for types would be: 
        * PROJECT_MANAGER: For exporting, saving, restoring, opening projects. Basically on how the projects are stored.
        * PROJECT_PARSER: To parse project files, this can be used to parse custom formats. This module must have a way to parse LOGIC, and LAYOUT
        * PROJECT_LAYOUT_VIEW: To render the project's Layout, you can get the layout from using the LAYOUT_PARSER module (we use sketchware-blocks-view for this)
        * PROJECT_LOGIC_VIEW: To display and edit the project's logic
        * PROJECT_COMPILER: To compile the LAYOUT and LOGIC into a ready-to-install APK file
        *
        * That's currently it, This might expand in the future.
        * 
        * I have an idea where the layout is also a module :flushed: 
        */
       return OpenBlocksModule.Type.PROJECT_MANAGER;
    }
    
    @Override
    public void saveProject(OpenBlocksProject project) {
        // OpenBlocksProject contains a list of files that is generated by PROJECT_PARSER, that will need to be saved / managed by the PROJECT_MANAGER
        // Oh yeah, it also has an ID, which is determined by the PROJECT_PARSER

        // In this example project manager, we're just going to save the stuff to the /.openblocks/projects/{ID}/ directory
        String external_dir = Environment.getExternalStorageDirectory();
        String project_dir = external_dir + "/.openblocks/projects/" + project.getID() + "/";

        for (OpenBlocksFile data: project.files) {  // project.files is an Array List of OpenBlocksFile
            Util.writeFile(project_dir data.getName(), data.getData());
        }
        // This is a very simple example, You can implement stuff like, AES encryption, or maybe a compression, or maybe upload it to cloud, idk, just go crazy with this
    }
    
    @Override
    public OpenBlocksProject getProject(String project_id) {
        // Here, we're going to get the project
        OpenBlocksProject output = new OpenBlocksProject(); // Initialize an empty project
        ArrayList<OpenBlocksFile> files = new ArrayList<>(); // Initialize an empty files

        // Because we write our files on /.openblocks/projects/{ID}/ directory, we're gonna read from that too.
        String external_dir = Environment.getExternalStorageDirectory();
        String project_dir = external_dir + "/.openblocks/projects/" + project_id + "/";

        for (File file: new File().listDir()) {
            files.add(new OpenBlocksFile(Util.readFile(file), file.getName())); // Note: Imaginary function Util.readFile returns byte[]
        }
        
        // Finally, save it to the output
        output.files = files;

        // Don't forgot to set the id!
        output.ID = project_id;
        
        // Aand return it
        return output;
    }

    @Override
    public void exportProject(OpenBlocksProject project) throws NotSupportedException {
        // uhhh, let's pretend we don't have plan on making export project, so, well, let's just throw a NotSupportedException, indicating that we don't support exporting projects
        throw new NotSupportedException("Exporting project in IyxanProjectManager is not supported, please wait for future update");
    }

    @Override
    public OpenBlocksProject importProject() throws NotSupportedException {
        // uhh, it's the same for import project
        throw new NotSupportedException("Importing project in IyxanProjectManager is not supported, please wait for future update");
    }
}

So, If you want to deploy this code, you can do this:

  • Compile it as an APK
  • Rename the APK extension to be a JAR file
  • Create a file named openblocks-module-manifest.json, edit and put this code:
    {"name": "IyxanProjectManager", "description": "Iyxan's personal project manager", "classpath": "com.iyxan23.project.manager.IyxanProjectManager", "version": 1, "lib_version": 1, "type": "PROJECT_MANAGER"}
  • Zip all of those files (JAR, and the openblocks-module-manifest.json) into 1 zip file
  • Then load it on the OpenBlocks App

More information: https://stackoverflow.com/a/25748704/9613353, https://stackoverflow.com/a/6860579/9613353

Clone this wiki locally