Skip to content

Commit

Permalink
make internal user dictionary fragment work (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 committed Nov 28, 2023
1 parent bd3e73e commit c43e3ef
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

package org.dslul.openboard.inputmethod.latin.userdictionary;

import android.app.Fragment;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -19,6 +17,12 @@
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import org.dslul.openboard.inputmethod.latin.R;
import org.dslul.openboard.inputmethod.latin.userdictionary.UserDictionaryAddWordContents.LocaleRenderer;
import org.dslul.openboard.inputmethod.latin.userdictionary.UserDictionaryLocalePicker.LocationChangedListener;
Expand Down Expand Up @@ -47,12 +51,12 @@ public class UserDictionaryAddWordFragment extends Fragment
private boolean mIsDeleting = false;

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
getActivity().getActionBar().setTitle(R.string.edit_personal_dictionary);
// Keep the instance so that we remember mContents when configuration changes (eg rotation)
setRetainInstance(true);
final ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (actionBar == null) return;
actionBar.setTitle(R.string.edit_personal_dictionary);
}

@Override
Expand All @@ -70,11 +74,8 @@ public View onCreateView(final LayoutInflater inflater, final ViewGroup containe
// it. That means in particular if the word undergoes any change, the old version should
// be updated, so the mContents object needs to switch to EDIT mode if it was in
// INSERT mode.
mContents = new UserDictionaryAddWordContents(mRootView,
mContents /* oldInstanceToBeEdited */);
mContents = new UserDictionaryAddWordContents(mRootView, mContents);
}
getActivity().getActionBar().setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName(
getActivity(), mContents.getCurrentUserDictionaryLocale()));
return mRootView;
}

Expand All @@ -100,13 +101,13 @@ public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == OPTIONS_MENU_ADD) {
// added the entry in "onPause"
getActivity().onBackPressed();
requireActivity().onBackPressed();
return true;
}
if (item.getItemId() == OPTIONS_MENU_DELETE) {
mContents.delete(getActivity());
mIsDeleting = true;
getActivity().onBackPressed();
requireActivity().onBackPressed();
return true;
}
return false;
Expand All @@ -119,13 +120,29 @@ public void onResume() {
updateSpinner();
}

@Override
public void onStart() {
super.onStart();
final ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (actionBar == null) return;
actionBar.setTitle(R.string.user_dict_settings_add_dialog_title);
actionBar.setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName(getActivity(), mContents.getCurrentUserDictionaryLocale()));
}

@Override
public void onStop() {
super.onStop();
final ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (actionBar == null) return;
actionBar.setSubtitle(null);
}

private void updateSpinner() {
final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity());

final Spinner localeSpinner =
mRootView.findViewById(R.id.user_dictionary_add_locale);
final Spinner localeSpinner = mRootView.findViewById(R.id.user_dictionary_add_locale);
final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<>(
getActivity(), android.R.layout.simple_spinner_item, localesList);
requireActivity(), android.R.layout.simple_spinner_item, localesList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localeSpinner.setAdapter(adapter);
localeSpinner.setOnItemSelectedListener(this);
Expand All @@ -145,8 +162,11 @@ public void onItemSelected(final AdapterView<?> parent, final View view, final i
final long id) {
final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
if (locale.isMoreLanguages()) {
PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
preferenceActivity.startPreferenceFragment(new UserDictionaryLocalePicker(), true);
AppCompatActivity activity = (AppCompatActivity) requireActivity();
activity.getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new UserDictionaryLocalePicker())
.addToBackStack(null)
.commit();
} else {
mContents.updateLocale(locale.getLocaleString());
}
Expand All @@ -156,14 +176,15 @@ public void onItemSelected(final AdapterView<?> parent, final View view, final i
public void onNothingSelected(final AdapterView<?> parent) {
// I'm not sure we can come here, but if we do, that's the right thing to do.
final Bundle args = getArguments();
if (args == null) return;
mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE));
}

