Skip to content

Commit

Permalink
Add FTS to search feature, turn edit text into search view
Browse files Browse the repository at this point in the history
  • Loading branch information
duonghaipham committed Apr 10, 2021
1 parent e7d33d0 commit 8bead9d
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 83 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
32 changes: 28 additions & 4 deletions app/src/main/java/com/example/love/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.example.love;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;

import android.content.Intent;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.love.database.StoryDataSource;
import com.example.love.model.Story;
Expand All @@ -14,7 +15,7 @@
import java.util.List;

public class MainActivity extends AppCompatActivity {
private ImageView ivSearch;
private SearchView svSearch;
private GridView gvStories;

@Override
Expand All @@ -29,17 +30,40 @@ protected void onCreate(Bundle savedInstanceState) {
intent.putExtra("id", position);
startActivity(intent);
});

svSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
StoryDataSource source = new StoryDataSource(MainActivity.this);
List<Story> stories = source.getStoriesBySearch(query);
loadGridStory(stories);
svSearch.clearFocus();
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (newText.isEmpty()) {
loadData();
}
return false;
}
});
}

private void map() { // map between front-end control and its back-end ones
ivSearch = findViewById(R.id.iv_search);
svSearch = findViewById(R.id.sv_search);
gvStories = findViewById(R.id.gv_stories);
}

private void loadData() { // load data from database and fill into user interface
StoryDataSource source = new StoryDataSource(MainActivity.this);
List<Story> allStories = source.getAllStories();
StoryDataAdapter adapter = new StoryDataAdapter(MainActivity.this, allStories);
loadGridStory(allStories);
}

