Skip to content

Initial Idea

Iyxan23 edited this page Feb 20, 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 extends OpenBlocksModule {
    @Override
    public int getType() {
       /* This function is used to indicate what type of module is this?
        * OpenBlocks need atleast 1 module for each tipe 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: For parsing projects, this can be used to parse custom formats. This module must have a way to parse LOGIC, and LAYOUT
        * PROJECT_LAYOUT_VIEW: For rendering 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: For displaying, and editing the project's logic
        *
        * 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 readProject(String project_id) {
        // Here, we're going to read 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;
        
       // 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 that we doesn'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 {
        // uhhh, it's the same for import project
        throw new NotSupportedException("Importing project in IyxanProjectManager is not supported, please wait for future update");
    }
}

Clone this wiki locally