// Called by the locale picker
@Override
public void onLocaleSelected(final Locale locale) {
mContents.updateLocale(locale.toString());
getActivity().onBackPressed();
requireActivity().onBackPressed();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package org.dslul.openboard.inputmethod.latin.userdictionary;

import android.app.Fragment;
import androidx.fragment.app.Fragment;

import java.util.Locale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

package org.dslul.openboard.inputmethod.latin.userdictionary;

import android.app.ListFragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -27,6 +26,12 @@
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.ListFragment;

import org.dslul.openboard.inputmethod.latin.R;

import java.util.Locale;
Expand All @@ -37,16 +42,15 @@

public class UserDictionarySettings extends ListFragment {

public static final boolean IS_SHORTCUT_API_SUPPORTED =
true;
public static final boolean IS_SHORTCUT_API_SUPPORTED = true;

private static final String[] QUERY_PROJECTION_SHORTCUT_UNSUPPORTED =
{ UserDictionary.Words._ID, UserDictionary.Words.WORD};
private static final String[] QUERY_PROJECTION_SHORTCUT_SUPPORTED =
{ UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT};
private static final String[] QUERY_PROJECTION =
IS_SHORTCUT_API_SUPPORTED ?
QUERY_PROJECTION_SHORTCUT_SUPPORTED : QUERY_PROJECTION_SHORTCUT_UNSUPPORTED;
private static final String[] QUERY_PROJECTION = IS_SHORTCUT_API_SUPPORTED
? QUERY_PROJECTION_SHORTCUT_SUPPORTED
: QUERY_PROJECTION_SHORTCUT_UNSUPPORTED;

// The index of the shortcut in the above array.
private static final int INDEX_SHORTCUT = 2;
Expand Down Expand Up @@ -75,10 +79,8 @@ public class UserDictionarySettings extends ListFragment {

// Either the locale is empty (means the word is applicable to all locales)
// or the word equals our current locale
private static final String QUERY_SELECTION =
UserDictionary.Words.LOCALE + "=?";
private static final String QUERY_SELECTION_ALL_LOCALES =
UserDictionary.Words.LOCALE + " is null";
private static final String QUERY_SELECTION = UserDictionary.Words.LOCALE + "=?";
private static final String QUERY_SELECTION_ALL_LOCALES = UserDictionary.Words.LOCALE + " is null";

private static final String DELETE_SELECTION_WITH_SHORTCUT = UserDictionary.Words.WORD
+ "=? AND " + UserDictionary.Words.SHORTCUT + "=?";
Expand All @@ -97,27 +99,25 @@ public class UserDictionarySettings extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getActionBar().setTitle(R.string.edit_personal_dictionary);
final ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (actionBar == null) return;
actionBar.setTitle(R.string.edit_personal_dictionary);
}

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(
R.layout.user_dictionary_preference_list_fragment, container, false);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.user_dictionary_preference_list_fragment, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

final Intent intent = getActivity().getIntent();
final String localeFromIntent =
null == intent ? null : intent.getStringExtra("locale");
final Intent intent = requireActivity().getIntent();
final String localeFromIntent = null == intent ? null : intent.getStringExtra("locale");

final Bundle arguments = getArguments();
final String localeFromArguments =
null == arguments ? null : arguments.getString("locale");
final String localeFromArguments = null == arguments ? null : arguments.getString("locale");

final String locale;
if (null != localeFromArguments) {
Expand All @@ -131,7 +131,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
// closing the cursor, so take care when resolving this TODO). We should either use a
// regular query and close the cursor, or switch to a LoaderManager and a CursorLoader.
mCursor = createCursor(locale);
TextView emptyView = getView().findViewById(android.R.id.empty);
TextView emptyView = view.findViewById(android.R.id.empty);
emptyView.setText(R.string.user_dict_settings_empty_text);

final ListView listView = getListView();
Expand All @@ -140,20 +140,34 @@ public void onActivityCreated(Bundle savedInstanceState) {
listView.setEmptyView(emptyView);

setHasOptionsMenu(true);
}

@Override
public void onStart() {
super.onStart();
// Show the language as a subtitle of the action bar
getActivity().getActionBar().setSubtitle(
UserDictionarySettingsUtils.getLocaleDisplayName(getActivity(), mLocale));
final ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (actionBar == null) return;
actionBar.setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName(getActivity(), mLocale));
}

@Override
public void onStop() {
super.onStop();
// clear the subtitle
final ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (actionBar == null) return;
actionBar.setSubtitle(null);
}

@Override
public void onResume() {
super.onResume();
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof MyAdapter) {
if (adapter instanceof MyAdapter listAdapter) {
// The list view is forced refreshed here. This allows the changes done
// in UserDictionaryAddWordFragment (update/delete/insert) to be seen when
// user goes back to this view.
MyAdapter listAdapter = (MyAdapter) adapter;
listAdapter.notifyDataSetChanged();
}
}
Expand All @@ -172,12 +186,12 @@ private Cursor createCursor(final String locale) {
// can be guaranteed not to match locales that may exist.
if ("".equals(locale)) {
// Case-insensitive sort
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
return requireActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION_ALL_LOCALES, null,
"UPPER(" + UserDictionary.Words.WORD + ")");
}
final String queryLocale = null != locale ? locale : Locale.getDefault().toString();
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
return requireActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION, new String[] { queryLocale },
"UPPER(" + UserDictionary.Words.WORD + ")");
}
Expand Down Expand Up @@ -207,11 +221,9 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
return;
}
}
MenuItem actionItem =
menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title)
MenuItem actionItem = menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title)
.setIcon(R.drawable.ic_plus);
actionItem.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}

@Override
Expand All @@ -236,10 +248,11 @@ private void showAddOrEditDialog(final String editingWord, final String editingS
args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord);
args.putString(UserDictionaryAddWordContents.EXTRA_SHORTCUT, editingShortcut);
args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale);
android.preference.PreferenceActivity pa =
(android.preference.PreferenceActivity)getActivity();
pa.startPreferencePanel(UserDictionaryAddWordFragment.class.getName(),
args, R.string.user_dict_settings_add_dialog_title, null, null, 0);
AppCompatActivity activity = (AppCompatActivity) requireActivity();
activity.getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, UserDictionaryAddWordFragment.class, args)
.addToBackStack(null)
.commit();
}

private String getWord(final int position) {
Expand Down

0 comments on commit c43e3ef

Please sign in to comment.