From a351b2a5b985b7fce6ba767d9565721f32178094 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 6 Apr 2014 06:43:48 -0700 Subject: [PATCH] implement search through loc dialog --- .../eehouse/android/xw4/loc/LocDelegate.java | 44 ++++++++- .../org/eehouse/android/xw4/loc/LocIDs.java | 12 +-- .../android/xw4/loc/LocListAdapter.java | 11 ++- .../eehouse/android/xw4/loc/LocListItem.java | 15 +-- .../eehouse/android/xw4/loc/LocSearcher.java | 92 +++++++++++++++++++ .../org/eehouse/android/xw4/loc/LocUtils.java | 22 ++++- 6 files changed, 172 insertions(+), 24 deletions(-) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java index 62cec8a36..e366fbb40 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java @@ -22,15 +22,24 @@ package org.eehouse.android.xw4.loc; import android.app.ListActivity; import android.os.Bundle; import android.widget.ListView; +import android.view.View; +import android.widget.EditText; +import android.widget.ImageButton; +import android.widget.LinearLayout; +import org.eehouse.android.xw4.DbgUtils; import org.eehouse.android.xw4.DelegateBase; import org.eehouse.android.xw4.R; +import org.eehouse.android.xw4.Utils; - -public class LocDelegate extends DelegateBase { +public class LocDelegate extends DelegateBase implements View.OnClickListener { private ListActivity m_activity; private LocListAdapter m_adapter; + private EditText m_searchField; + private ImageButton m_searchButton; + private LocSearcher m_searcher; + private String m_curSearch; protected LocDelegate( ListActivity activity, Bundle savedInstanceState ) { @@ -45,13 +54,38 @@ public class LocDelegate extends DelegateBase { return false; } + @Override + public void onClick( View view ) + { + String newText = m_searchField.getText().toString(); + if ( null == m_curSearch || ! m_curSearch.equals( newText ) ) { + m_curSearch = newText; + m_searcher.start( newText ); // synchronous for now + makeNewAdapter(); + } + } + + private void makeNewAdapter() + { + ListView listview = m_activity.getListView(); + m_adapter = new LocListAdapter( m_activity, listview, m_searcher ); + m_activity.setListAdapter( m_adapter ); + } + private void init( Bundle savedInstanceState ) { m_activity.setContentView( R.layout.loc_main ); - ListView listview = m_activity.getListView(); - m_adapter = new LocListAdapter( m_activity, listview ); - m_activity.setListAdapter( m_adapter ); + View root = Utils.getContentView( m_activity ); + m_searchButton = (ImageButton)root.findViewById( R.id.loc_search_button ); + m_searchButton.setOnClickListener( this ); + + m_searchField = (EditText)root.findViewById( R.id.loc_search_field ); + + LocSearcher.Pair[] pairs = LocUtils.makePairs( m_activity ); + m_searcher = new LocSearcher( pairs ); + + makeNewAdapter(); } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocIDs.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocIDs.java index 8398ecf2a..864c5a3ac 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocIDs.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocIDs.java @@ -42,17 +42,15 @@ public class LocIDs extends LocIDsData { protected static int getID( String key ) { - return LocIDsData.s_map.get( key ); + int result = LocIDsData.NOT_FOUND; + if ( null != key ) { + result = LocIDsData.s_map.get( key ); + } + return result; } protected static int size() { return LocIDsData.s_map.size(); } - - protected static int get( String key ) - { - return LocIDsData.s_map.get( key ); - } - } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListAdapter.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListAdapter.java index 8270fa497..9a81bd793 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListAdapter.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListAdapter.java @@ -32,17 +32,20 @@ public class LocListAdapter extends XWListAdapter { private Context m_context; private ListView m_listView; + private LocSearcher m_searcher; - protected LocListAdapter( Context context, ListView listView ) + protected LocListAdapter( Context context, ListView listView, + LocSearcher searcher ) { m_context = context; m_listView = listView; + m_searcher = searcher; } @Override public int getCount() { - int count = LocIDs.size(); + int count = m_searcher.matchSize(); DbgUtils.logf(" LocListAdapter.getCount() => %d", count ); return count; } @@ -50,7 +53,7 @@ public class LocListAdapter extends XWListAdapter { public View getView( int position, View convertView, ViewGroup parent ) { DbgUtils.logf( "LocListAdapter.getView(position=%d)", position ); - String key = LocIDs.getNthKey( position ); - return LocListItem.create( m_context, key, position ); + LocSearcher.Pair pair = m_searcher.getNthMatch( position ); + return LocListItem.create( m_context, pair, position ); } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListItem.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListItem.java index 9d2adb4fa..b18bfc726 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListItem.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocListItem.java @@ -37,7 +37,7 @@ import org.eehouse.android.xw4.DbgUtils; public class LocListItem extends LinearLayout implements OnFocusChangeListener { private Context m_context; - private String m_key; + private LocSearcher.Pair m_pair; private int m_position; private EditText m_edit; private String m_xlation; @@ -58,7 +58,7 @@ public class LocListItem extends LinearLayout implements OnFocusChangeListener { private void setEnglish() { - int id = LocIDs.get( m_key ); + int id = LocIDs.getID( m_pair.getKey() ); String str = m_context.getString( id ); TextView tv = (TextView)findViewById( R.id.english_view ); tv.setText( str ); @@ -67,8 +67,9 @@ public class LocListItem extends LinearLayout implements OnFocusChangeListener { private void setXlated() { - m_xlation = LocUtils.getXlation( m_context, m_key ); + m_xlation = LocUtils.getXlation( m_context, m_pair.getKey() ); if ( null != m_xlation ) { + m_pair.setXlation( m_xlation ); m_edit.setText( m_xlation ); } } @@ -80,17 +81,19 @@ public class LocListItem extends LinearLayout implements OnFocusChangeListener { CharSequence txt = m_edit.getText(); DbgUtils.logf( "view with text %s lost focus", txt ); if ( ! txt.equals( m_xlation ) ) { - LocUtils.setXlation( m_context, m_key, txt.toString() ); + LocUtils.setXlation( m_context, m_pair.getKey(), + txt.toString() ); } } } - protected static LocListItem create( Context context, String key, + protected static LocListItem create( Context context, + LocSearcher.Pair pair, int position ) { LocListItem result = (LocListItem)Utils.inflate( context, R.layout.loc_list_item ); - result.m_key = key; + result.m_pair = pair; result.m_position = position; result.setEnglish(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java new file mode 100644 index 000000000..7b7ba1463 --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java @@ -0,0 +1,92 @@ +/* -*- compile-command: "find-and-ant.sh debug install"; -*- */ +/* + * Copyright 2014 by Eric House (xwords@eehouse.org). All rights + * reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.eehouse.android.xw4.loc; + +import java.util.ArrayList; + +import org.eehouse.android.xw4.DbgUtils; + +public class LocSearcher { + + public static class Pair { + public Pair( String key, String eng, String xlation ) { + m_key = key; + m_english = eng; + m_xlation = xlation; + } + + public boolean matches( String term ) + { + return m_english.contains( term ) || + (null != m_xlation && m_xlation.contains( term ) ); + } + + public void setXlation( String xlation ) + { + m_xlation = xlation; + } + + public String getKey() { return m_key; } + + private String m_key; + private String m_english; + private String m_xlation; + } + + private Pair[] m_pairs; + private Pair[] m_matchingPairs; + private String m_lastTerm; + + public LocSearcher( Pair pairs[] ) + { + m_pairs = m_matchingPairs = pairs; + } + + protected void start( String term ) + { + if ( 0 == term.length() ) { + m_matchingPairs = m_pairs; + } else { + Pair[] usePairs = null != m_lastTerm && term.contains(m_lastTerm) + ? m_matchingPairs : m_pairs; + DbgUtils.logf( "start: searching %d pairs", usePairs.length ); + ArrayList matchers = new ArrayList(); + for ( Pair pair : usePairs ) { + if ( pair.matches( term ) ) { + matchers.add( pair ); + } + } + m_matchingPairs = matchers.toArray( new Pair[matchers.size()] ); + } + m_lastTerm = term; + } + + protected Pair getNthMatch( int nn ) + { + return m_matchingPairs[nn]; + } + + protected int matchSize() + { + return m_matchingPairs.length; + } + +} diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java index 293df411a..e6421a063 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java @@ -28,6 +28,7 @@ import android.view.MenuItem.OnMenuItemClickListener; import android.view.MenuItem; import java.util.HashMap; +import java.util.Iterator; import junit.framework.Assert; @@ -121,17 +122,34 @@ public class LocUtils { DBUtils.saveXlations( context, "te_ST", s_xlations ); } + + protected static LocSearcher.Pair[] makePairs( Context context ) + { + loadXlations( context ); + + HashMap map = LocIDsData.s_map; + int siz = map.size(); + LocSearcher.Pair[] result = new LocSearcher.Pair[siz]; + Iterator iter = map.keySet().iterator(); + + for ( int ii = 0; iter.hasNext(); ++ii ) { + String key = iter.next(); + String english = context.getString( map.get( key ) ); + String xlation = s_xlations.get( key ); + result[ii] = new LocSearcher.Pair( key, english, xlation ); + } + return result; + } + private static void xlateMenu( final Activity activity, Menu menu, int depth ) { int count = menu.size(); - DbgUtils.logf( "xlateMenu: menu has %d items", count ); for ( int ii = 0; ii < count; ++ii ) { MenuItem item = menu.getItem( ii ); CharSequence ts = item.getTitle(); if ( null != ts ) { String title = ts.toString(); - DbgUtils.logf( "xlateMenu: %d; %s", ii, title ); if ( title.startsWith( LOC_PREFIX ) ) { String asKey = title.substring( LOC_PREFIX.length() ); int id = LocIDs.getID( asKey );