FileUploadingRenderer.java 1.74 KB
Newer Older
Yusuke Iwaki's avatar
Yusuke Iwaki committed
1 2 3 4 5
package chat.rocket.android.renderer;

import android.content.Context;
import android.widget.ProgressBar;
import android.widget.TextView;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
6

Tiago Cunha's avatar
Tiago Cunha committed
7
import chat.rocket.persistence.realm.models.internal.FileUploading;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/**
 * rendering FileUploading status.
 */
public class FileUploadingRenderer extends AbstractRenderer<FileUploading> {
  public FileUploadingRenderer(Context context, FileUploading object) {
    super(context, object);
  }

  public FileUploadingRenderer progressInto(ProgressBar progressBar) {
    if (!shouldHandle(progressBar)) {
      return this;
    }

    if (object.getFilesize() >= Integer.MAX_VALUE) {
      int max = 1000;
      int progress = (int) (max * object.getUploadedSize() / object.getFilesize());
      progressBar.setProgress(progress);
      progressBar.setMax(max);
    } else {
      progressBar.setProgress((int) object.getUploadedSize());
      progressBar.setMax((int) object.getFilesize());
    }

    return this;
  }

  public FileUploadingRenderer progressTextInto(TextView uploadedSizeText, TextView totalSizeText) {
    if (!shouldHandle(uploadedSizeText) || !shouldHandle(totalSizeText)) {
      return this;
    }

    long uploaded = object.getUploadedSize();
    long total = object.getFilesize();

    if (total < 50 * 1024) { //<50KB
      uploadedSizeText.setText(String.format("%,d", uploaded));
      totalSizeText.setText(String.format("%,d", total));
    } else if (total < 8 * 1048576) { //<8MB
Yusuke Iwaki's avatar
Yusuke Iwaki committed
47 48
      uploadedSizeText.setText(String.format("%,d", uploaded / 1024));
      totalSizeText.setText(String.format("%,d KB", total / 1024));
Yusuke Iwaki's avatar
Yusuke Iwaki committed
49
    } else {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
50 51
      uploadedSizeText.setText(String.format("%,d", uploaded / 1048576));
      totalSizeText.setText(String.format("%,d MB", total / 1048576));
Yusuke Iwaki's avatar
Yusuke Iwaki committed
52 53 54 55 56
    }

    return this;
  }
}