Skip to content
This repository has been archived by the owner on Jan 26, 2020. It is now read-only.

Commit

Permalink
Add show/hide search engines
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Jan 27, 2016
1 parent ff6afbb commit efff8fb
Show file tree
Hide file tree
Showing 16 changed files with 381 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ public class CustomEngineTable {
public static final String TABLE_NAME = "search_engine";

public static final String COLUMN_ID = "id";
public static final String COLUMN_ENABLED = "enabled";
public static final String COLUMN_DATA = "data";

public static final String SQL_CREATE_ENTRIES = "create table " + TABLE_NAME
+ "("
+ COLUMN_ID + " integer primary key,"
+ COLUMN_ENABLED + " integer,"
+ COLUMN_DATA + " blob"
+ ");";
}
15 changes: 10 additions & 5 deletions app/src/main/java/rikka/searchbyimage/staticdata/CustomEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
public class CustomEngine {
public int id;
public int enabled;
public String name;
public String upload_url;
public String post_file_key;
Expand Down Expand Up @@ -56,11 +57,13 @@ private static List<CustomEngine> loadList(Context context) {
if (cursor.getCount() > 0) {
int columnId = cursor.getColumnIndex(CustomEngineTable.COLUMN_ID);
int columnData = cursor.getColumnIndex(CustomEngineTable.COLUMN_DATA);
int columnEnabled = cursor.getColumnIndex(CustomEngineTable.COLUMN_ENABLED);

cursor.moveToFirst();
do {
CustomEngine item = ParcelableUtils.unmarshall(cursor.getBlob(columnData), CustomEngineParcelable.CREATOR).data;
item.id = cursor.getInt(columnId);
item.enabled = cursor.getInt(columnEnabled);
list.add(item);
} while (cursor.moveToNext());
}
Expand Down Expand Up @@ -154,6 +157,7 @@ private static void addBuildInEngines(Context context, List<CustomEngine> list)
if (!ids[i]) {
CustomEngineParcelable parcelable = new CustomEngineParcelable();
parcelable.data.id = i;
parcelable.data.enabled = 1;
parcelable.data.name = BUILD_IN_ENGINE_NAME[i];
parcelable.data.upload_url = BUILD_IN_ENGINE_URL[i];
parcelable.data.post_file_key = BUILD_IN_ENGINE_FILE_KEY[i];
Expand Down Expand Up @@ -186,21 +190,22 @@ private static void addBuildInEngines(Context context, List<CustomEngine> list)
}
}

private static void addEngineToDb(Context context, CustomEngineParcelable parcelable, int id) {
public static void addEngineToDb(Context context, CustomEngineParcelable parcelable, int id) {
SQLiteDatabase db = DatabaseHelper.instance(context).getWritableDatabase();

ContentValues values = new ContentValues();
values.put(CustomEngineTable.COLUMN_ID, id);
values.put(CustomEngineTable.COLUMN_ENABLED, 1);
values.put(CustomEngineTable.COLUMN_DATA, ParcelableUtils.marshall(parcelable));

db.insert(CustomEngineTable.TABLE_NAME, null, values);
}

private static void addEngineToList(CustomEngineParcelable parcelable) {
addEngineToList(parcelable, sList);
public static void addEngineToList(CustomEngine data) {
addEngineToList(data, sList);
}

private static void addEngineToList(CustomEngineParcelable parcelable, List<CustomEngine> list) {
list.add(parcelable.data);
public static void addEngineToList(CustomEngine data, List<CustomEngine> list) {
list.add(data);
}
}
173 changes: 106 additions & 67 deletions app/src/main/java/rikka/searchbyimage/ui/EditSiteInfoActivity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package rikka.searchbyimage.ui;

import android.app.Activity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -28,13 +34,16 @@
import rikka.searchbyimage.staticdata.CustomEngineParcelable;
import rikka.searchbyimage.ui.apdater.PostFormAdapter;
import rikka.searchbyimage.utils.ParcelableUtils;
import rikka.searchbyimage.utils.URLUtils;
import rikka.searchbyimage.utils.Utils;

public class EditSiteInfoActivity extends AppCompatActivity {
public static final String EXTRA_EDIT_LOCATION =
"rikka.searchbyimage.ui.EditSiteInfoActivity.EXTRA_EDIT_LOCATION";

DatabaseHelper mDbHelper;

Activity mActivity;
CoordinatorLayout mCoordinatorLayout;
Toolbar mToolbar;
FloatingActionButton mFAB;
Expand All @@ -47,6 +56,7 @@ public class EditSiteInfoActivity extends AppCompatActivity {
List<CustomEngine> mData;
CustomEngine mItem;
PostFormAdapter mAdapter;
MyLinearLayoutManager mLayoutManager;

boolean mEnabled = true;

Expand All @@ -57,6 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_engine);

mActivity = this;

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Expand All @@ -81,7 +93,7 @@ public void onClick(View view) {
}

if (!check()) {
Snackbar.make(mCoordinatorLayout, "Check your data", Snackbar.LENGTH_LONG).show();
Snackbar.make(mCoordinatorLayout, R.string.check_your_data, Snackbar.LENGTH_LONG).show();
return;
}

Expand All @@ -96,35 +108,44 @@ public void onClick(View view) {
});

mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this) {
mLayoutManager = new MyLinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(EditSitesActivity.getAdapter(this));
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean canScrollVertically() {
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int width = View.MeasureSpec.getSize(widthSpec);
int height = 0;
int childHeight = 0;
for (int i = 0; i < getItemCount(); i++) {
try {
childHeight = measureScrapChildHeight(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED));
height = height + childHeight;

} catch (IndexOutOfBoundsException ignore) {
height = height + childHeight;
}
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder.getLayoutPosition() == mAdapter.getItemCount() - 1) {
return 0;
}
setMeasuredDimension(width, height);
return super.getMovementFlags(recyclerView, viewHolder);
}
});
mRecyclerView.setAdapter(EditSitesActivity.getAdapter(this));
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
mAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());
mAdapter.setItemCount(mAdapter.getItemCount() - 1);
mLayoutManager.setFakeItemCount(1);
mCoordinatorLayout.postDelayed(new Runnable() {
@Override
public void run() {
mLayoutManager.setFakeItemCount(0);
mRecyclerView.requestLayout();
}
}, 500);
}
};

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);

