-1
\$\begingroup\$

I have created a method that is responsible for the creation and the saving of memes. This is a huge mistake because now I need to use 1 of those functionalities (creating) for a different purpose. I tried to divide them into different methods but the code is tightly chained together. For example, when I removed all the code responsible for creating the meme, I noticed that my mutableBitmap object is also used in the block of code responsible for saving the meme.

This is the method I want to divide with detailed comments

public void createBitmapAndSave(ImageView img) {

        //Gets the image from ImageView and turns its into a mutable bitmap
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
        Bitmap bitmap = bitmapDrawable.getBitmap();
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        //Gets the texts from the textviews to draw on the bitmap
        String topText = topTextView.getText().toString();
        String bottomText = bottomTextView.getText().toString();

        //Text becomes all caps
        topText = topText.toUpperCase();
        bottomText = bottomText.toUpperCase();

        //Canvas created and takes mutableBitmap as its parameter
        Canvas canvas = new Canvas(mutableBitmap);

        //Creating 4 TextPaint objects for the outlined Impact meme custom font
        TextPaint topFillPaint = new TextPaint();
        TextPaint bottomFillPaint = new TextPaint();

        TextPaint topStrokePaint = new TextPaint();
        TextPaint bottomStrokePaint = new TextPaint();

        //Typeface for the impact font
        Typeface typeface = getResources().getFont(R.font.impact);

        //Setting the attributes of the custom font
        topFillPaint.setColor(Color.WHITE);
        topFillPaint.setTextSize(topTextView.getTextSize());
        topFillPaint.setTypeface(typeface);

        topStrokePaint.setStyle(Paint.Style.STROKE);
        topStrokePaint.setStrokeWidth(8);
        topStrokePaint.setColor(Color.BLACK);
        topStrokePaint.setTextSize(topTextView.getTextSize());
        topStrokePaint.setTypeface(typeface);

        bottomFillPaint.setColor(Color.WHITE);
        bottomFillPaint.setTextSize(bottomTextView.getTextSize());
        bottomFillPaint.setTypeface(typeface);

        bottomStrokePaint.setStyle(Paint.Style.STROKE);
        bottomStrokePaint.setStrokeWidth(8);
        bottomStrokePaint.setColor(Color.BLACK);
        bottomStrokePaint.setTextSize(bottomTextView.getTextSize());
        bottomStrokePaint.setTypeface(typeface);

        //Using StaticLayout because the text could potentially be multiline.  Made 4 StaticLayout objects for the custom font
        StaticLayout topFillLayout = new StaticLayout(topText, topFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);
        StaticLayout topStrokeLayout = new StaticLayout(topText, topStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);

        StaticLayout bottomFillLayout = new StaticLayout(bottomText, bottomFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);
        StaticLayout bottomStrokeLayout = new StaticLayout(bottomText, bottomStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);

        //Drawing the text on the canvas using the StaticLayout.draw() method
        topFillLayout.draw(canvas);

        topStrokeLayout.draw(canvas);

        canvas.translate(0, canvas.getHeight() - 210);
        bottomFillLayout.draw(canvas);

        bottomStrokeLayout.draw(canvas);

        //Used to add an integer after the timestamp to ensure uniqueness
        counter++;

        //The remaining block of code deals with saving the meme to the device
        File file;
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
        file = new File(path + "/SimpliMeme/" + timeStamp + "-" + counter + ".jpg");
        file.getParentFile().mkdir();

        try {
            OutputStream stream = new FileOutputStream(file);
            mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
    }

Now I want to divide this code into these 2 methods:

public void createMeme(){returns the mutable bitmap with the text drawn}

public void saveMeme(){}
\$\endgroup\$

closed as off-topic by Toby Speight, Ludisposed, yuri, IEatBagels, Gerrit0 Mar 14 at 1:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Toby Speight, Ludisposed, yuri, IEatBagels, Gerrit0
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ It appears that you haven't yet finished the code to your own satisfaction. Please complete the work that you outlined, and then edit to put the finished code in your review request. \$\endgroup\$ – Toby Speight Mar 13 at 11:44
1
\$\begingroup\$

Instead of createMeme() being a void function, you could have it return the created meme, mutableBitmap.
Then, set up your saveMeme function to accept a mutableBitmap as a parameter.

That way, when you want to create AND save, as you originally intended when you created the large function, you might call saveMeme like this: saveMeme(createMeme())

\$\endgroup\$
  • \$\begingroup\$ And is saveMeme(createMeme()) what I call in the onReceive() of my LocalBroadcastManager? \$\endgroup\$ – Andros Adrianopolos Mar 14 at 0:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.