Skip to content

Commit

Permalink
Merge pull request #85 from HiddenRamblings/polish_amiibo_image_view
Browse files Browse the repository at this point in the history
Add ImageActivity
  • Loading branch information
North101 authored Sep 2, 2017
2 parents 56aba33 + 4d5035e commit e1c2aae
Show file tree
Hide file tree
Showing 22 changed files with 470 additions and 80 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<activity
android:name=".ImageActivity_"
android:theme="@style/AppTheme"/>
<activity
android:name=".SettingsActivity_"
android:theme="@style/AppTheme"/>
Expand Down
48 changes: 30 additions & 18 deletions app/src/main/java/com/hiddenramblings/tagmo/BrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

@EActivity(R.layout.browser_layout)
@OptionsMenu({R.menu.browser_menu})
public class BrowserActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, SwipeRefreshLayout.OnRefreshListener, RecyclerItemClickListener.OnItemClickListener {
public class BrowserActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, SwipeRefreshLayout.OnRefreshListener {
public static final int SORT_ID = 0x0;
public static final int SORT_NAME = 0x1;
public static final int SORT_AMIIBO_SERIES = 0x2;
Expand Down Expand Up @@ -183,7 +183,6 @@ protected void afterViews() {
this.swipeRefreshLayout.setOnRefreshListener(this);
this.listView.setLayoutManager(new LinearLayoutManager(this));
this.listView.setAdapter(new AmiiboFilesAdapter(this));
this.listView.addOnItemTouchListener(new RecyclerItemClickListener(this, this.listView, this));
if (this.amiiboFiles == null) {
this.onRefresh();
} else {
Expand All @@ -202,21 +201,6 @@ protected AmiiboFilesAdapter getListAdapter() {
return (AmiiboFilesAdapter) this.listView.getAdapter();
}

@Override
public void onItemClick(View view, int position) {
AmiiboFile item = getListAdapter().getItem(position);

Intent returnIntent = new Intent();
returnIntent.setData(Uri.fromFile(new File(item.filePath)));

this.setResult(Activity.RESULT_OK, returnIntent);
finish();
}

@Override
public void onLongItemClick(View view, int position) {
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
Expand Down Expand Up @@ -754,6 +738,8 @@ static abstract class AmiiboVewHolder extends RecyclerView.ViewHolder {
TextView txtCharacter;
TextView txtPath;
ImageView imageAmiibo;
AmiiboFile amiiboFile = null;
BrowserActivity activity;

SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() {
@Override
Expand All @@ -776,6 +762,17 @@ public void onResourceReady(Bitmap resource, Transition transition) {
public AmiiboVewHolder(View itemView) {
super(itemView);

this.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent returnIntent = new Intent();
returnIntent.setData(Uri.fromFile(new File(amiiboFile.filePath)));

activity.setResult(Activity.RESULT_OK, returnIntent);
activity.finish();
}
});

this.txtTagInfo = itemView.findViewById(R.id.txtTagInfo);
this.txtName = itemView.findViewById(R.id.txtName);
this.txtTagId = itemView.findViewById(R.id.txtTagId);
Expand All @@ -785,9 +782,24 @@ public AmiiboVewHolder(View itemView) {
this.txtCharacter = itemView.findViewById(R.id.txtCharacter);
this.txtPath = itemView.findViewById(R.id.txtPath);
this.imageAmiibo = itemView.findViewById(R.id.imageAmiibo);
this.imageAmiibo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putLong(ImageActivity.INTENT_EXTRA_AMIIBO_ID, amiiboFile.id);

Intent intent = new Intent(activity, ImageActivity_.class);
intent.putExtras(bundle);

activity.startActivity(intent);
}
});
}

void bind(BrowserActivity activity, AmiiboFile item) {
void bind(BrowserActivity activity, final AmiiboFile item) {
this.activity = activity;
this.amiiboFile = item;

String tagInfo = "";
String amiiboHexId = "";
String amiiboName = "";
Expand Down
238 changes: 238 additions & 0 deletions app/src/main/java/com/hiddenramblings/tagmo/ImageActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package com.hiddenramblings.tagmo;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hiddenramblings.tagmo.amiibo.Amiibo;
import com.hiddenramblings.tagmo.amiibo.AmiiboManager;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;

@EActivity(R.layout.activity_image)
@OptionsMenu({R.menu.image_menu})
public class ImageActivity extends AppCompatActivity {
public static final String INTENT_EXTRA_AMIIBO_ID = "AMIIBO_ID";

@ViewById(R.id.imageAmiibo)
ImageView imageView;
@ViewById(R.id.bottom_sheet1)
View bottomSheet;
@ViewById(R.id.toggle)
ImageView toggle;

@ViewById(R.id.txtTagInfo)
TextView txtTagInfo;
@ViewById(R.id.txtTagId)
TextView txtTagId;
@ViewById(R.id.txtName)
TextView txtName;
@ViewById(R.id.txtGameSeries)
TextView txtGameSeries;
//@ViewById(R.id.txtCharacter)
//TextView txtCharacter;
@ViewById(R.id.txtAmiiboType)
TextView txtAmiiboType;
@ViewById(R.id.txtAmiiboSeries)
TextView txtAmiiboSeries;

BottomSheetBehavior mBottomSheetBehavior1;

@Extra(INTENT_EXTRA_AMIIBO_ID)
long amiiboId;

Amiibo amiibo;
AmiiboManager amiiboManager;

@AfterViews
void afterViews() {
mBottomSheetBehavior1 = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_COLLAPSED);

toggle.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
int height = ((View) view.getParent()).getPaddingTop() + view.getHeight() + view.getPaddingBottom();
mBottomSheetBehavior1.setPeekHeight(height);
imageView.setPadding(imageView.getPaddingLeft(), imageView.getPaddingTop(), imageView.getPaddingRight(), imageView.getPaddingTop() + height);
}
});

updateImage();
loadAmiiboManager();
}

@Click(R.id.toggle)
void onToggleClick() {
if (mBottomSheetBehavior1.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
toggle.setImageResource(R.drawable.ic_expand_more_white_24dp);
mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_EXPANDED);
} else {
toggle.setImageResource(R.drawable.ic_expand_less_white_24dp);
mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}

@Background
void loadAmiiboManager() {
AmiiboManager amiiboManager = null;
try {
amiiboManager = Util.loadAmiiboManager(this);
} catch (IOException | JSONException | ParseException e) {
e.printStackTrace();
}

setAmiiboManager(amiiboManager);
}

@UiThread
void setAmiiboManager(AmiiboManager amiiboManager) {
this.amiiboManager = amiiboManager;
this.updateView();
}

void updateImage() {
Glide.with(this)
.load(getImageUrl())
.into(imageView);
}

void updateView() {
String tagInfo = "";
String amiiboHexId = "";
String amiiboName = "";
String amiiboSeries = "";
String amiiboType = "";
String gameSeries = "";

amiibo = null;
if (amiiboId == 0) {
tagInfo = "<Blank tag>";
} else {
if (this.amiiboManager != null) {
amiibo = amiiboManager.amiibos.get(amiiboId);
if (amiibo == null)
amiibo = new Amiibo(amiiboManager, amiiboId, null, null);
}
if (amiibo != null) {
amiiboHexId = TagUtil.amiiboIdToHex(amiibo.id);
if (amiibo.name != null)
amiiboName = amiibo.name;
if (amiibo.getAmiiboSeries() != null)
amiiboSeries = amiibo.getAmiiboSeries().name;
if (amiibo.getAmiiboType() != null)
amiiboType = amiibo.getAmiiboType().name;
if (amiibo.getGameSeries() != null)
gameSeries = amiibo.getGameSeries().name;
} else {
tagInfo = "<Unknown amiibo id: " + TagUtil.amiiboIdToHex(amiiboId) + ">";
}
}

txtTagInfo.setText(tagInfo);
setAmiiboInfoText(txtName, amiiboName, !tagInfo.isEmpty());
setAmiiboInfoText(txtTagId, amiiboHexId, !tagInfo.isEmpty());
setAmiiboInfoText(txtAmiiboSeries, amiiboSeries, !tagInfo.isEmpty());
setAmiiboInfoText(txtAmiiboType, amiiboType, !tagInfo.isEmpty());
setAmiiboInfoText(txtGameSeries, gameSeries, !tagInfo.isEmpty());
}

void setAmiiboInfoText(TextView textView, CharSequence text, boolean hasTagInfo) {
if (hasTagInfo) {
textView.setText("");
} else if (text.length() == 0) {
textView.setText("Unknown");
textView.setEnabled(false);
} else {
textView.setText(text);
textView.setEnabled(true);
}
}

String getImageUrl() {
return Amiibo.getImageUrl(amiiboId);
}

@OptionsItem(R.id.mnu_save)
void onSaveClicked() {
mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_EXPANDED);
if (true)
return;

final View view = this.getLayoutInflater().inflate(R.layout.edit_text, null);
final EditText editText = view.findViewById(R.id.editText);
if (amiibo != null) {
editText.setText(amiibo.getName());
} else {
editText.setText(TagUtil.amiiboIdToHex(amiiboId));
}

(new AlertDialog.Builder(this))
.setTitle("Save Image")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
File dir = Util.getDataDir();
if (!dir.isDirectory())
dir.mkdir();

final File file = new File(dir.getAbsolutePath(), editText.getText().toString() + ".png");

Glide.with(ImageActivity.this)
.asBitmap()
.load(getImageUrl())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition transition) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
resource.compress(Bitmap.CompressFormat.PNG, 100, fos);

String text = "Saved file as " + Util.friendlyPath(file.getAbsolutePath());
Toast.makeText(ImageActivity.this, text, Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
})
.setNegativeButton("Cancel", null)
.setView(view)
.show();
}
}
24 changes: 23 additions & 1 deletion app/src/main/java/com/hiddenramblings/tagmo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class MainActivity extends AppCompatActivity {
public static final int VIEW_TYPE_COMPACT = 1;
public static final int VIEW_TYPE_LARGE = 2;

public static abstract class AmiiboView extends Fragment {
public static abstract class AmiiboView extends Fragment implements View.OnClickListener {
TextView txtTagInfo;
TextView txtTagId;
TextView txtName;
Expand Down Expand Up @@ -204,6 +204,25 @@ void setAmiiboInfoText(TextView textView, CharSequence text, boolean hasTagInfo)
textView.setEnabled(true);
}
}

@Override
public void onClick(View view) {
long amiiboId;
try {
amiiboId = TagUtil.amiiboIdFromTag(this.tagData);
} catch (Exception e) {
e.printStackTrace();
return;
}

Bundle bundle = new Bundle();
bundle.putLong(ImageActivity.INTENT_EXTRA_AMIIBO_ID, amiiboId);

Intent intent = new Intent(this.getContext(), ImageActivity_.class);
intent.putExtras(bundle);

this.startActivity(intent);
}
}

