Allow user to change image from list of images

This commit is contained in:
RichyHBM 2017-11-25 18:57:06 +00:00
parent e8c71161bb
commit 8258b972e1
8 changed files with 169 additions and 7 deletions

View file

@ -245,6 +245,8 @@ public class Entry {
public EntryThumbnail.EntryThumbnails getThumbnail() { return thumbnail; }
public void setThumbnail( EntryThumbnail.EntryThumbnails value) { thumbnail = value; }
public TokenCalculator.HashAlgorithm getAlgorithm() {
return this.algorithm;
}

View file

@ -3,21 +3,50 @@ package org.shadowice.flocke.andotp.Utilities;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGBuilder;
import org.shadowice.flocke.andotp.R;
import java.io.IOException;
public class EntryThumbnail {
public enum EntryThumbnails {
Default
Default(R.mipmap.ic_launcher_round),
Github(R.drawable.ic_github);
private int resource;
EntryThumbnails(int resource) {
this.resource = resource;
}
public int getResource() {
return resource;
}
}
public static Bitmap getThumbnailGraphic(Context context, String label, int size, EntryThumbnails thumbnail) {
switch (thumbnail) {
case Default:
if(thumbnail == EntryThumbnails.Default) {
LetterBitmap letterBitmap = new LetterBitmap(context);
return letterBitmap.getLetterTile(label, label, size, size);
default:
return BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
}
}
try {
Drawable drawable = context.getResources().getDrawable(thumbnail.getResource());
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch(Exception e) {
e.printStackTrace();
}
return BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_round);
}
}

View file

@ -28,6 +28,7 @@ import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.preference.PreferenceManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
@ -36,10 +37,15 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.shadowice.flocke.andotp.Database.Entry;
@ -268,6 +274,51 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
.show();
}
public void changeThumbnail(final int pos) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
int marginSmall = context.getResources().getDimensionPixelSize(R.dimen.activity_margin_small);
int marginMedium = context.getResources().getDimensionPixelSize(R.dimen.activity_margin_medium);
ListView list = new ListView(context);
list.setAdapter(new ThumbnailSelectionAdapter(context));
list.setDividerHeight(0);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int realIndex = getRealIndex(pos);
TextView v = (TextView) view.findViewById(R.id.thumbnail_selection_text);
EntryThumbnail.EntryThumbnails thumbnail = EntryThumbnail.EntryThumbnails.Default;
try {
thumbnail = EntryThumbnail.EntryThumbnails.valueOf(v.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size);
Entry e = entries.get(realIndex);
e.setThumbnailImage(EntryThumbnail.getThumbnailGraphic(context, e.getLabel(), size, thumbnail));
DatabaseHelper.saveDatabase(context, entries);
notifyItemChanged(pos);
}
});
FrameLayout container = new FrameLayout(context);
container.setPaddingRelative(marginMedium, marginSmall, marginMedium, 0);
container.addView(list);
builder.setTitle(R.string.menu_popup_change_image)
.setView(container)
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
})
.create()
.show();
}
public void editEntryTags(final int pos) {
final int realPos = getRealIndex(pos);
final Entry entry = entries.get(realPos);
@ -352,6 +403,9 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
if (id == R.id.menu_popup_editLabel) {
editEntryLabel(pos);
return true;
} else if(id == R.id.menu_popup_changeImage) {
changeThumbnail(pos);
return true;
} else if (id == R.id.menu_popup_editTags) {
editEntryTags(pos);
return true;

View file

@ -0,0 +1,50 @@
package org.shadowice.flocke.andotp.View;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.EntryThumbnail;
import java.util.ArrayList;
public class ThumbnailSelectionAdapter extends ArrayAdapter<EntryThumbnail.EntryThumbnails> {
private Context context;
public ThumbnailSelectionAdapter(Context context) {
super(context, R.layout.component_thumbnail_selection, new ArrayList<EntryThumbnail.EntryThumbnails>());
this.context = context;
for (EntryThumbnail.EntryThumbnails thumb : EntryThumbnail.EntryThumbnails.values()) {
add(thumb);
}
}
@NonNull
@Override
public View getView(int i, View view, @NonNull ViewGroup viewGroup) {
View newView;
if (view == null) {
newView = LayoutInflater.from(context).inflate(R.layout.component_thumbnail_selection , viewGroup, false);
} else{
newView = view;
}
ImageView imageView = (ImageView) newView.findViewById(R.id.thumbnail_selection_icon);
TextView textView = (TextView) newView.findViewById(R.id.thumbnail_selection_text);
EntryThumbnail.EntryThumbnails thumb = getItem(i);
int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size);
imageView.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, thumb.name(), size, thumb));
textView.setText(thumb.name());
return newView;
}
}

