Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Number Block Disappears When Placed In Workspace #733

Open
TheBrokenRail opened this issue Jul 21, 2018 · 3 comments
Open

Number Block Disappears When Placed In Workspace #733

TheBrokenRail opened this issue Jul 21, 2018 · 3 comments

Comments

@TheBrokenRail
Copy link
Contributor

TheBrokenRail commented Jul 21, 2018

When dragging out the number block, it disappears after you place in the work space and reappears after the file has been saved and reloaded.

untitled

@TheBrokenRail TheBrokenRail changed the title Number Block Disappears When Placed In Work space Number Block Disappears When Placed In Workspace Jul 21, 2018
@TheBrokenRail
Copy link
Contributor Author

Activity Code:

package com.thebrokenrail.letscode.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.google.blockly.android.BlocklyActivityHelper;
import com.google.blockly.android.codegen.CodeGenerationRequest;
import com.google.blockly.android.codegen.LanguageDefinition;
import com.google.blockly.android.control.BlocklyController;
import com.google.blockly.model.BlocklySerializerException;
import com.google.blockly.model.DefaultBlocks;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;
import static com.thebrokenrail.letscode.app.ProgramsAdapter.EXTRA_MESSAGE;

public class BlocklyEdit extends AppCompatActivity {
    private static final List<String> BLOCK_DEFINITIONS = DefaultBlocks.getAllBlockDefinitions();
    private static final List<String> JAVASCRIPT_GENERATORS = Arrays.asList(
            // Custom block generators go here. Default blocks are already included.
    );

    @NonNull
    protected List<String> getBlockDefinitionsJsonPaths() {
        return BLOCK_DEFINITIONS;
    }

    @NonNull
    protected String getToolboxContentsXmlPath() {
        // Replace with a toolbox that includes application specific blocks.
        return DefaultBlocks.TOOLBOX_PATH;
    }

    @NonNull
    protected List<String> getGeneratorsJsPaths() {
        return JAVASCRIPT_GENERATORS;
    }

    @NonNull
    protected CodeGenerationRequest.CodeGeneratorCallback getCodeGenerationCallback() {
        // Uses the same callback for every generation call.
        return new CodeGenerationRequest.CodeGeneratorCallback() {
            @Override
            public void onFinishCodeGeneration(String generatedCode) {
                Log.d("Blockly", generatedCode);
                Intent intent = new Intent(getApplicationContext(), RunJavascript.class);
                intent.putExtra(EXTRA_MESSAGE, generatedCode);
                startActivity(intent);
            }
        };
    }

    String fileName;
    protected BlocklyActivityHelper mBlocklyActivityHelper;

    public final BlocklyController getController() {
        return mBlocklyActivityHelper.getController();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getController().onSaveSnapshot(outState);
    }

    @Override
    public void onBackPressed() {
        // Try to close any open drawer / toolbox before backing out of the Activity.
        if (!mBlocklyActivityHelper.onBackToCloseFlyouts()) {
            try {
                mBlocklyActivityHelper.saveWorkspaceToAppDir(fileName);
            } catch (FileNotFoundException | BlocklySerializerException e) {
                e.printStackTrace();
            }
            super.onBackPressed();
        }
    }

    protected BlocklyActivityHelper onCreateActivityHelper() {
        return new BlocklyActivityHelper(this, this.getSupportFragmentManager());
    }

    @Override
    protected void onStart() {
        super.onStart();
        mBlocklyActivityHelper.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mBlocklyActivityHelper.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mBlocklyActivityHelper.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mBlocklyActivityHelper.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mBlocklyActivityHelper.onRestart();
    }

    protected void onLoadInitialWorkspace() {
        onInitWorkspace();
        getController().closeFlyouts();
    }

    protected void onInitWorkspace() {
        mBlocklyActivityHelper.loadWorkspaceFromAppDirSafely(fileName);
    }

    @NonNull
    protected LanguageDefinition getBlockGeneratorLanguage() {
        return DefaultBlocks.LANGUAGE_DEFINITION;
    }

    protected void configureBlockExtensions() {
        mBlocklyActivityHelper.configureExtensions();
    }

    protected void configureMutators() {
        mBlocklyActivityHelper.configureMutators();
    }

    protected void configureCategoryFactories() {
        mBlocklyActivityHelper.configureCategoryFactories();
    }

    protected void onRunCode() {
        mBlocklyActivityHelper.requestCodeGeneration(getBlockGeneratorLanguage(), getBlockDefinitionsJsonPaths(),
                getGeneratorsJsPaths(), getCodeGenerationCallback());
    }

    protected void reloadToolbox() {
        mBlocklyActivityHelper.reloadToolbox(getToolboxContentsXmlPath());
    }

    protected void resetBlockFactory() {
        mBlocklyActivityHelper.resetBlockFactory(getBlockDefinitionsJsonPaths());

        configureBlockExtensions();
        configureMutators();
        configureCategoryFactories();
    }

    protected View onCreateContentView() {
        return getLayoutInflater().inflate(R.layout.blockly_unified_workspace, null);
    }

    protected void onCreateActivityRootView() {
        View content = onCreateContentView();
        if (content != null) {
            FrameLayout contentContainer = findViewById(R.id.content_container);
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            if (content.getParent() != contentContainer) {
                contentContainer.addView(content, lp);
            } else {
                content.setLayoutParams(lp);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_blockly);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Intent intent = getIntent();
        fileName = intent.getStringExtra(EXTRA_MESSAGE);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        onCreateActivityRootView();
        mBlocklyActivityHelper = onCreateActivityHelper();
        if (mBlocklyActivityHelper == null) {
            throw new IllegalStateException(
                    "BlocklyActivityHelper is null. " + "onCreateActivityHelper must return a instance.");
        }
        resetBlockFactory(); // Initial load of block definitions, extensions, and mutators.
        configureCategoryFactories(); // After BlockFactory; before Toolbox
        reloadToolbox();
        boolean loadedPriorInstance = getController().onRestoreSnapshot(savedInstanceState);
        if (!loadedPriorInstance) {
            onLoadInitialWorkspace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_edit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_save_program: {
                try {
                    mBlocklyActivityHelper.saveWorkspaceToAppDir(fileName);
                } catch (FileNotFoundException | BlocklySerializerException e) {
                    e.printStackTrace();
                }
                return true;
            }
            case R.id.action_run_program: {
                try {
                    mBlocklyActivityHelper.saveWorkspaceToAppDir(fileName);
                } catch (FileNotFoundException | BlocklySerializerException e) {
                    e.printStackTrace();
                }
                onRunCode();
                return true;
            }
            case android.R.id.home: {
                try {
                    mBlocklyActivityHelper.saveWorkspaceToAppDir(fileName);
                } catch (FileNotFoundException | BlocklySerializerException e) {
                    e.printStackTrace();
                }
                return super.onOptionsItemSelected(item);
            }
            default: {
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);
            }
        }
    }
}

@TheBrokenRail
Copy link
Contributor Author

Strangely this only happens with the number block and not in the demo.

@loyilin
Copy link

loyilin commented Apr 30, 2021

@TheBrokenRail Me, too. Have you solved it

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants