preserve translation changes in memory. Next: write to DB.

This commit is contained in:
Eric House 2014-04-05 13:33:12 -07:00
parent 7dfa042b77
commit 527685da79
2 changed files with 61 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -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 HashMap<String, String>s_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<String, String>();
}
}
}