View file

@ -0,0 +1,4 @@
<vector android:height="24dp" android:viewportHeight="250.0"
android:viewportWidth="256.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#161614" android:pathData="M128,0C57.32,0 0,57.31 0,128C0,184.56 36.68,232.54 87.53,249.46C93.93,250.65 96.28,246.68 96.28,243.3C96.28,240.25 96.16,230.17 96.11,219.47C60.5,227.22 52.98,204.37 52.98,204.37C47.16,189.57 38.77,185.64 38.77,185.64C27.16,177.7 39.65,177.86 39.65,177.86C52.5,178.76 59.27,191.05 59.27,191.05C70.68,210.62 89.21,204.96 96.52,201.69C97.66,193.42 100.98,187.77 104.64,184.57C76.21,181.34 46.32,170.36 46.32,121.32C46.32,107.34 51.33,95.92 59.51,86.96C58.18,83.73 53.8,70.72 60.75,53.08C60.75,53.08 71.5,49.64 95.96,66.2C106.17,63.37 117.12,61.95 128,61.9C138.88,61.95 149.84,63.37 160.07,66.2C184.5,49.64 195.23,53.08 195.23,53.08C202.2,70.72 197.82,83.73 196.49,86.96C204.69,95.92 209.66,107.34 209.66,121.32C209.66,170.48 179.72,181.3 151.21,184.47C155.8,188.44 159.9,196.23 159.9,208.18C159.9,225.3 159.75,239.09 159.75,243.3C159.75,246.71 162.05,250.7 168.54,249.44C219.37,232.5 256,184.54 256,128C256,57.31 198.69,0 128,0ZM47.94,182.34C47.66,182.98 46.66,183.17 45.75,182.73C44.82,182.31 44.3,181.45 44.6,180.81C44.87,180.15 45.88,179.97 46.8,180.41C47.73,180.83 48.26,181.7 47.94,182.34ZM54.24,187.96C53.63,188.52 52.43,188.26 51.62,187.37C50.79,186.47 50.63,185.28 51.25,184.71C51.88,184.14 53.03,184.41 53.87,185.3C54.71,186.2 54.87,187.39 54.24,187.96ZM58.56,195.15C57.77,195.69 56.49,195.18 55.7,194.04C54.91,192.9 54.91,191.54 55.71,190.99C56.51,190.44 57.77,190.94 58.58,192.07C59.36,193.22 59.36,194.59 58.56,195.15ZM65.86,203.47C65.16,204.24 63.67,204.04 62.57,202.98C61.45,201.95 61.14,200.48 61.84,199.71C62.55,198.94 64.06,199.15 65.16,200.2C66.27,201.23 66.61,202.71 65.86,203.47ZM75.3,206.28C74.99,207.28 73.55,207.74 72.1,207.31C70.66,206.88 69.71,205.7 70,204.69C70.3,203.68 71.75,203.2 73.21,203.66C74.65,204.1 75.6,205.26 75.3,206.28ZM86.05,207.47C86.08,208.53 84.85,209.4 83.33,209.42C81.8,209.46 80.56,208.6 80.55,207.56C80.55,206.5 81.75,205.63 83.28,205.61C84.8,205.58 86.05,206.42 86.05,207.47ZM96.6,207.07C96.78,208.1 95.73,209.16 94.22,209.44C92.73,209.71 91.35,209.07 91.17,208.05C90.98,207 92.06,205.94 93.54,205.67C95.05,205.4 96.41,206.02 96.6,207.07Z"/>
</vector>

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/thumbnail_selection_icon"
android:layout_width="@dimen/card_thumbnail_size"
android:layout_height="@dimen/card_thumbnail_size"
android:layout_marginEnd="@dimen/activity_margin_small"/>
<TextView android:id="@+id/thumbnail_selection_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:layout_gravity="center_vertical" />
</LinearLayout>

View file

@ -5,6 +5,10 @@
android:id="@+id/menu_popup_editLabel"
android:title="@string/menu_popup_edit_label" />
<item
android:id="@+id/menu_popup_changeImage"
android:title="@string/menu_popup_change_image" />
<item
android:id="@+id/menu_popup_editTags"
android:title="@string/menu_popup_edit_tags" />

View file

@ -44,6 +44,7 @@
<string name="menu_sort_label">Label</string>
<string name="menu_popup_edit_label">Edit label</string>
<string name="menu_popup_change_image">Change image</string>
<string name="menu_popup_edit_tags">Edit tags</string>
<string name="menu_popup_remove">Remove</string>