private void loadGridStory(List<Story> stories) { // set adapter
StoryDataAdapter adapter = new StoryDataAdapter(MainActivity.this, stories);
gvStories.setAdapter(adapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.love.database.ChapterDataSource;
import com.example.love.model.Chapter;

public class ReadingInterfaceActivity extends AppCompatActivity {

private TextView tvChapterBar;
private TextView tvBody;
private ImageView ivPrevious;
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/com/example/love/database/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public static final String STORY_NUMBER_CHAPTERS = "number_chapters";
public static final String STORY_AVATAR = "avatar";

public static final String VIRTUAL_TABLE_STORY = "VirtualStory";

public static final String TABLE_CHAPTER = "Chapter"; // table chapter, below are its fields
public static final String CHAPTER_STORY = "story";
public static final String CHAPTER_INDEX = "id";
public static final String CHAPTER_NAME = "chapter";
public static final String CHAPTER_CONTENT = "content";

private static final String CREATE_TABLE_STORY = "CREATE TABLE " + TABLE_STORY + "(" +
private static final String CREATE_TABLE_STORY = "CREATE TABLE " + TABLE_STORY + " (" +
STORY_ID + " INTEGER," +
STORY_NAME + " TEXT," +
STORY_AUTHOR + " TEXT," +
STORY_NUMBER_CHAPTERS + " INTEGER," +
STORY_AVATAR + " BLOB," +
"PRIMARY KEY (" + STORY_ID +"))";

private static final String CREATE_VIRTUAL_TABLE_STORY = "CREATE VIRTUAL TABLE " + VIRTUAL_TABLE_STORY + " USING FTS3 (" +
STORY_ID + "," +
STORY_NAME + "," +
STORY_AUTHOR + ")";

private static final String CREATE_TABLE_CHAPTER = "CREATE TABLE " + TABLE_CHAPTER + "(" +
CHAPTER_STORY + " INTEGER," +
CHAPTER_INDEX + " INTEGER," +
Expand All @@ -43,6 +51,7 @@ public DatabaseHelper(Context context) {
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STORY);
db.execSQL(CREATE_VIRTUAL_TABLE_STORY);
db.execSQL(CREATE_TABLE_CHAPTER);
}

Expand Down
22 changes: 21 additions & 1 deletion app/src/main/java/com/example/love/database/StoryDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.example.love.model.Story;
Expand Down Expand Up @@ -52,6 +51,27 @@ public List<Story> getAllStories() {
return allStories;
}

public List<Story> getStoriesBySearch(String keyword) {
List<Story> stories = new Vector<>();
String selectQuery = "SELECT * FROM Story WHERE id = " +
"(SELECT id FROM VirtualStory WHERE VirtualStory MATCH ?)";
String[] keywords = { keyword };
Cursor cursor = database.rawQuery(selectQuery, keywords);
if (cursor.moveToFirst()) {
do {
Story story = new Story(cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getInt(3),
cursor.getBlob(4));
stories.add(story);
} while (cursor.moveToNext());
}
cursor.close();
dbHelper.close();
return stories;
}

public Story getStoryById(String id) {
String selectQuery = "SELECT * FROM Story WHERE " + DatabaseHelper.STORY_ID + " = " + id;
Cursor cursor = database.rawQuery(selectQuery, null);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/ic_next.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<vector android:height="48dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/green_parakeet" android:pathData="M256,0C114.618,0 0,114.618 0,256s114.618,256 256,256s256,-114.618 256,-256S397.382,0 256,0zM256,469.333c-117.818,0 -213.333,-95.515 -213.333,-213.333S138.182,42.667 256,42.667S469.333,138.182 469.333,256S373.818,469.333 256,469.333z"/>
<path android:fillColor="@color/green_parakeet" android:pathData="M228.418,134.248c-8.331,-8.331 -21.839,-8.331 -30.17,0c-8.331,8.331 -8.331,21.839 0,30.17L289.83,256l-91.582,91.582c-8.331,8.331 -8.331,21.839 0,30.17c8.331,8.331 21.839,8.331 30.17,0l106.667,-106.667c8.331,-8.331 8.331,-21.839 0,-30.17L228.418,134.248z"/>
<path android:fillColor="@color/white_nearly" android:pathData="M256,0C114.618,0 0,114.618 0,256s114.618,256 256,256s256,-114.618 256,-256S397.382,0 256,0zM256,469.333c-117.818,0 -213.333,-95.515 -213.333,-213.333S138.182,42.667 256,42.667S469.333,138.182 469.333,256S373.818,469.333 256,469.333z"/>
<path android:fillColor="@color/white_nearly" android:pathData="M228.418,134.248c-8.331,-8.331 -21.839,-8.331 -30.17,0c-8.331,8.331 -8.331,21.839 0,30.17L289.83,256l-91.582,91.582c-8.331,8.331 -8.331,21.839 0,30.17c8.331,8.331 21.839,8.331 30.17,0l106.667,-106.667c8.331,-8.331 8.331,-21.839 0,-30.17L228.418,134.248z"/>
</vector>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/ic_previous.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<vector android:height="48dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/green_parakeet" android:pathData="M256,0C114.618,0 0,114.618 0,256s114.618,256 256,256s256,-114.618 256,-256S397.382,0 256,0zM256,469.333c-117.818,0 -213.333,-95.515 -213.333,-213.333S138.182,42.667 256,42.667S469.333,138.182 469.333,256S373.818,469.333 256,469.333z"/>
<path android:fillColor="@color/green_parakeet" android:pathData="M313.752,134.248c-8.331,-8.331 -21.839,-8.331 -30.17,0L176.915,240.915c-8.331,8.331 -8.331,21.839 0,30.17l106.667,106.667c8.331,8.331 21.839,8.331 30.17,0c8.331,-8.331 8.331,-21.839 0,-30.17L222.17,256l91.582,-91.582C322.083,156.087 322.083,142.58 313.752,134.248z"/>
<path android:fillColor="@color/white_nearly" android:pathData="M256,0C114.618,0 0,114.618 0,256s114.618,256 256,256s256,-114.618 256,-256S397.382,0 256,0zM256,469.333c-117.818,0 -213.333,-95.515 -213.333,-213.333S138.182,42.667 256,42.667S469.333,138.182 469.333,256S373.818,469.333 256,469.333z"/>
<path android:fillColor="@color/white_nearly" android:pathData="M313.752,134.248c-8.331,-8.331 -21.839,-8.331 -30.17,0L176.915,240.915c-8.331,8.331 -8.331,21.839 0,30.17l106.667,106.667c8.331,8.331 21.839,8.331 30.17,0c8.331,-8.331 8.331,-21.839 0,-30.17L222.17,256l91.582,-91.582C322.083,156.087 322.083,142.58 313.752,134.248z"/>
</vector>
Binary file added app/src/main/res/font/ebgaramond_medium.ttf
Binary file not shown.
Binary file added app/src/main/res/font/mali_regular.ttf
Binary file not shown.
Binary file added app/src/main/res/font/roboto_regular.ttf
Binary file not shown.
40 changes: 6 additions & 34 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,50 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
<androidx.appcompat.widget.SearchView
android:id="@+id/sv_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/green_gradient_background"
android:elevation="5dp"
android:orientation="horizontal"
android:weightSum="15">

<EditText
android:id="@+id/et_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="13"
android:layout_marginStart="5dp"
android:background="@drawable/edit_text_background"
android:backgroundTint="@color/white"
android:inputType="text"
android:textCursorDrawable="@null"
android:textSize="17sp"
android:textColor="@color/white_nearly"
android:textColorHint="@color/white_nearly"
android:hint="@string/search_hint"/>

<ImageView
android:id="@+id/iv_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:src="@drawable/ic_search_interface_symbol"
android:foreground="?selectableItemBackground"
android:scaleX="0.5"
android:scaleY="0.5"/>

</LinearLayout>
app:queryHint="@string/search_hint"/>

<GridView
android:id="@+id/gv_stories"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:padding="20dp"
android:gravity="center"
android:horizontalSpacing="20dp"
android:numColumns="2"
Expand Down
62 changes: 34 additions & 28 deletions app/src/main/res/layout/activity_reading_interface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,64 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_chapter_bar"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="7dp"
android:paddingEnd="10dp"
android:paddingBottom="7dp"
android:paddingStart="10dp"
android:background="@drawable/green_gradient_background"
android:gravity="center"
android:textSize="25sp"
android:textColor="@color/white"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tv_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:scrollbars="vertical"
android:justificationMode="inter_word"
android:textSize="20sp"
android:text="@string/administration_area"/>
</ScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
android:elevation="5dp"
android:orientation="horizontal"
android:background="@drawable/green_gradient_background">

<ImageView
android:id="@+id/iv_previous"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:foreground="?selectableItemBackgroundBorderless"
android:clickable="true"
android:src="@drawable/ic_previous"/>

<TextView
android:id="@+id/tv_chapter_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:gravity="center"
android:textSize="25sp"
android:textAllCaps="true"
android:textColor="@color/white"
android:fontFamily="@font/roboto_regular"/>

<ImageView
android:id="@+id/iv_next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:foreground="?selectableItemBackgroundBorderless"
android:clickable="true"
android:src="@drawable/ic_next"/>

</LinearLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tv_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:justificationMode="inter_word"
android:lineSpacingExtra="20sp"
android:textSize="23sp"
android:textColor="@color/black_metal"
android:fontFamily="@font/ebgaramond_medium"/>

</ScrollView>

</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/chapter_item_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
android:layout_marginBottom="10dp"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:textSize="20sp"/>
android:textSize="18sp"/>

</LinearLayout>
16 changes: 11 additions & 5 deletions app/src/main/res/layout/content_scrolling.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:layout_marginStart="20dp"
android:textSize="20sp"/>
android:textSize="18sp"/>

<TextView
android:id="@+id/tv_number_chapters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:layout_marginBottom="15dp"
android:layout_marginStart="20dp"
android:textSize="20sp"/>
android:textSize="18sp"/>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:background="@color/gray_flint"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_chapters"
Expand All @@ -42,8 +49,7 @@
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="20dp"
android:layout_marginStart="10dp"
android:divider="@color/transparent"/>
android:layout_marginStart="10dp"/>
</LinearLayout>

</androidx.core.widget.NestedScrollView>
5 changes: 3 additions & 2 deletions app/src/main/res/layout/profile_item_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
android:id="@+id/iv_avt"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerInside"
android:scaleType="fitCenter"
android:contentDescription="@string/avatar"/>

<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="7dp"
android:textSize="18sp"
android:textAlignment="center"/>

</LinearLayout>
Loading

0 comments on commit 8bead9d

Please sign in to comment.