@EFragment(R.layout.amiibo_simple_card)
Expand All @@ -225,6 +244,7 @@ void afterViews() {
txtAmiiboType = this.getView().findViewById(R.id.txtAmiiboType);
txtAmiiboSeries = this.getView().findViewById(R.id.txtAmiiboSeries);
imageAmiibo = this.getView().findViewById(R.id.imageAmiibo);
imageAmiibo.setOnClickListener(this);

updateView();
}
Expand All @@ -249,6 +269,7 @@ void afterViews() {
txtAmiiboType = this.getView().findViewById(R.id.txtAmiiboType);
txtAmiiboSeries = this.getView().findViewById(R.id.txtAmiiboSeries);
imageAmiibo = this.getView().findViewById(R.id.imageAmiibo);
imageAmiibo.setOnClickListener(this);

updateView();
}
Expand All @@ -273,6 +294,7 @@ void afterViews() {
txtAmiiboType = this.getView().findViewById(R.id.txtAmiiboType);
txtAmiiboSeries = this.getView().findViewById(R.id.txtAmiiboSeries);
imageAmiibo = this.getView().findViewById(R.id.imageAmiibo);
imageAmiibo.setOnClickListener(this);

updateView();
}
Expand Down
Loading

0 comments on commit e1c2aae

Please sign in to comment.