From 527685da79133ad42f65f5b3fb75a92e737505bc Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 5 Apr 2014 13:33:12 -0700 Subject: [PATCH] preserve translation changes in memory. Next: write to DB. --- .../eehouse/android/xw4/loc/LocListItem.java | 39 ++++++++++++++++++- .../org/eehouse/android/xw4/loc/LocUtils.java | 23 +++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) 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 0fe3bcd6f..9d2adb4fa 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 @@ -24,16 +24,23 @@ import android.widget.LinearLayout; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; +import android.widget.EditText; +import android.view.View; +import android.view.View.OnFocusChangeListener; + +import junit.framework.Assert; import org.eehouse.android.xw4.R; import org.eehouse.android.xw4.Utils; import org.eehouse.android.xw4.DbgUtils; -public class LocListItem extends LinearLayout { +public class LocListItem extends LinearLayout implements OnFocusChangeListener { private Context m_context; private String m_key; private int m_position; + private EditText m_edit; + private String m_xlation; public LocListItem( Context cx, AttributeSet as ) { @@ -41,6 +48,14 @@ public class LocListItem extends LinearLayout { m_context = cx; } + @Override + protected void onFinishInflate() + { + super.onFinishInflate(); + m_edit = (EditText)findViewById( R.id.xlated_view ); + m_edit.setOnFocusChangeListener( this ); + } + private void setEnglish() { int id = LocIDs.get( m_key ); @@ -50,6 +65,26 @@ public class LocListItem extends LinearLayout { DbgUtils.logf( "setEnglish: set to %s", str ); } + private void setXlated() + { + m_xlation = LocUtils.getXlation( m_context, m_key ); + if ( null != m_xlation ) { + m_edit.setText( m_xlation ); + } + } + + public void onFocusChange( View view, boolean hasFocus ) + { + Assert.assertTrue( view == m_edit ); + if ( !hasFocus ) { + 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() ); + } + } + } + protected static LocListItem create( Context context, String key, int position ) { @@ -57,7 +92,9 @@ public class LocListItem extends LinearLayout { (LocListItem)Utils.inflate( context, R.layout.loc_list_item ); result.m_key = key; result.m_position = position; + result.setEnglish(); + result.setXlated(); return result; } } 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 9fc693298..80661a0ad 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 @@ -27,6 +27,8 @@ import android.view.Menu; import android.view.MenuItem.OnMenuItemClickListener; import android.view.MenuItem; +import java.util.HashMap; + import junit.framework.Assert; import org.eehouse.android.xw4.R; @@ -36,6 +38,7 @@ public class LocUtils { // Keep this in sync with gen_loc_ids.py and what's used in the menu.xml // files to mark me-localized strings. private static final String LOC_PREFIX = "loc:"; + private static HashMaps_xlations = null; public interface LocIface { void setText( CharSequence text ); @@ -99,6 +102,19 @@ public class LocUtils { return str.toUpperCase(); } + public static void setXlation( Context context, String key, String txt ) + { + loadXlations( context ); + s_xlations.put( key, txt ); + } + + public static String getXlation( Context context, String key ) + { + loadXlations( context ); + String result = s_xlations.get( key ); + return result; + } + private static void xlateMenu( final Activity activity, Menu menu, int depth ) { @@ -142,4 +158,11 @@ public class LocUtils { } } + private static void loadXlations( Context context ) + { + if ( null == s_xlations ) { + s_xlations = new HashMap(); + } + } + }