Search in sources :

Example 1 with DeckOptions

use of com.ichi2.anki.DeckOptions in project AnkiChinaAndroid by ankichinateam.

the class StudyOptionsFragment method configMaxDayLearnAndRev.

private void configMaxDayLearnAndRev() {
    CustomStyleDialog customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("设置每天学习量").setCustomLayout(R.layout.dialog_common_custom_next).setMultiEditorModeCallback(new CustomStyleDialog.Builder.MultiEditorModeCallback() {

        @Override
        public String[] getEditorText() {
            // return new String[] {mOptions.getJSONObject("new").getString("perDay"), mOptions.getJSONObject("rev").getString("perDay"),};
            return new String[] { "30", "200" };
        }

        @Override
        public String[] getEditorHint() {
            return new String[] { "每天新卡上限", "每天复习上限" };
        }

        @Override
        public String[] getItemHint() {
            return new String[] { "学习一段时间后,未来每天新卡+复习卡约180张,大约需30分钟", "建议设置为最大9999,有多少复习多少" };
        }
    }).addSingleTextChangedListener(new CustomStyleDialog.Builder.MyTextWatcher() {

        @Override
        public void beforeTextChanged(Dialog dialog, CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(Dialog dialog, CharSequence s, int start, int before, int count) {
            if (s.toString().isEmpty()) {
                ((CustomStyleDialog) dialog).getSingleEditorModeHintView().setText("未来每天学习量=新卡数x6");
            } else {
                int num = 0;
                try {
                    num = Integer.parseInt(s.toString()) * 6;
                } catch (Exception ignored) {
                }
                int time = num * 10 / 60;
                ((CustomStyleDialog) dialog).getSingleEditorModeHintView().setText(String.format("学习一段时间后,未来每天新卡+复习卡约%s张,大约需%s分钟", num, time));
            }
        }

        @Override
        public void afterTextChanged(Dialog dialog, Editable s) {
        }
    }).setPositiveButton("进入学习", (dialog, which) -> {
        String text1 = ((CustomStyleDialog) dialog).getMultiEditor().get(0).getText().toString();
        String text2 = ((CustomStyleDialog) dialog).getMultiEditor().get(1).getText().toString();
        int maxNewCard = 0;
        int maxRevCard = 0;
        try {
            maxNewCard = Integer.parseInt(text1);
        } catch (Exception ignored) {
        }
        try {
            maxRevCard = Integer.parseInt(text2);
        } catch (Exception ignored) {
        }
        if (maxNewCard >= 0 && maxNewCard <= 9999 && maxRevCard >= 0 && maxRevCard <= 9999) {
            mOptions.getJSONObject("new").put("perDay", maxNewCard);
            mOptions.getJSONObject("rev").put("perDay", maxRevCard);
            Timber.i("edit new and rev max:" + maxNewCard + "," + maxRevCard);
            try {
                getCol().getDecks().save(mOptions);
            } catch (RuntimeException e) {
                Timber.e(e, "DeckOptions - RuntimeException on saving conf");
                AnkiDroidApp.sendExceptionReport(e, "DeckOptionsSaveConf");
            }
            refreshOption();
            dialog.dismiss();
            openReviewerInternal();
        } else {
            UIUtils.showThemedToast(getAnkiActivity(), "请填写0至9999之间的数值", false);
        }
    }).setNegativeButton("跳过", (dialog, which) -> {
        dialog.dismiss();
        try {
            getCol().getDecks().save(mOptions);
        } catch (RuntimeException e) {
            Timber.e(e, "DeckOptions - RuntimeException on saving conf");
            AnkiDroidApp.sendExceptionReport(e, "DeckOptionsSaveConf");
        }
        refreshOption();
        openReviewerInternal();
    }).create();
    customDialog.show();
}
Also used : DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) Bundle(android.os.Bundle) TypeToken(com.google.gson.reflect.TypeToken) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) Maps(java8.util.Maps) KEY_STOPPED(com.ichi2.anki.StudySettingActivity.KEY_STOPPED) DeckInfoListAdapter(com.ichi2.anki.widgets.DeckInfoListAdapter) Decks(com.ichi2.libanki.Decks) Gson(com.google.gson.Gson) Locale(java.util.Locale) Handler(android.os.Handler) DeckConfig(com.ichi2.libanki.DeckConfig) Map(java.util.Map) Fragment(androidx.fragment.app.Fragment) View(android.view.View) Button(android.widget.Button) TaskData(com.ichi2.async.TaskData) RecyclerView(androidx.recyclerview.widget.RecyclerView) REQUEST_BROWSE_CARDS(com.ichi2.anki.DeckPicker.REQUEST_BROWSE_CARDS) TabLayout(com.google.android.material.tabs.TabLayout) TYPE_LIFE(com.ichi2.libanki.stats.Stats.AxisType.TYPE_LIFE) SHOW_STUDYOPTIONS(com.ichi2.anki.DeckPicker.SHOW_STUDYOPTIONS) Set(java.util.Set) FragmentTransaction(androidx.fragment.app.FragmentTransaction) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) DeckPickerContextMenu(com.ichi2.anki.dialogs.DeckPickerContextMenu) List(java.util.List) Nullable(androidx.annotation.Nullable) TextView(android.widget.TextView) NextCardHandler(com.ichi2.anki.AbstractFlashcardViewer.NextCardHandler) ALL_DECKS_ID(com.ichi2.libanki.stats.Stats.ALL_DECKS_ID) CompatHelper(com.ichi2.compat.CompatHelper) Toolbar(androidx.appcompat.widget.Toolbar) RelativeLayout(android.widget.RelativeLayout) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Window(android.view.Window) Snackbar(com.google.android.material.snackbar.Snackbar) DialogFragment(androidx.fragment.app.DialogFragment) KEY_SHOW_TTS_ICON(com.ichi2.libanki.Consts.KEY_SHOW_TTS_ICON) Context(android.content.Context) TaskListener(com.ichi2.async.TaskListener) Dialog(android.app.Dialog) Intent(android.content.Intent) StepsPreference.convertToJSON(com.ichi2.preferences.StepsPreference.convertToJSON) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) PixelFormat(android.graphics.PixelFormat) Editable(android.text.Editable) TypedArray(android.content.res.TypedArray) MenuItem(android.view.MenuItem) TASK_TYPE(com.ichi2.async.CollectionTask.TASK_TYPE) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Menu(android.view.Menu) Utils(com.ichi2.libanki.Utils) AbstractDeckTreeNode(com.ichi2.libanki.sched.AbstractDeckTreeNode) Stats(com.ichi2.libanki.stats.Stats) LayoutInflater(android.view.LayoutInflater) CollectionTask(com.ichi2.async.CollectionTask) Color(android.graphics.Color) Gravity(android.view.Gravity) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) TreeMap(java.util.TreeMap) KEY_MIND_MODE(com.ichi2.anki.StudySettingActivity.KEY_MIND_MODE) CustomStudyDialog(com.ichi2.anki.dialogs.CustomStudyDialog) DeckPicker.fadeOut(com.ichi2.anki.DeckPicker.fadeOut) Activity(android.app.Activity) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) DeckPicker.fadeIn(com.ichi2.anki.DeckPicker.fadeIn) STUDY_SETTING(com.ichi2.anki.StudySettingActivity.STUDY_SETTING) EditText(android.widget.EditText) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) Resources(android.content.res.Resources) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Dialog(android.app.Dialog) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) CustomStudyDialog(com.ichi2.anki.dialogs.CustomStudyDialog) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) Editable(android.text.Editable)

