231 lines
7.9 KiB
Java
231 lines
7.9 KiB
Java
package com.utopiaindustries.qualitycontrol.adapters;
|
|
|
|
import android.app.Activity;
|
|
import android.app.AlertDialog;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.net.Uri;
|
|
import android.os.Environment;
|
|
import android.provider.MediaStore;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.AdapterView;
|
|
import android.widget.AutoCompleteTextView;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.core.content.FileProvider;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.bumptech.glide.Glide;
|
|
import com.bumptech.glide.request.RequestOptions;
|
|
import com.utopiaindustries.qualitycontrol.R;
|
|
import com.utopiaindustries.qualitycontrol.utils.ImageSelectionListener;
|
|
import com.utopiaindustries.qualitycontrol.viewmodels.ItemModel;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.function.Consumer;
|
|
|
|
public class ItemStepsAdapter extends RecyclerView.Adapter<ItemStepsAdapter.ItemViewHolder> {
|
|
|
|
private final Context context;
|
|
private final List<ItemModel> items;
|
|
private final List<String> dropdownOptions = new ArrayList<>();
|
|
double percentage = 0.0, selectedValue = 0.0;
|
|
private final ImageSelectionListener imageSelectionListener;
|
|
|
|
public ItemStepsAdapter(Context context, List<ItemModel> items, ImageSelectionListener listener) {
|
|
this.context = context;
|
|
this.items = items;
|
|
this.imageSelectionListener = listener;
|
|
|
|
dropdownOptions.add("1");
|
|
dropdownOptions.add("2");
|
|
dropdownOptions.add("3");
|
|
dropdownOptions.add("4");
|
|
dropdownOptions.add("5");
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_view, parent, false);
|
|
return new ItemViewHolder(view);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
|
|
ItemModel currentItem = items.get(position);
|
|
|
|
// Set data
|
|
holder.et_remarks.setText(currentItem.getRemarks());
|
|
holder.tvPercentage.setText(currentItem.getPercentage());
|
|
//Log.e("Spinner-selection: ",""+currentItem.getSelectedOption());
|
|
holder.scoreTextview.setText(String.valueOf(currentItem.getSelectedOption()));
|
|
|
|
if (currentItem.getImageUri() != null) {
|
|
|
|
Glide.with(context)
|
|
.load(currentItem.getImageUri()) // Glide will handle the decoding and placeholder
|
|
.placeholder(R.drawable.img_load)
|
|
.apply(new RequestOptions().centerCrop())
|
|
.into(holder.imageView);
|
|
|
|
}
|
|
|
|
currentItem.setStepId(position + 1);
|
|
switch (position) {
|
|
case 0:
|
|
holder.tvItemName.setText("Sort");
|
|
break;
|
|
|
|
case 1:
|
|
holder.tvItemName.setText("Set In Order");
|
|
break;
|
|
|
|
case 2:
|
|
holder.tvItemName.setText("Shine");
|
|
break;
|
|
|
|
case 3:
|
|
holder.tvItemName.setText("Standardize");
|
|
break;
|
|
|
|
case 4:
|
|
holder.tvItemName.setText("Sustain");
|
|
break;
|
|
|
|
case 5:
|
|
holder.tvItemName.setText("Safety");
|
|
break;
|
|
}
|
|
|
|
|
|
holder.et_remarks.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
// Do nothing
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
currentItem.setRemarks(s.toString());
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
// Do nothing
|
|
}
|
|
});
|
|
|
|
holder.imageView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (imageSelectionListener != null) {
|
|
imageSelectionListener.onSelectImage(holder.getAdapterPosition()); // Notify the fragment
|
|
}
|
|
}
|
|
});
|
|
|
|
ScoreSpinnerAdapter scoreSpinnerAdapter = new ScoreSpinnerAdapter(context, dropdownOptions);
|
|
holder.scoreTextview.setAdapter(scoreSpinnerAdapter);
|
|
|
|
holder.scoreTextview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
@Override
|
|
public void onItemClick(AdapterView<?> parent, View view, int sub_position, long id) {
|
|
|
|
currentItem.setSelectedOption(sub_position + 1);
|
|
currentItem.setRating(sub_position + 1);
|
|
|
|
selectedValue = Double.parseDouble(parent.getItemAtPosition(sub_position).toString());
|
|
percentage = (selectedValue / 5) * 100;
|
|
holder.tvPercentage.setText(String.valueOf(percentage) + " %");
|
|
currentItem.setPercentage(String.valueOf(percentage));
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return items.size();
|
|
}
|
|
|
|
public static class ItemViewHolder extends RecyclerView.ViewHolder {
|
|
TextView tvItemName, tvPercentage;
|
|
AutoCompleteTextView scoreTextview;
|
|
EditText et_remarks;
|
|
ImageView imageView;
|
|
|
|
public ItemViewHolder(@NonNull View itemView) {
|
|
super(itemView);
|
|
tvItemName = itemView.findViewById(R.id.tv_item_name);
|
|
scoreTextview = itemView.findViewById(R.id.score_textview);
|
|
tvPercentage = itemView.findViewById(R.id.tv_percentage);
|
|
et_remarks = itemView.findViewById(R.id.et_remarks);
|
|
imageView = itemView.findViewById(R.id.img1);
|
|
|
|
}
|
|
}
|
|
|
|
public void uriToByteArrayAsync(
|
|
Context context,
|
|
Uri uri,
|
|
int targetSizeInKB,
|
|
Consumer<byte[]> onSuccess,
|
|
Consumer<Exception> onError
|
|
) {
|
|
new Thread(() -> {
|
|
try {
|
|
int targetSizeInBytes = targetSizeInKB * 1024;
|
|
|
|
// Load the image as a Bitmap without scaling
|
|
Bitmap bitmap;
|
|
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
|
|
bitmap = BitmapFactory.decodeStream(inputStream);
|
|
}
|
|
|
|
if (bitmap == null) {
|
|
throw new IOException("Failed to decode image from URI.");
|
|
}
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
int minQuality = 10;
|
|
int maxQuality = 100;
|
|
int quality = maxQuality;
|
|
|
|
// Binary search for the best quality that meets the target size
|
|
while (minQuality <= maxQuality) {
|
|
byteArrayOutputStream.reset();
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
|
|
|
|
int byteSize = byteArrayOutputStream.size();
|
|
if (byteSize > targetSizeInBytes) {
|
|
maxQuality = quality - 1;
|
|
} else {
|
|
minQuality = quality + 1;
|
|
}
|
|
quality = (minQuality + maxQuality) / 2;
|
|
}
|
|
|
|
onSuccess.accept(byteArrayOutputStream.toByteArray());
|
|
} catch (IOException e) {
|
|
onError.accept(e);
|
|
}
|
|
}).start();
|
|
}
|
|
}
|