Skip to content

Commit

Permalink
Implement add tags functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekscl committed Mar 21, 2018
1 parent 5918b13 commit 431aca0
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Starred {
private String mnr;
private String title;
private SearchResult.MediaType mediaType;
private Tag tag;

@Override
public String toString() {
Expand Down Expand Up @@ -94,4 +95,18 @@ public SearchResult.MediaType getMediaType() {
public void setMediaType(SearchResult.MediaType mediaType) {
this.mediaType = mediaType;
}

/**
* Get this item's tag
*/
public Tag getTag() {
return tag;
}

/**
* Set this item's tag
*/
public void setTag(Tag tag) {
this.tag = tag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
package de.geeksfactory.opacclient.frontend;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
Expand All @@ -37,6 +39,7 @@
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Html;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuInflater;
Expand All @@ -46,6 +49,7 @@
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
Expand Down Expand Up @@ -99,6 +103,7 @@ public class StarredFragment extends Fragment implements
private int activatedPosition = ListView.INVALID_POSITION;
private TextView tvWelcome;
private Starred sItem;
private String tagName = "";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand Down Expand Up @@ -473,6 +478,12 @@ private void setActivatedPosition(int position) {
activatedPosition = position;
}

private void addTag(Starred item, String tagName) {
StarDataSource data = new StarDataSource(getActivity());
sItem = item;
data.addTag(item, tagName);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand All @@ -498,6 +509,7 @@ public ItemListAdapter() {
@Override
public void bindView(View view, Context context, Cursor cursor) {
Starred item = StarDataSource.cursorToItem(cursor);
item.setTag(StarDataSource.cursorToTag(cursor));

TextView tv = (TextView) view.findViewById(R.id.tvTitle);
if (item.getTitle() != null) {
Expand All @@ -513,6 +525,42 @@ public void bindView(View view, Context context, Cursor cursor) {
ivType.setImageBitmap(null);
}

TextView tagView = (TextView) view.findViewById(R.id.tvTag);
if (item.getTag() != null) {
tagView.setText(Html.fromHtml(item.getTag().getTagName()));
} else {
tagView.setText("");
}

// EditText et = (EditText) view.findViewById(R.id.etvAddTagText);
ImageView ivAddTag = (ImageView) view.findViewById(R.id.ivAddTag);
ivAddTag.setFocusableInTouchMode(false);
ivAddTag.setFocusable(false);
ivAddTag.setTag(item);

ivAddTag.setOnClickListener(arg0 -> {
// Create alert dialog box for entering of tag
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Add tag to " + item.getTitle());

// Set up the input
final EditText input = new EditText(context);

// Specify the type of input expected
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);

// Set up the buttons
builder.setPositiveButton("OK",
(dialog, which) -> tagName = input.getText().toString());

builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());

builder.show();
Starred item1 = (Starred) arg0.getTag();
addTag(item1, tagName);
});

ImageView ivDelete = (ImageView) view.findViewById(R.id.ivDelete);
ivDelete.setFocusableInTouchMode(false);
ivDelete.setFocusable(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,13 @@ public static Tag cursorToTag(Cursor cursor) {
return tag;
}

public long addTag(Starred item, Tag tag) {
public long addTag(Starred item, String tagName) {
ContentValues values = new ContentValues();
values.put("tag", tag.getTagName());
values.put("tag", tagName);
database.insert(StarDatabase.TAGS_TABLE, null, values);

values = new ContentValues();
values.put("tag", tag.getId());
values.put("tag", getTagByTagName(tagName).getId());
values.put("item", item.getId());
return database.insert(StarDatabase.STAR_TAGS_TABLE, null, values);
}
Expand All @@ -245,4 +246,19 @@ public void removeTag(Tag tag) {
database.delete(StarDatabase.TAGS_TABLE, "tag=?", selA);
}
}

public Tag getTagByTagName(String tagName) {
String[] selA = {tagName};
Cursor cursor = database.query(StarDatabase.TAGS_TABLE, StarDatabase.TAGS_COLUMNS, "tag = ?",
selA, null, null, null);
Tag item = null;
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
item = cursorToTag(cursor);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class StarDatabase extends SQLiteOpenHelper {
public static final String STAR_WHERE_NR_LIB = "bib = ? AND medianr = ?";
public static final String[] COLUMNS = {"id AS _id", "medianr", "bib",
"title", "mediatype"};
public static final String[] TAGS_COLUMNS = {"id", "tags"};
public static final String[] TAGS_COLUMNS = {"id", "tag"};
public static final String[] STAR_TAGS_COLUMNS = {"tag", "item"};
private static final String DATABASE_NAME = "starred.db";
private static final int DATABASE_VERSION = 7; // REPLACE ONUPGRADE IF YOU
Expand Down
18 changes: 18 additions & 0 deletions opacclient/opacapp/src/main/res/layout/listitem_starred.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,22 @@
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
android:id="@+id/tvTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ivMediaType"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"/>

<ImageView
android:id="@+id/ivAddTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/ivDelete"
android:layout_toRightOf="@id/ivMediaType"
app:srcCompat="@drawable/ic_add_24dp"
android:contentDescription="@string/tag_add"/>
</RelativeLayout>
1 change: 1 addition & 0 deletions opacclient/opacapp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<string name="account_city">Library</string>
<string name="account_delete_confirm">Really delete account?</string>
<string name="account_select">Select Account</string>
<string name="tag_add">Add tag</string>
<string name="privacy">Privacy</string>
<string name="nav_hl_library">Library</string>
<string name="nav_search">Search</string>
Expand Down

0 comments on commit 431aca0

Please sign in to comment.