Example 2 with DeckOptions

use of com.ichi2.anki.DeckOptions in project AnkiChinaAndroid by ankichinateam.

the class StudyOptionsFragment method openReviewer.

private void openReviewer() {
    if (mShouldConfigBeforeStudy && mOptions != null) {
        SharedPreferences sharedPreferences = getAnkiActivity().getSharedPreferences(STUDY_SETTING, 0);
        StringBuilder initIds = new StringBuilder(sharedPreferences.getString(KEY_CONFIG_INIT, ""));
        if (initIds.length() > 0) {
            String[] ids = initIds.toString().split(",");
            if (ids.length > 0) {
                for (String id : ids) {
                    if (getCol().getDecks().current().getLong("id") == Long.parseLong(id)) {
                        // 已经初始化过了,直接跳过
                        openReviewerInternal();
                        return;
                    }
                }
            }
        // 没初始化过,那现在也初始化了。
        // initIds.append(",");
        }
        for (long id : getDeckIds(getCol().getDecks().current().getLong("id"), getCol())) {
            if (!initIds.toString().isEmpty()) {
                initIds.append(",");
            }
            initIds.append(id);
        }
        // initIds += Stats.deckLimit(getCol().getDecks().current().getLong("id"), getCol()).replace("(","").replace(")","").trim();//要把子牌组也加入里面
        sharedPreferences.edit().putString(KEY_CONFIG_INIT, initIds.toString()).apply();
        String savedMindModeValue = sharedPreferences.getString(KEY_MIND_MODE, "");
        Map<String, Integer> map = null;
        try {
            Gson gson = new Gson();
            map = gson.fromJson(savedMindModeValue, new TypeToken<Map<String, Integer>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String mDeckIdStr = String.valueOf(getCol().getDecks().current().getLong("id"));
        mCurrentMindModeValue = map != null && map.get(mDeckIdStr) != null ? map.get(mDeckIdStr) : 0;
        if (mMindModeValues == null) {
            mMindModeValues = getResources().getStringArray(R.array.mind_mode_values);
            mMindModeLabels = getResources().getStringArray(R.array.mind_mode_labels);
            mMindModeHints = getResources().getStringArray(R.array.mind_mode_hint);
            for (int i = 0; i < mMindModeValues.length; i++) {
                mMindModeMap.put(Integer.valueOf(mMindModeValues[i]), mMindModeLabels[i]);
            }
        }
        CustomStyleDialog customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("记忆模式").setCustomLayout(R.layout.dialog_common_custom_next).setSelectListModeCallback(new CustomStyleDialog.Builder.SelectListModeCallback() {

            @Override
            public String[] getItemContent() {
                return mMindModeLabels;
            }

            @Override
            public String[] getItemHint() {
                return mMindModeHints;
            }

            @Override
            public int getDefaultSelectedPosition() {
                return mCurrentMindModeValue;
            }

            @Override
            public void onItemSelect(int position) {
                mCurrentMindModeValue = position;
            }
        }).setPositiveButton("下一步", (dialog, which) -> {
            String oldValue = sharedPreferences.getString(KEY_MIND_MODE, "");
            Map<String, Integer> oldMap = null;
            Gson gson = new Gson();
            try {
                oldMap = gson.fromJson(oldValue, new TypeToken<Map<String, Integer>>() {
                }.getType());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (oldMap == null) {
                oldMap = new HashMap<>();
            }
            for (long id : getDeckIds(getCol().getDecks().current().getLong("id"), getCol())) {
                // Timber.i("看看都是什么id %s", id);
                oldMap.put(String.valueOf(id), mCurrentMindModeValue);
            }
            String newValue = gson.toJson(oldMap);
            sharedPreferences.edit().putString(KEY_MIND_MODE, newValue).apply();
            CollectionTask.launchCollectionTask(CONF_RESET, new ConfChangeHandler(StudyOptionsFragment.this, mCurrentMindModeValue), // 先恢复默认,即长记模式
            new TaskData(new Object[] { mOptions }));
            dialog.dismiss();
        }).setNegativeButton("跳过", (dialog, which) -> {
            dialog.dismiss();
            try {
                getCol().getDecks().save(mOptions);
            } catch (RuntimeException e) {
                Timber.e(e, "DeckOptions - RuntimeException on saving conf");
                AnkiDroidApp.sendExceptionReport(e, "DeckOptionsSaveConf");
            }
            refreshOption();
            openReviewerInternal();
        }).create();
        customDialog.show();
    } else {
        openReviewerInternal();
    }
}
Also used : DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) Bundle(android.os.Bundle) TypeToken(com.google.gson.reflect.TypeToken) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) Maps(java8.util.Maps) KEY_STOPPED(com.ichi2.anki.StudySettingActivity.KEY_STOPPED) DeckInfoListAdapter(com.ichi2.anki.widgets.DeckInfoListAdapter) Decks(com.ichi2.libanki.Decks) Gson(com.google.gson.Gson) Locale(java.util.Locale) Handler(android.os.Handler) DeckConfig(com.ichi2.libanki.DeckConfig) Map(java.util.Map) Fragment(androidx.fragment.app.Fragment) View(android.view.View) Button(android.widget.Button) TaskData(com.ichi2.async.TaskData) RecyclerView(androidx.recyclerview.widget.RecyclerView) REQUEST_BROWSE_CARDS(com.ichi2.anki.DeckPicker.REQUEST_BROWSE_CARDS) TabLayout(com.google.android.material.tabs.TabLayout) TYPE_LIFE(com.ichi2.libanki.stats.Stats.AxisType.TYPE_LIFE) SHOW_STUDYOPTIONS(com.ichi2.anki.DeckPicker.SHOW_STUDYOPTIONS) Set(java.util.Set) FragmentTransaction(androidx.fragment.app.FragmentTransaction) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) DeckPickerContextMenu(com.ichi2.anki.dialogs.DeckPickerContextMenu) List(java.util.List) Nullable(androidx.annotation.Nullable) TextView(android.widget.TextView) NextCardHandler(com.ichi2.anki.AbstractFlashcardViewer.NextCardHandler) ALL_DECKS_ID(com.ichi2.libanki.stats.Stats.ALL_DECKS_ID) CompatHelper(com.ichi2.compat.CompatHelper) Toolbar(androidx.appcompat.widget.Toolbar) RelativeLayout(android.widget.RelativeLayout) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Window(android.view.Window) Snackbar(com.google.android.material.snackbar.Snackbar) DialogFragment(androidx.fragment.app.DialogFragment) KEY_SHOW_TTS_ICON(com.ichi2.libanki.Consts.KEY_SHOW_TTS_ICON) Context(android.content.Context) TaskListener(com.ichi2.async.TaskListener) Dialog(android.app.Dialog) Intent(android.content.Intent) StepsPreference.convertToJSON(com.ichi2.preferences.StepsPreference.convertToJSON) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) PixelFormat(android.graphics.PixelFormat) Editable(android.text.Editable) TypedArray(android.content.res.TypedArray) MenuItem(android.view.MenuItem) TASK_TYPE(com.ichi2.async.CollectionTask.TASK_TYPE) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Menu(android.view.Menu) Utils(com.ichi2.libanki.Utils) AbstractDeckTreeNode(com.ichi2.libanki.sched.AbstractDeckTreeNode) Stats(com.ichi2.libanki.stats.Stats) LayoutInflater(android.view.LayoutInflater) CollectionTask(com.ichi2.async.CollectionTask) Color(android.graphics.Color) Gravity(android.view.Gravity) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) TreeMap(java.util.TreeMap) KEY_MIND_MODE(com.ichi2.anki.StudySettingActivity.KEY_MIND_MODE) CustomStudyDialog(com.ichi2.anki.dialogs.CustomStudyDialog) DeckPicker.fadeOut(com.ichi2.anki.DeckPicker.fadeOut) Activity(android.app.Activity) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) DeckPicker.fadeIn(com.ichi2.anki.DeckPicker.fadeIn) STUDY_SETTING(com.ichi2.anki.StudySettingActivity.STUDY_SETTING) EditText(android.widget.EditText) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) Resources(android.content.res.Resources) SharedPreferences(android.content.SharedPreferences) Gson(com.google.gson.Gson) TaskData(com.ichi2.async.TaskData) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 3 with DeckOptions

