Skip to content

Commit

Permalink
feat: copy image charts android (#109)
Browse files Browse the repository at this point in the history
* temp

* fix: totals view initialization

* fix: missing file

Co-authored-by: Vittorio Cellucci <vel@qlik.com>
  • Loading branch information
vcellu and vcellu committed Nov 2, 2022
1 parent 1a8d94a commit af81780
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 10 deletions.
Expand Up @@ -58,7 +58,7 @@ public class CellView extends LinearLayout implements SelectionsObserver {
}
return true;
};
String copyString = tableView.getTranslation("menu", "copy");
String copyString = tableView.getTranslation("menu", content.getCopyMenuString());
View.OnCreateContextMenuListener onCreateContextMenuListener = (contextMenu, view, contextMenuInfo) -> contextMenu.add(0, 0, 0, copyString).setOnMenuItemClickListener(handleMenuItemClick);
View contentView = (View) content;
contentView.setOnCreateContextMenuListener(onCreateContextMenuListener);
Expand All @@ -81,10 +81,9 @@ public void handleDragBoxDrag(Rect dragBoxBounds, int columnId) {
}

private void copyCell(Context context){
DataCell cell = content.getCell();
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(cell.qText, cell.qText);
clipboard.setPrimaryClip(clip);
if(content != null) {
content.copyToClipBoard();
}
}

public void setData(DataCell cell) {
Expand Down
Expand Up @@ -4,6 +4,8 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
Expand Down Expand Up @@ -184,4 +186,17 @@ public DataCell getCell() {
public boolean isSelected() {
return selected;
}

public void copyToClipBoard() {
ImageShare imageShare = new ImageShare();
BitmapDrawable drawable = (BitmapDrawable) this.getDrawable();
if(drawable != null) {
Bitmap bitmap = drawable.getBitmap();
imageShare.share(bitmap, getContext());
}
}

public String getCopyMenuString() {
return "share";
}
}
@@ -1,6 +1,8 @@
package com.qliktrialreactnativestraighttable;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
Expand Down Expand Up @@ -178,4 +180,14 @@ public int getMeasuredLineCount() {
public int measureLines(DataColumn dataColumn) {
return textWrapper.getMeasureLinedCount(dataColumn);
}

public void copyToClipBoard() {
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(cell.qText, cell.qText);
clipboard.setPrimaryClip(clip);
}

public String getCopyMenuString() {
return "copy";
}
}
@@ -1,7 +1,6 @@
package com.qliktrialreactnativestraighttable;

import android.view.GestureDetector;
import android.view.MotionEvent;

public interface Content {
void updateBackgroundColor(boolean shouldAnimate);
Expand All @@ -11,4 +10,6 @@ public interface Content {
boolean isSelected();
void setCell(DataCell cell);
DataCell getCell();
String getCopyMenuString();
void copyToClipBoard();
}
Expand Up @@ -160,8 +160,9 @@ private void buildTotals(Context context) {
totalsView.post(() -> {
int headerHeight = headerView.getMeasuredHeight();
int y = topPosition ? headerHeight : tableView.getMeasuredHeight() - TableTheme.rowHeightFactor * 2;

totalsView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableTheme.rowHeightFactor));
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, TableTheme.rowHeightFactor);
params.topMargin = tableView.headerHeight;
totalsView.setLayoutParams(params);
totalsView.setOrientation(LinearLayout.HORIZONTAL);
totalsView.setElevation((int) PixelUtils.dpToPx(4));
totalsView.setZ((int) PixelUtils.dpToPx(headerZ));
Expand Down
@@ -0,0 +1,31 @@
package com.qliktrialreactnativestraighttable;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.ByteArrayOutputStream;

public class ImageShare {

public void share(Bitmap bitmap, Context context) {
Uri uri = getImageUri(bitmap, context);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
context.startActivity(Intent.createChooser(intent, null));
} catch (Exception ex) {
ex.printStackTrace();
}
}

private Uri getImageUri(Bitmap bitmap, Context context) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, null, null);
return Uri.parse(path);
}
}
Expand Up @@ -23,7 +23,8 @@ public MiniChartRenderer(qMiniChart chartData, Representation representation ) {
this.representation = representation;
miniChartData = chartData;
miniChartInfo = representation.miniChart;
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
}

public void updateData(qMiniChart chartData, Representation representation) {
Expand Down
@@ -1,13 +1,21 @@
package com.qliktrialreactnativestraighttable;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.GestureDetector;
import android.view.View;

import java.io.ByteArrayOutputStream;

public class MiniChartView extends View implements Content {
Rect bounds = new Rect();
Paint paint = new Paint();
Expand Down Expand Up @@ -73,4 +81,28 @@ public void setCell(DataCell cell) {
public DataCell getCell() {
return dataCell;
}

public void copyToClipBoard() {
if(miniChartRenderer != null) {
Bitmap bitmap = renderToBitmap();
ImageShare imageShare = new ImageShare();
imageShare.share(bitmap, getContext());
}
}

private Bitmap renderToBitmap() {
int height = (int)PixelUtils.dpToPx(getHeight());
int width = (int)PixelUtils.dpToPx(getWidth());
Rect newBounds = new Rect(0, 0, width, height);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
miniChartRenderer.resetScales(newBounds);
miniChartRenderer.render(canvas);
return bitmap;
}

public String getCopyMenuString() {
return "share";
}
}
Expand Up @@ -189,6 +189,9 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
@Override
public void run() {
scrollView.requestLayout();
if(tableViewFactory.totalsView != null) {
tableViewFactory.totalsView.requestLayout();
}
requestLayout();
}
});
Expand Down
Expand Up @@ -66,7 +66,14 @@ public void createAll() {
if(tableView.headerContentStyle.wrap) {
headerView.testTextWrap();
updateFirstColumnHeaderHeight();
updateTotalsViewHeight();
}
tableView.post(new Runnable() {
@Override
public void run() {
updateGrabbers();
}
});
}

protected void updateRowHeights() {
Expand Down Expand Up @@ -307,13 +314,15 @@ public void updateHeaderViewLineCount() {
int maxLineCount = headerView.getMaxLineCount();
int headerHeight = maxLineCount * TableTheme.rowHeightFactor;
tableView.headerHeight = headerHeight;

ViewGroup.LayoutParams params = headerView.getLayoutParams();
params.height = headerHeight;
headerView.setLayoutParams(params);
FrameLayout.LayoutParams recyclerParams = (FrameLayout.LayoutParams) coupledRecyclerView.getLayoutParams();
recyclerParams.topMargin = headerHeight;
recyclerParams.topMargin = headerHeight + TableTheme.rowHeightFactor;
coupledRecyclerView.setLayoutParams(recyclerParams);

updateTotalsViewHeight();
updateFirstColumnsHeights();

tableView.post(new Runnable() {
Expand All @@ -325,6 +334,11 @@ public void run() {
if(firstColumnHeaderCell != null) {
firstColumnHeaderCell.requestLayout();
}

if(totalsView != null) {
totalsView.requestLayout();
}

coupledRecyclerView.requestLayout();
rootLayout.requestLayout();
headerView.requestLayout();
Expand Down Expand Up @@ -362,4 +376,12 @@ public void run() {
}
});
}

public void updateTotalsViewHeight() {
if(totalsView != null) {
FrameLayout.LayoutParams pp = (FrameLayout.LayoutParams)totalsView.getLayoutParams();
pp.topMargin = tableView.headerHeight;
totalsView.setLayoutParams(pp);
}
}
}

0 comments on commit af81780

Please sign in to comment.