Skip to content

Commit

Permalink
Implement search equal and search contains
Browse files Browse the repository at this point in the history
  • Loading branch information
shiami committed Jan 26, 2017
1 parent 125bb8d commit 7a444c7
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.taccotap.taigidict.R;
import com.taccotap.taigidict.tailo.word.TailoWordActivity;
Expand All @@ -17,7 +21,7 @@
import io.reactivex.functions.Consumer;
import io.realm.Realm;

public class TailoSearchActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
public class TailoSearchActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, CompoundButton.OnCheckedChangeListener {

public static final String ACTION_SEARCH_LMJ = "ACTION_SEARCH_LMJ";
public static final String ACTION_SEARCH_HOAGI = "ACTION_SEARCH_HOAGI";
Expand All @@ -33,10 +37,22 @@ public class TailoSearchActivity extends AppCompatActivity implements SearchView
@BindView(R.id.recyclerView)
RecyclerView mRecyclerView;

@BindView(R.id.searchRangeRadioGroup)
RadioGroup mSearchRangeRadioGroup;

@BindView(R.id.searchContainsRadioButton)
RadioButton mSearchContainsRadioButton;

@BindView(R.id.searchEqualRadioButton)
RadioButton mSearchEqualRadioButton;

private TailoSearchAdapter mTailoSearchAdapter;
private Realm mRealm;
private int mCurrentSearchType = SEARCH_TYPE_LOMAJI;

private String mCurrentQueryString = "Tâi-gí";
private boolean mIsCurrentSearchEquals = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -46,29 +62,24 @@ protected void onCreate(Bundle savedInstanceState) {
handleIntent();

mRealm = Realm.getDefaultInstance();
// initActionBar();
initRecyclerView();
initRadioButton();
initSearch();
}

// private void initActionBar() {
// final ActionBar supportActionBar = getSupportActionBar();
// if (supportActionBar != null) {
// supportActionBar.setDisplayShowHomeEnabled(true);
// supportActionBar.setDisplayHomeAsUpEnabled(true);
// }
// }

private void handleIntent() {
// Get the intent, verify the action and get the query
Intent intent = getIntent();

if (ACTION_SEARCH_LMJ.equals(intent.getAction())) {
mCurrentSearchType = 0;
setTitle(getString(R.string.nav_dict_tailo) + " (" + getString(R.string.fragment_tailo_search_button_lomaji) + ")");
} else if (ACTION_SEARCH_HOAGI.equals(intent.getAction())) {
mCurrentSearchType = 1;
setTitle(getString(R.string.nav_dict_tailo) + " (" + getString(R.string.fragment_tailo_search_button_hoagi) + ")");
} else if (ACTION_SEARCH_ALL.equals(intent.getAction())) {
mCurrentSearchType = 2;
setTitle(getString(R.string.nav_dict_tailo) + " (" + getString(R.string.fragment_tailo_search_button_all) + ")");
}
}

Expand All @@ -86,32 +97,50 @@ public void accept(Integer integer) throws Exception {
});
}

private void initRadioButton() {
mSearchContainsRadioButton.setOnCheckedChangeListener(this);
mSearchEqualRadioButton.setOnCheckedChangeListener(this);
}

private void initSearch() {
mSearchView.setIconifiedByDefault(false);
mSearchView.requestFocus();

if (mCurrentSearchType == SEARCH_TYPE_LOMAJI) {
mSearchView.setQueryHint(getString(R.string.search_lomaji_hint));
mSearchRangeRadioGroup.setVisibility(View.VISIBLE);
} else if (mCurrentSearchType == SEARCH_TYPE_HOAGI) {
mSearchView.setQueryHint(getString(R.string.search_hoagi_hint));
mSearchRangeRadioGroup.setVisibility(View.VISIBLE);
} else if (mCurrentSearchType == SEARCH_TYPE_ALL) {
mSearchView.setQueryHint(getString(R.string.search_all_hint));
mSearchRangeRadioGroup.setVisibility(View.GONE);
}

mSearchView.setOnQueryTextListener(this);
}

private void doSearch() {
doSearch(mCurrentQueryString, mIsCurrentSearchEquals);
}

private void doSearch(String query) {
doSearch(query, mIsCurrentSearchEquals);
}