use of com.ichi2.anki.DeckOptions in project AnkiChinaAndroid by ankichinateam.

the class DeckOptions method onCreate.

@Override
// conversion to fragments tracked as #5019 in github
@SuppressWarnings("deprecation")
protected void onCreate(Bundle icicle) {
    Themes.setThemeLegacy(this);
    super.onCreate(icicle);
    mCol = CollectionHelper.getInstance().getCol(this);
    if (mCol == null) {
        finish();
        return;
    }
    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey("did")) {
        mDeck = mCol.getDecks().get(extras.getLong("did"));
    } else {
        mDeck = mCol.getDecks().current();
    }
    registerExternalStorageListener();
    if (mCol == null) {
        Timber.w("DeckOptions - No Collection loaded");
        finish();
    } else {
        mPref = new DeckPreferenceHack();
        // #6068 - constructor can call finish()
        if (this.isFinishing()) {
            return;
        }
        mPref.registerOnSharedPreferenceChangeListener(this);
        this.addPreferencesFromResource(R.xml.deck_options);
        this.buildLists();
        this.updateSummaries();
        // Set the activity title to include the name of the deck
        String title = getResources().getString(R.string.deckpreferences_title);
        if (title.contains("XXX")) {
            try {
                title = title.replace("XXX", mDeck.getString("name"));
            } catch (JSONException e) {
                title = title.replace("XXX", "???");
            }
        }
        this.setTitle(title);
    }
    // Add a home button to the actionbar
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Also used : Bundle(android.os.Bundle) JSONException(com.ichi2.utils.JSONException)

