0

I use an Indeterminate progress dialog when I publish, but I want to replace it with a progress bar, with upload percent, like what Facebook is doing.

I used android-simple-facebook library but I can use native Facebook SDK too, if it will do what I want.

This is my code:

    public void publish(final Photo photo, OnPublishListener onPublishListener) {
    this.onPublisherListener = onPublishListener;
    isPublish = true;
    if (!simpleFacebook.isLogin())
        simpleFacebook.login(this);
    else {
        if (isPermissionAllowed("publish_actions")) {
            isPublish = false;
            if (photo != null)
                simpleFacebook.publish(photo, false, onPublishListener);
        } else {
            requestUserPhotosPermissionAndPublish(photo, onPublishListener);
        }
    }
}

and my OnPublishListener

new OnPublishListener() {
    @Override
    public void onComplete(String response) {
           super.onComplete(response);
                }

    @Override
    public void onException(Throwable throwable) {
    super.onException(throwable);

    }

    @Override
    public void onFail(String reason) {
    super.onFail(reason);

    }
  }

Also there was a solution here facebook upload progress, but it is not available on the current Facebook SDK version.

Community
  • 1
  • 1
Ma7moud El-Naggar
  • 548
  • 1
  • 6
  • 18

1 Answers1

1

Well you are not using OnPublishListener object to effectively publish progress. You can simply refactor your code in this way: Create an interface:

 IPublishProgress {
     void publishProgress(int status);
 }

Then refactor publish in this way:

public void publish(final Photo photo, IPublishProgress iPublishProgress) {
      iPublishProgress.publishProgress(1);
      //some stuff
      if (!simpleFacebook.isLogin())
           simpleFacebook.login(this);
      else {
           if (isPermissionAllowed("publish_actions")) {
                isPublish = false;
                iPublishProgress.publishProgress(2);
                if (photo != null)
                     simpleFacebook.publish(photo, false, onPublishListener);
                     iPublishProgress.publishProgress(3);
           } 
      }
 }

Finally when you call publish method, you only need to implement publishProgress(int status) in the correct way:

publish(photo, new IPublishProgress() {
            @Override
            public void publishMessage(final int status) {
                switch (status) {
                    case 1:
                       //do some stuff;
                       break;
                    case 2:
                       //do some stuff;
                       break;
                    case 3:
                       //do some stuff;
                       break;
                    //otherwiseyou can just do progressBar.setProgressBar(status);
                 }
             });

I hope to help you.

Lorenzo Camaione
  • 505
  • 3
  • 15