Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback for image upload #56

Open
cngarkar opened this issue Dec 17, 2018 · 6 comments
Open

Callback for image upload #56

cngarkar opened this issue Dec 17, 2018 · 6 comments

Comments

@cngarkar
Copy link

I like this rte. One thing that is stopping me from fully trying this out is the image upload functionality. Such that, if I upload an image to the server I get an image url(server location), then I want to insert the image using this server image url. Do you have any way to achieve this functionality? Currently there doesn't seem to be any callback/method to achieve this.

@chinalwb
Copy link
Owner

I will check and back to you.

Thanks.

@chinalwb
Copy link
Owner

I added a new interface:

package com.chinalwb.are.strategies;

import android.net.Uri;

import com.chinalwb.are.styles.toolitems.styles.ARE_Style_Image;

public interface ImageStrategy {

    /**
     * Upload the video to server and return the url of the video at server.
     * After that done, you need to call
     * {@link ARE_Style_Image#insertImage(Object, com.chinalwb.are.spans.AreImageSpan.ImageType)}
     * to insert the url on server to ARE
     *
     * @param uri
     * @param areStyleImage used to insert the url on server to ARE
     */
    void uploadAndInsertImage(Uri uri, ARE_Style_Image areStyleImage);
}

And this is a demo implementation:

package com.chinalwb.are.demo.helpers;

import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;

import com.chinalwb.are.spans.AreImageSpan;
import com.chinalwb.are.strategies.ImageStrategy;
import com.chinalwb.are.styles.toolitems.styles.ARE_Style_Image;

import java.lang.ref.WeakReference;

import static android.os.AsyncTask.THREAD_POOL_EXECUTOR;

public class DemoImageStrategy implements ImageStrategy {
    @Override
    public void uploadAndInsertImage(Uri uri, ARE_Style_Image areStyleImage) {
        new UploadImageTask(areStyleImage).executeOnExecutor(THREAD_POOL_EXECUTOR, uri);
    }

    private static class UploadImageTask extends AsyncTask<Uri, Integer, String> {

        WeakReference<ARE_Style_Image> areStyleImage;
        private ProgressDialog dialog;
        UploadImageTask(ARE_Style_Image styleImage) {
            this.areStyleImage = new WeakReference<>(styleImage);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (dialog == null) {
                dialog = ProgressDialog.show(
                        areStyleImage.get().getEditText().getContext(),
                        "",
                        "Uploading image. Please wait...",
                        true);
            } else {
                dialog.show();
            }
        }

        @Override
        protected String doInBackground(Uri... uris) {
            if (uris != null && uris.length > 0) {
                try {
                    // do upload here ~
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // Returns the image url on server here
                return "https://avatars0.githubusercontent.com/u/1758864?s=460&v=4";
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (dialog != null) {
                dialog.dismiss();
            }
            if (areStyleImage.get() != null) {
                areStyleImage.get().insertImage(s, AreImageSpan.ImageType.URL);
            }
        }
    }
}

And set it to are:

com.chinalwb.are.AREditText#setImageStrategy

Sample usage here:

com.chinalwb.are.demo.ARE_DefaultToolbarActivity

And here is demo a video:
imageupload

Hope this helps you.

Currently it is in repository master. I'll publish 0.1.5 to enable this feature.

Thanks.

@cngarkar
Copy link
Author

Thanks for doing it. I'll check it out once you publish it. And will post an update here.

@chinalwb
Copy link
Owner

I did publish (oh I thought I had posted this comment but not...).

Try this:

    implementation 'com.github.bumptech.glide:glide:4.3.1'
    implementation 'com.github.chinalwb:are:0.1.5'

@cngarkar
Copy link
Author

cngarkar commented Dec 18, 2018

Are you sure you published changes with DemoImageStrategy?
I'm seeing cannot resolve symbol 'DemoImageStrategy' error with 0.1.5 release.
Also why the name is DemoImageStrategy? Can't it resemble to what its actually doing?

Also how do I use uploadAndInsertImage() in my activity?

@chinalwb
Copy link
Owner

DemoImageStrategy is not published within the library, it is in Github.

Checkout the latest code and run the demo. Thanks.

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

No branches or pull requests

2 participants