itemTouchHelper.attachToRecyclerView(mRecyclerView);

Intent intent = getIntent();
if (intent.hasExtra(EXTRA_EDIT_LOCATION)) {
Expand Down Expand Up @@ -158,15 +179,37 @@ public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
}
} else {
mAdapter = new PostFormAdapter();
mItem = mAdapter.getData();
mRecyclerView.setAdapter(mAdapter);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.edit_info, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.help:
URLUtils.Open(
"https://github.com/RikkaW/SearchByImage/wiki/%E5%B8%AE%E5%8A%A9%EF%BC%9A%E8%87%AA%E5%AE%9A%E4%B9%89%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E",
this);
return true;
}

return super.onOptionsItemSelected(item);
}

private void modify() {
SQLiteDatabase db = mDbHelper.getReadableDatabase();

CustomEngineParcelable parcelable = getParcelable();
CustomEngineParcelable parcelable = new CustomEngineParcelable();
parcelable.data = getData();

ContentValues values = new ContentValues();
values.put(CustomEngineTable.COLUMN_DATA, ParcelableUtils.marshall(parcelable));
Expand All @@ -184,12 +227,15 @@ private void modify() {
mItem.upload_url = parcelable.data.upload_url;
mItem.post_file_key = parcelable.data.post_file_key;
mItem.result_open_action = parcelable.data.result_open_action;
mItem.post_text_key = parcelable.data.post_text_key;
mItem.post_text_value = parcelable.data.post_text_value;
mItem.post_text_type = parcelable.data.post_text_type;

EditSitesActivity.getAdapter(this).notifyItemChanged(mLocation + 2);
EditSitesActivity.getAdapter(this).notifyItemChanged(mLocation + 1);
}

private void add() {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
/*SQLiteDatabase db = mDbHelper.getWritableDatabase();
CustomEngineParcelable parcelable = getParcelable();
parcelable.data.id = CustomEngine.getAvailableId();
mData.add(parcelable.data);
Expand All @@ -198,55 +244,48 @@ private void add() {
values.put(CustomEngineTable.COLUMN_ID, parcelable.data.id);
values.put(CustomEngineTable.COLUMN_DATA, ParcelableUtils.marshall(parcelable));
db.insert(CustomEngineTable.TABLE_NAME, null, values);
db.insert(CustomEngineTable.TABLE_NAME, null, values);*/

CustomEngineParcelable parcelable = new CustomEngineParcelable();
parcelable.data = getData();
parcelable.data.id = CustomEngine.getAvailableId();
parcelable.data.enabled = 1;

CustomEngine.addEngineToDb(this, parcelable, parcelable.data.id);
CustomEngine.addEngineToList(parcelable.data);

EditSitesActivity.getAdapter(this).notifyItemInserted(mData.size() - 1);
}

private CustomEngineParcelable getParcelable() {
CustomEngineParcelable parcelable = new CustomEngineParcelable();
parcelable.data.name = mEditTextName.getText().toString();
parcelable.data.upload_url = mEditTextUrl.getText().toString();
parcelable.data.post_file_key = mEditTextFileKey.getText().toString();
parcelable.data.result_open_action = mSpinner.getSelectedItemPosition();
parcelable.data.post_text_type = mItem.post_text_type;
parcelable.data.post_text_value = mItem.post_text_value;
parcelable.data.post_text_key = mItem.post_text_key;
return parcelable;
private CustomEngine getData() {
CustomEngine data = new CustomEngine();
data.name = mEditTextName.getText().toString();
data.upload_url = mEditTextUrl.getText().toString();
data.post_file_key = mEditTextFileKey.getText().toString();
data.result_open_action = mSpinner.getSelectedItemPosition();

data.post_text_key.clear();
data.post_text_value.clear();
data.post_text_type.clear();

for (int i = 0; i < mRecyclerView.getChildCount(); i++) {
View view = mRecyclerView.getChildAt(i);
if (view == null)
continue;
EditText key = (EditText) view.findViewById(R.id.editText_key);
EditText value = (EditText) view.findViewById(R.id.editText_value);
if (key != null && value != null) {
data.post_text_key.add(key.getEditableText().toString());
data.post_text_value.add(value.getEditableText().toString());
data.post_text_type.add(0);
}
}
return data;
}

private boolean check() {
return mEditTextName.getText().toString().length() != 0
&& (URLUtil.isHttpUrl(mEditTextUrl.getText().toString()) || URLUtil.isHttpsUrl(mEditTextUrl.getText().toString()))
&& mEditTextFileKey.getText().toString().length() != 0;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}

return super.onOptionsItemSelected(item);
}

private int measureScrapChildHeight(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec) throws IndexOutOfBoundsException {
View view = recycler.getViewForPosition(position);
int height = 0;
if (view != null) {

RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
view.getPaddingLeft() + view.getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
view.getPaddingTop() + view.getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
height = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
return height;
}
}
Loading

0 comments on commit efff8fb

Please sign in to comment.