private void doSearch(String query, boolean isSearchEquals) {
mCurrentQueryString = query;

query = query.trim();

if (TextUtils.isEmpty(query)) {
query = "Tâi-gí";
}

if (mCurrentSearchType == SEARCH_TYPE_LOMAJI) {
mTailoSearchAdapter.searchLomaji(query);
mTailoSearchAdapter.searchLomaji(query, isSearchEquals);
} else if (mCurrentSearchType == SEARCH_TYPE_HOAGI) {
mTailoSearchAdapter.searchHoagi(query);
mTailoSearchAdapter.searchHoagi(query, isSearchEquals);
} else if (mCurrentSearchType == SEARCH_TYPE_ALL) {
mTailoSearchAdapter.searchAll(query);
}
Expand All @@ -138,4 +167,23 @@ public boolean onQueryTextChange(String newText) {
doSearch(newText);
return true;
}

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (compoundButton == mSearchContainsRadioButton && checked) {
mSearchContainsRadioButton.setChecked(true);
mSearchEqualRadioButton.setChecked(false);

mIsCurrentSearchEquals = false;

doSearch();
} else if (compoundButton == mSearchEqualRadioButton && checked) {
mSearchContainsRadioButton.setChecked(false);
mSearchEqualRadioButton.setChecked(true);

mIsCurrentSearchEquals = true;

doSearch();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ public TailoSearchViewHolder(ListitemTailoSearchBinding dataBinding) {
}
}

public void searchLomaji(String lomaji) {
public void searchLomaji(String lomaji, boolean isSearchEquals) {
final RealmResults<TlTaigiWord> realmResults;
RealmQuery<TlTaigiWord> where = mRealm.where(TlTaigiWord.class).contains("lomaji", lomaji, Case.INSENSITIVE);

RealmQuery<TlTaigiWord> where = mRealm.where(TlTaigiWord.class);
if (!isSearchEquals) {
where = where.contains("lomaji", lomaji, Case.INSENSITIVE);
} else {
where = where.equalTo("lomaji", lomaji, Case.INSENSITIVE);
}

realmResults = where.findAllSortedAsync("lomaji", Sort.ASCENDING);

Expand All @@ -83,9 +89,14 @@ public void onChange(RealmResults<TlTaigiWord> element) {
});
}

public void searchHoagi(String hoagi) {
public void searchHoagi(String hoagi, boolean isSearchEquals) {
final RealmResults<TlTaigiWord> realmResults;
RealmQuery<TlTaigiWord> where = mRealm.where(TlTaigiWord.class).contains("hoagiWords.hoagiWord", hoagi);
RealmQuery<TlTaigiWord> where = mRealm.where(TlTaigiWord.class);
if (!isSearchEquals) {
where = where.contains("hoagiWords.hoagiWord", hoagi);
} else {
where = where.equalTo("hoagiWords.hoagiWord", hoagi);
}

realmResults = where.findAllSortedAsync("mainCode", Sort.ASCENDING);

Expand All @@ -99,7 +110,9 @@ public void onChange(RealmResults<TlTaigiWord> element) {

public void searchAll(String query) {
final RealmResults<TlTaigiWord> realmResults;
RealmQuery<TlTaigiWord> where = mRealm.where(TlTaigiWord.class).contains("lomaji", query, Case.INSENSITIVE)
RealmQuery<TlTaigiWord> where = mRealm.where(TlTaigiWord.class);

where = where.contains("lomaji", query, Case.INSENSITIVE)
.or().contains("descriptions.description", query)
.or().contains("descriptions.exampleSentences.exampleSentenceHanji", query)
.or().contains("descriptions.exampleSentences.exampleSentenceLomaji", query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import com.taccotap.taigidictmodel.tailo.TlExampleSentence;
import com.taccotap.taigidictmodel.tailo.TlTaigiWord;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
Expand Down Expand Up @@ -93,6 +91,11 @@ public void onChange(RealmModel element) {
// bind word
mBinding.setTaigiWord(taigiWord);

// check voice avalibility (附錄皆無語音檔)
if (taigiWord.getWordPropertyCode() >= 11 && taigiWord.getWordPropertyCode() <= 22) {
mFloatingActionButton.setVisibility(View.GONE);
}

// bind desc
final RealmList<TlDescription> descriptions = taigiWord.getDescriptions();
final int descriptionsCount = descriptions.size();
Expand Down Expand Up @@ -165,17 +168,12 @@ void onClickFab() {
Toast.makeText(TailoWordActivity.this, R.string.toast_voice_need_network_connection, Toast.LENGTH_SHORT).show();
return;
}
String voiceUrl = TailoTaigiWordAudioUrlHelper.getTaigiAudioUrl(mMainCode);

if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();

mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mMediaPlayer.setDataSource(this, Uri.parse(TailoTaigiWordAudioUrlHelper.getTaigiAudioUrl(mMainCode)));
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}

mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
Expand All @@ -186,16 +184,39 @@ public void onCompletion(MediaPlayer mediaPlayer) {
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
mFloatingActionButton.setImageResource(R.drawable.ic_volume_up_grey_300_36dp);
Toast.makeText(TailoWordActivity.this, R.string.toast_sorry_cant_play, Toast.LENGTH_SHORT).show();
toastCantPlayVoiceMessage();
return false;
}
});
}
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
startPlayVoice();
}
});