Example 4 with DeckOptions

use of com.ichi2.anki.DeckOptions in project Anki-Android by ankidroid.

the class DeckOptions method onCreate.

@Override
// conversion to fragments tracked as #5019 in github
@SuppressWarnings("deprecation")
protected void onCreate(Bundle icicle) {
    Themes.setThemeLegacy(this);
    super.onCreate(icicle);
    mCol = CollectionHelper.getInstance().getCol(this);
    if (mCol == null) {
        finish();
        return;
    }
    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey("did")) {
        mDeck = mCol.getDecks().get(extras.getLong("did"));
    } else {
        mDeck = mCol.getDecks().current();
    }
    registerExternalStorageListener();
    if (mCol == null) {
        Timber.w("DeckOptions - No Collection loaded");
        finish();
    } else {
        mPref = new DeckPreferenceHack();
        // #6068 - constructor can call finish()
        if (this.isFinishing()) {
            return;
        }
        mPref.registerOnSharedPreferenceChangeListener(this);
        this.addPreferencesFromResource(R.xml.deck_options);
        if (this.isSchedV2()) {
            this.enableSchedV2Preferences();
        }
        this.buildLists();
        this.updateSummaries();
        // Set the activity title to include the name of the deck
        String title = getResources().getString(R.string.deckpreferences_title);
        if (title.contains("XXX")) {
            try {
                title = title.replace("XXX", mDeck.getString("name"));
            } catch (JSONException e) {
                Timber.w(e);
                title = title.replace("XXX", "???");
            }
        }
        this.setTitle(title);
    }
    // Add a home button to the actionbar
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Also used : Bundle(android.os.Bundle) JSONException(com.ichi2.utils.JSONException)

Aggregations

Bundle (android.os.Bundle)4 Activity (android.app.Activity)2 Dialog (android.app.Dialog)2 Context (android.content.Context)2 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Resources (android.content.res.Resources)2 TypedArray (android.content.res.TypedArray)2 Color (android.graphics.Color)2 PixelFormat (android.graphics.PixelFormat)2 ColorDrawable (android.graphics.drawable.ColorDrawable)2 Drawable (android.graphics.drawable.Drawable)2 Uri (android.net.Uri)2 Handler (android.os.Handler)2 Editable (android.text.Editable)2 DisplayMetrics (android.util.DisplayMetrics)2 TypedValue (android.util.TypedValue)2 Gravity (android.view.Gravity)2 LayoutInflater (android.view.LayoutInflater)2 Menu (android.view.Menu)2