try {
mMediaPlayer.setDataSource(this, Uri.parse(voiceUrl));
mMediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
toastCantPlayVoiceMessage();
}
} else {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.seekTo(0);
}

if (mMediaPlayer.isPlaying()) {
mMediaPlayer.seekTo(0);
startPlayVoice();
}
}

private void toastCantPlayVoiceMessage() {
mFloatingActionButton.setImageResource(R.drawable.ic_volume_up_grey_300_36dp);
Toast.makeText(TailoWordActivity.this, R.string.toast_sorry_cant_play, Toast.LENGTH_SHORT).show();
}

private void startPlayVoice() {
mFloatingActionButton.setImageResource(R.drawable.ic_volume_up_light_green_a100_36dp);
mMediaPlayer.start();
}
Expand Down
47 changes: 46 additions & 1 deletion TaigiDict/app/src/main/res/layout/activity_tailo_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,55 @@
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#eee"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/listitem_tailo_search"/>

<RadioGroup
android:id="@+id/searchRangeRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#fddd"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<RadioButton
android:id="@+id/searchContainsRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/tailo_search_contains_radiobutton_text"
android:textColor="@color/colorAccent"
android:textSize="19sp"/>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<RadioButton
android:id="@+id/searchEqualRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/tailo_search_equals_radiobutton_text"
android:textColor="@color/colorAccent"
android:textSize="19sp"/>
</LinearLayout>

</RadioGroup>

</LinearLayout>
9 changes: 5 additions & 4 deletions TaigiDict/app/src/main/res/layout/fragment_tailo_dict.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@
android:gravity="center_horizontal"
android:text="@string/fragment_tailo_search_method"
android:textColor="#f666"
android:textSize="28sp"/>
android:textSize="24sp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:orientation="vertical">

<Button
android:id="@+id/button_search_lmj"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:backgroundTint="@color/colorAccent"
android:text="@string/fragment_tailo_search_button_lomaji"
Expand All @@ -65,7 +66,7 @@
android:id="@+id/button_search_hoagi"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:backgroundTint="@color/colorAccent"
android:text="@string/fragment_tailo_search_button_hoagi"
Expand All @@ -77,7 +78,7 @@
android:id="@+id/button_search_all"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:backgroundTint="@color/colorAccent"
android:text="@string/fragment_tailo_search_button_all"
Expand Down
5 changes: 4 additions & 1 deletion TaigiDict/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
<string name="search_hoagi_hint">請輸入對應ê華語漢字來揣......</string>
<string name="search_all_hint">請輸入全文關鍵字來揣......</string>

<string name="tailo_search_contains_radiobutton_text">揣差不多ê</string>
<string name="tailo_search_equals_radiobutton_text">揣攏相仝ê</string>

<string name="toast_voice_need_network_connection">語音ê功能需要上網才會使聽。</string>
<string name="toast_sorry_cant_play">真歹勢,這馬無法度放。</string>
<string name="toast_sorry_cant_play">真歹勢,這無法度聽。</string>

<string name="navigation_drawer_open">navigation drawer open</string>
<string name="navigation_drawer_close">navigation drawer close</string>
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 7a444c7

Please sign in to comment.