Search in sources :

Example 1 with OnRangeChangedListener

use of com.jaygoo.widget.OnRangeChangedListener in project AnkiChinaAndroid by ankichinateam.

the class SelfStudyActivity method showScreenDialog.

private void showScreenDialog() {
    InputMethodManager manager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
    if (manager != null && getCurrentFocus() != null) {
        manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
    View dialog = getLayoutInflater().inflate(R.layout.pop_window_screen_self_study, null);
    mPopupWindow = new PopupWindow(dialog, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    TextView studyTitle = dialog.findViewById(R.id.memory_title);
    TextView markTitle = dialog.findViewById(R.id.mark_title);
    TextView answerTitle = dialog.findViewById(R.id.answer_title);
    WarpLinearLayout studyLayout = dialog.findViewById(R.id.memory_layout);
    WarpLinearLayout markLayout = dialog.findViewById(R.id.mark_layout);
    WarpLinearLayout answerLayout = dialog.findViewById(R.id.answer_layout);
    studyLayout.setVisibility(mTabType != TAB_MARK_STATE && mTabType != TAB_ANSWER_STATE ? View.VISIBLE : View.GONE);
    studyTitle.setVisibility(studyLayout.getVisibility());
    markLayout.setVisibility(View.VISIBLE);
    markTitle.setVisibility(View.VISIBLE);
    answerLayout.setVisibility(mTabType != TAB_MARK_STATE && mTabType != TAB_STUDY_STATE ? View.VISIBLE : View.GONE);
    answerTitle.setVisibility(answerLayout.getVisibility());
    List<Button> studyButtonList = new ArrayList<>();
    for (int i = 0; i < mArrayStudy.length; i++) {
        View view = getLayoutInflater().inflate(R.layout.item_warp, null);
        Button button = view.findViewById(R.id.text);
        button.setSelected(mSelectedStudyButtonList.contains(i));
        button.setText(mArrayStudy[i]);
        button.setTag(i);
        button.setOnClickListener(v -> {
            if ((Integer) v.getTag() == 0) {
                for (int m = 1; m < studyButtonList.size(); m++) {
                    if (mSelectedStudyButtonList.contains(m)) {
                        mSelectedStudyButtonList.remove((Object) m);
                    }
                    studyButtonList.get(m).setSelected(false);
                }
            } else {
                studyButtonList.get(0).setSelected(false);
                if (mSelectedStudyButtonList.contains(0)) {
                    mSelectedStudyButtonList.remove((Object) 0);
                }
            }
            if (mSelectedStudyButtonList.contains(v.getTag())) {
                mSelectedStudyButtonList.remove(v.getTag());
            } else {
                mSelectedStudyButtonList.add((Integer) v.getTag());
            }
            v.setSelected(!v.isSelected());
        });
        studyButtonList.add(button);
        studyLayout.addView(view);
    }
    List<Button> markButtonList = new ArrayList<>();
    for (int i = 0; i < mArrayMark.length; i++) {
        View view = getLayoutInflater().inflate(R.layout.item_warp, null);
        Button button = view.findViewById(R.id.text);
        button.setSelected(mSelectedMarkButtonList.contains(i));
        button.setText(mArrayMark[i]);
        button.setTag(i);
        button.setOnClickListener(v -> {
            if ((Integer) v.getTag() == 0) {
                for (int m = 1; m < markButtonList.size(); m++) {
                    if (mSelectedMarkButtonList.contains(m)) {
                        mSelectedMarkButtonList.remove((Object) m);
                    }
                    markButtonList.get(m).setSelected(false);
                }
            } else {
                markButtonList.get(0).setSelected(false);
                if (mSelectedMarkButtonList.contains(0)) {
                    mSelectedMarkButtonList.remove((Object) 0);
                }
            }
            if (mSelectedMarkButtonList.contains(v.getTag())) {
                mSelectedMarkButtonList.remove(v.getTag());
            } else {
                mSelectedMarkButtonList.add((Integer) v.getTag());
            }
            v.setSelected(!v.isSelected());
        });
        markButtonList.add(button);
        markLayout.addView(view);
    }
    List<Button> answerButtonList = new ArrayList<>();
    for (int i = 0; i < mArrayAnswer.length; i++) {
        View view = getLayoutInflater().inflate(R.layout.item_warp, null);
        Button button = view.findViewById(R.id.text);
        button.setSelected(mSelectedAnswerButtonList.contains(i));
        button.setText(mArrayAnswer[i]);
        button.setTag(i);
        button.setOnClickListener(v -> {
            if ((Integer) v.getTag() == 0) {
                for (int m = 1; m < answerButtonList.size(); m++) {
                    if (mSelectedAnswerButtonList.contains(m)) {
                        mSelectedAnswerButtonList.remove((Object) m);
                    }
                    answerButtonList.get(m).setSelected(false);
                }
            } else {
                answerButtonList.get(0).setSelected(false);
                if (mSelectedAnswerButtonList.contains(0)) {
                    mSelectedAnswerButtonList.remove((Object) 0);
                }
            }
            if (mSelectedAnswerButtonList.contains(v.getTag())) {
                mSelectedAnswerButtonList.remove(v.getTag());
            } else {
                mSelectedAnswerButtonList.add((Integer) v.getTag());
            }
            v.setSelected(!v.isSelected());
        });
        answerButtonList.add(button);
        answerLayout.addView(view);
    }
    RangeSeekBar answerTimesSeekBar = dialog.findViewById(R.id.answer_times_seek_bar);
    answerTimesSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {

        @Override
        public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
            if (mTabType == TAB_CUSTOM_STATE) {
                ((TextView) dialog.findViewById(R.id.answer_times_num)).setText(leftValue == answerTimesSeekBar.getMaxProgress() ? "不限" : String.format(Locale.CHINA, "最近%.0f天", leftValue));
            } else {
                ((TextView) dialog.findViewById(R.id.answer_times_num)).setText(String.format(Locale.CHINA, "最近%.0f天", leftValue));
            }
            if (leftValue == answerTimesSeekBar.getMaxProgress()) {
                mRangeSearchAnswerFilter = "";
            } else {
                mRangeSearchAnswerFilter = String.format(Locale.CHINA, "(rated:%.0f)", leftValue);
            }
        }

        @Override
        public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }

        @Override
        public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }
    });
    answerTimesSeekBar.setRange(0, mTabType == TAB_CUSTOM_STATE ? 31 : 30);
    answerTimesSeekBar.setProgress(mTabType == TAB_CUSTOM_STATE ? 31 : 30);
    RangeSeekBar createTimeSeekBar = dialog.findViewById(R.id.create_time_seek_bar);
    createTimeSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {

        @Override
        public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
            ((TextView) dialog.findViewById(R.id.create_time_num)).setText(leftValue == createTimeSeekBar.getMaxProgress() ? "不限" : String.format(Locale.CHINA, "最近%.0f天", leftValue));
            if (leftValue == createTimeSeekBar.getMaxProgress()) {
                mRangeCreateTimeFilter = "";
            } else {
                mRangeCreateTimeFilter = String.format(Locale.CHINA, "(added:%.0f)", leftValue);
            }
        }

        @Override
        public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }

        @Override
        public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }
    });
    createTimeSeekBar.setRange(0, 366);
    createTimeSeekBar.setProgress(366);
    dialog.findViewById(R.id.answer_times_seek_layout).setVisibility(mTabType == TAB_ANSWER_STATE || mTabType == TAB_CUSTOM_STATE || mTabType == TAB_MAIN_STATE ? View.VISIBLE : View.GONE);
    RangeSeekBar reviewTimesSeekBar = dialog.findViewById(R.id.review_times_seek_bar);
    reviewTimesSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {

        @Override
        public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
            String rightStr = rightValue == reviewTimesSeekBar.getMaxProgress() ? "不限" : String.format(Locale.CHINA, "%.0f", rightValue);
            ((TextView) dialog.findViewById(R.id.review_times_num)).setText(String.format(Locale.CHINA, "%.0f-%s", leftValue, rightStr));
            if (rightValue == reviewTimesSeekBar.getMaxProgress()) {
                if (leftValue > 0) {
                    mRangeSearchReviewTimesFilter = String.format(Locale.CHINA, "(prop:reps>=%.0f)", leftValue);
                } else {
                    mRangeSearchReviewTimesFilter = "";
                }
            } else {
                mRangeSearchReviewTimesFilter = String.format(Locale.CHINA, "(prop:reps>=%.0f prop:reps<=%.0f)", leftValue, rightValue);
            }
        }

        @Override
        public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }

        @Override
        public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }
    });
    reviewTimesSeekBar.setRange(0, 20);
    reviewTimesSeekBar.setProgress(0, 20);
    RangeSeekBar forgetTimesSeekBar = dialog.findViewById(R.id.forget_times_seek_bar);
    forgetTimesSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {

        @Override
        public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
            String rightStr = rightValue == forgetTimesSeekBar.getMaxProgress() ? "不限" : String.format(Locale.CHINA, "%.0f", rightValue);
            ((TextView) dialog.findViewById(R.id.forget_times_num)).setText(String.format(Locale.CHINA, "%.0f-%s", leftValue, rightStr));
            if (rightValue == forgetTimesSeekBar.getMaxProgress()) {
                if (leftValue > 0) {
                    mRangeSearchForgetTimesFilter = String.format(Locale.CHINA, "(prop:lapses>=%.0f)", leftValue);
                } else {
                    mRangeSearchForgetTimesFilter = "";
                }
            } else {
                mRangeSearchForgetTimesFilter = String.format(Locale.CHINA, "(prop:lapses>=%.0f prop:lapses<=%.0f)", leftValue, rightValue);
            }
        }

        @Override
        public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }

        @Override
        public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }
    });
    forgetTimesSeekBar.setRange(0, 10);
    forgetTimesSeekBar.setProgress(0, 10);
    Spinner deck_spinner = dialog.findViewById(R.id.deck_spinner);
    deck_spinner.setAdapter(mDropDownDeckAdapter);
    deck_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            mSelectedDropDownDeckPosition = position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        // do nothing
        }
    });
    deck_spinner.setSelection(mSelectedDropDownDeckPosition);
    RelativeLayout tag_spinner = dialog.findViewById(R.id.tag_spinner);
    tag_spinner.setVisibility(mTabType == TAB_CUSTOM_STATE || mTabType == TAB_MAIN_STATE ? View.VISIBLE : View.GONE);
    TextView tag_selected = dialog.findViewById(R.id.tag_selected);
    tag_spinner.setOnClickListener(v -> showTagsDialog(tag_selected));
    Button reset = dialog.findViewById(R.id.reset);
    Button confirm = dialog.findViewById(R.id.confirm);
    reset.setOnClickListener(v -> {
        resetFilter();
        // 重置卡组选择
        deck_spinner.setSelection(mSelectedDropDownDeckPosition);
        for (int i = 1; i < studyLayout.getChildCount(); i++) {
            studyLayout.getChildAt(i).setSelected(false);
        }
        for (int i = 1; i < markLayout.getChildCount(); i++) {
            markLayout.getChildAt(i).setSelected(false);
        }
        for (int i = 1; i < answerLayout.getChildCount(); i++) {
            answerLayout.getChildAt(i).setSelected(false);
        }
        switch(mTabType) {
            case TAB_STUDY_STATE:
                studyLayout.getChildAt(1).performClick();
                break;
            case TAB_MARK_STATE:
                markLayout.getChildAt(1).performClick();
                break;
            case TAB_ANSWER_STATE:
                answerLayout.getChildAt(1).performClick();
                break;
            case TAB_CUSTOM_STATE:
            case TAB_MAIN_STATE:
                break;
        }
        forgetTimesSeekBar.setProgress(0, 10);
        reviewTimesSeekBar.setProgress(0, 20);
        answerTimesSeekBar.setProgress(mTabType == TAB_CUSTOM_STATE || mTabType == TAB_MAIN_STATE ? 31 : 30);
    });
    confirm.setOnClickListener(v -> {
        if (mTabType == TAB_MAIN_STATE) {
            for (int i = 0; i < mTabLayout.getTabCount(); i++) {
                mTabLayout.getTabAt(i).getCustomView().setSelected(false);
            }
        } else if (mTabType != TAB_CUSTOM_STATE) {
            mTabLayout.getTabAt(mSelectedTabIndex - 1).getCustomView().setSelected(false);
        }
        mRestrictOnTab = "";
        // 标签选择
        updateTagsSearchTerms();
        // 卡牌选择
        deckDropDownItemChanged(mSelectedDropDownDeckPosition);
        // 按钮选择
        updateButtonFilter();
        mPopupWindow.dismiss();
        searchCards();
    });
    mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    mPopupWindow.setOutsideTouchable(false);
    mPopupWindow.setFocusable(true);
    mPopupWindow.setOnDismissListener(() -> findViewById(R.id.shadeView).setVisibility(View.GONE));
    mPopupWindow.showAsDropDown(mToolbar, 0, 0);
    findViewById(R.id.shadeView).setVisibility(View.VISIBLE);
}
Also used : RangeSeekBar(com.jaygoo.widget.RangeSeekBar) OnRangeChangedListener(com.jaygoo.widget.OnRangeChangedListener) Spinner(android.widget.Spinner) ListPopupWindow(androidx.appcompat.widget.ListPopupWindow) PopupWindow(android.widget.PopupWindow) ArrayList(java.util.ArrayList) InputMethodManager(android.view.inputmethod.InputMethodManager) WarpLinearLayout(com.ichi2.ui.WarpLinearLayout) ImageView(android.widget.ImageView) SearchView(androidx.appcompat.widget.SearchView) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) Button(android.widget.Button) RelativeLayout(android.widget.RelativeLayout) OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) TextView(android.widget.TextView)

Example 2 with OnRangeChangedListener

use of com.jaygoo.widget.OnRangeChangedListener in project AnkiChinaAndroid by ankichinateam.

the class SpeakSettingActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_speak_setting);
    Toolbar toolbar = findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        int[] attrs = new int[] { R.attr.reviewStatusBarColor, R.attr.primaryTextColor222222 };
        TypedArray ta = obtainStyledAttributes(attrs);
        toolbar.setBackground(ta.getDrawable(0));
        ((TextView) toolbar.findViewById(R.id.toolbar_title)).setText("朗读设置");
        ((TextView) toolbar.findViewById(R.id.toolbar_title)).setTextColor(ta.getColor(1, ContextCompat.getColor(this, R.color.black)));
    // Decide which action to take when the navigation button is tapped.
    // toolbar.setNavigationOnClickListener(v -> onNavigationPressed());
    }
    tx_front_language = findViewById(R.id.tx_front_language);
    tx_back_language = findViewById(R.id.tx_back_language);
    findViewById2(R.id.rl_front_language);
    findViewById2(R.id.rl_back_language);
    findViewById2(R.id.rl_engine);
    sb_speak_speed = findViewById(R.id.sb_speak_speed);
    switch_auto_speak = findViewById(R.id.switch_speak_auto);
    switch_use_tts = findViewById(R.id.switch_tts_first);
    switch_show_icon = findViewById(R.id.switch_speak_icon_show);
    setTitle("朗读设置");
    long did = getIntent().getLongExtra("did", -1);
    long cid = getIntent().getLongExtra("cid", -1);
    mCurrentCard = getCol().getCard(cid);
    mCurrentDeck = getCol().getDecks().get(did);
    int ord = mCurrentCard.getOrd();
    ReadText.releaseTts();
    ReadText.initializeTts(this, true, false, null);
    if (ReadText.getLanguageName(did, ord, Sound.SoundSide.QUESTION).isEmpty()) {
        ReadText.selectDefaultLanguage(getTextForTts(mCurrentCard.q(true)), getDeckIdForCard(mCurrentCard), mCurrentCard.getOrd(), Sound.SoundSide.QUESTION, true);
    }
    if (ReadText.getLanguageName(did, ord, Sound.SoundSide.ANSWER).isEmpty()) {
        ReadText.selectDefaultLanguage(getTextForTts(mCurrentCard.q(true)), getDeckIdForCard(mCurrentCard), mCurrentCard.getOrd(), Sound.SoundSide.ANSWER, true);
    }
    // tx_front_language.setText(AnkiDroidApp.getSharedPrefs(this).getBoolean(KEY_SELECT_ONLINE_SPEAK_ENGINE, false)?ReadText.getAzureLanguageDisplay(did, ord, Sound.SoundSide.QUESTION):ReadText.getLanguageName(did, ord, Sound.SoundSide.QUESTION));
    // tx_back_language.setText(AnkiDroidApp.getSharedPrefs(this).getBoolean(KEY_SELECT_ONLINE_SPEAK_ENGINE, false)?ReadText.getAzureLanguageDisplay(did, ord, Sound.SoundSide.ANSWER):ReadText.getLanguageName(did, ord, Sound.SoundSide.ANSWER));
    sb_speak_speed.setRange(50, 200);
    sb_speak_speed.setProgress(ReadText.getSpeechRate(did, ord) * 100);
    sb_speak_speed.setOnRangeChangedListener(new OnRangeChangedListener() {

        @Override
        public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
            ReadText.setTtsSpeed(did, ord, leftValue / 100);
        }

        @Override
        public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }

        @Override
        public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
        }
    });
    // DeckConfig mOptions = getCol().getDecks().confForDid(getDeckIdForCard(mCurrentCard));
    switch_auto_speak.setChecked(AnkiDroidApp.getSharedPrefs(this).getBoolean(KEY_AUTO_PLAY_TTS, false));
    switch_auto_speak.setOnCheckedChangeListener((buttonView, isChecked) -> {
        AnkiDroidApp.getSharedPrefs(this).edit().putBoolean(KEY_AUTO_PLAY_TTS, isChecked).apply();
        if (!isChecked) {
            switch_use_tts.setChecked(false);
        }
    // applyAutoPlay(mOptions, isChecked);
    });
    switch_use_tts.setChecked(AnkiDroidApp.getSharedPrefs(this).getBoolean("tts", false));
    switch_use_tts.setOnCheckedChangeListener((buttonView, isChecked) -> {
        AnkiDroidApp.getSharedPrefs(this).edit().putBoolean("tts", isChecked).apply();
        if (isChecked) {
            switch_auto_speak.setChecked(true);
        }
    });
    switch_show_icon.setChecked(AnkiDroidApp.getSharedPrefs(this).getBoolean(KEY_SHOW_TTS_ICON, true));
    switch_show_icon.setOnCheckedChangeListener((buttonView, isChecked) -> AnkiDroidApp.getSharedPrefs(this).edit().putBoolean(KEY_SHOW_TTS_ICON, isChecked).apply());
}
Also used : RangeSeekBar(com.jaygoo.widget.RangeSeekBar) OnRangeChangedListener(com.jaygoo.widget.OnRangeChangedListener) TypedArray(android.content.res.TypedArray) TextView(android.widget.TextView) Toolbar(androidx.appcompat.widget.Toolbar)

Example 3 with OnRangeChangedListener

use of com.jaygoo.widget.OnRangeChangedListener in project AnkiChinaAndroid by ankichinateam.

the class AbstractFlashcardViewer method showLayoutDialog.

protected void showLayoutDialog() {
    SharedPreferences preference = AnkiDroidApp.getSharedPrefs(this);
    if (mLayoutConfigDialog == null) {
        mLayoutConfigDialog = new Dialog(this, R.style.DialogTheme2);
        View view = View.inflate(this, R.layout.dialog_layout_config, null);
        mLayoutConfigDialog.setContentView(view);
        Window window = mLayoutConfigDialog.getWindow();
        mLayoutConfigDialog.setOnDismissListener(dialog -> {
            if (mCurrentCSS.equals(mCurrentEditingDefaultCSS)) {
                // 没修改过,直接返回
                return;
            }
            // 将当前css转化为类,添加修改时间
            JSONObject object = convertCss2JsonAndSave();
            if (needUploadViewSetting == null) {
                needUploadViewSetting = new JSONObject();
            }
            needUploadViewSetting.put(String.valueOf(mCurrentCard.model().getLong("id")), object);
            Timber.i("save layout config :%s", needUploadViewSetting.toString());
            preference.edit().putString(Consts.KEY_LOCAL_LAYOUT_CONFIG, needUploadViewSetting.toString().replace("\\", "")).apply();
            getAccount().getToken(AbstractFlashcardViewer.this, new MyAccount.TokenCallback() {

                @Override
                public void onSuccess(String token) {
                    RequestBody formBody = new FormBody.Builder().add("models_view_settings", needUploadViewSetting.toString()).build();
                    OKHttpUtil.post(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "users/conf", formBody, token, "", new OKHttpUtil.MyCallBack() {

                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Call call, String token, Object arg1, Response response) {
                            if (response.isSuccessful()) {
                                Timber.i("post view settings successfully!:%s", response.body());
                            } else {
                                Timber.e("post view settings failed, error code %d", response.code());
                            }
                        }
                    });
                }

                @Override
                public void onFail(String message) {
                }
            });
        });
        // addMenuBottomDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        // 设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.findViewById(R.id.close).setOnClickListener(v -> mLayoutConfigDialog.dismiss());
        view.findViewById(R.id.reset).setOnClickListener(v -> {
            // 将当前样式变回css()样式
            // mCurrentCSS = mCurrentCard.css().replace("<style>", "").replace("</style>", "");
            mCurrentCSS = ".card {}";
            // mCurrentCSS = mCurrentEditingDefaultCSS;
            // Timber.i("show updateTypeAnswerInfo");
            // JSONObject temp = convertCss2JsonAndSave();
            fontSizeSeekBar.setProgress(0);
            for (View view1 : layers) {
                // 存在无一被选中的情况,通过预先全设置为非选中实现
                view1.setSelected(false);
            }
            for (View view1 : bgColors) {
                view1.setSelected(false);
            }
            for (View view1 : aligns) {
                view1.setSelected(false);
            }
            String localViewSettingStr = preference.getString(Consts.KEY_LOCAL_LAYOUT_CONFIG, "");
            mCurrentCSSModelID = mCurrentCard.model().getLong("id");
            if (localViewSettingStr != null && !localViewSettingStr.isEmpty()) {
                needUploadViewSetting = new JSONObject(localViewSettingStr);
                if (needUploadViewSetting.remove(String.valueOf(mCurrentCSSModelID)) != null) {
                    Timber.i("remove saved setting :%s", mCurrentCSSModelID);
                    preference.edit().putString(Consts.KEY_LOCAL_LAYOUT_CONFIG, needUploadViewSetting.toString().replace("\\", "")).apply();
                }
            }
            if (!mCacheContent.isEmpty()) {
                updateCard(mCacheContent);
            }
        });
        alignLeft = view.findViewById(R.id.ic_align_left);
        alignRight = view.findViewById(R.id.ic_align_right);
        alignNormal = view.findViewById(R.id.ic_align_normal);
        alignCenter = view.findViewById(R.id.ic_align_center);
        aligns.add(alignLeft);
        aligns.add(alignRight);
        aligns.add(alignNormal);
        aligns.add(alignCenter);
        layerTop = view.findViewById(R.id.ic_layer_top);
        layerCenter = view.findViewById(R.id.ic_layer_center);
        layerBottom = view.findViewById(R.id.ic_layer_bottom);
        layers.add(layerTop);
        layers.add(layerCenter);
        layers.add(layerBottom);
        bgColorWhite = view.findViewById(R.id.ic_bg_white);
        bgColorGrey = view.findViewById(R.id.ic_bg_grey);
        bgColorBrown = view.findViewById(R.id.ic_bg_brown);
        bgColorGreen = view.findViewById(R.id.ic_bg_green);
        bgColorBlack = view.findViewById(R.id.ic_bg_black);
        bgColors.add(bgColorWhite);
        bgColors.add(bgColorGrey);
        bgColors.add(bgColorBrown);
        bgColors.add(bgColorGreen);
        bgColors.add(bgColorBlack);
        alignLeft.setOnClickListener(v -> onLayoutConfigAlignClick("left", alignLeft));
        alignRight.setOnClickListener(v -> onLayoutConfigAlignClick("right", alignRight));
        alignNormal.setOnClickListener(v -> onLayoutConfigAlignClick("justify", alignNormal));
        alignCenter.setOnClickListener(v -> onLayoutConfigAlignClick("center", alignCenter));
        layerTop.setOnClickListener(v -> onLayoutConfigLayerClick("flex-start", layerTop));
        layerCenter.setOnClickListener(v -> onLayoutConfigLayerClick("center", layerCenter));
        layerBottom.setOnClickListener(v -> onLayoutConfigLayerClick("flex-end", layerBottom));
        bgColorWhite.setOnClickListener(v -> onLayoutConfigBgClick("white", bgColorWhite));
        bgColorGrey.setOnClickListener(v -> onLayoutConfigBgClick("#F4F4F6", bgColorGrey));
        bgColorBrown.setOnClickListener(v -> onLayoutConfigBgClick("#E4DCCE", bgColorBrown));
        bgColorGreen.setOnClickListener(v -> onLayoutConfigBgClick("#BADEBE", bgColorGreen));
        bgColorBlack.setOnClickListener(v -> onLayoutConfigBgClick("black", bgColorBlack));
        fontSizeSeekBar = view.findViewById(R.id.font_size_seek_bar);
        fontSizeSeekBar.setRange(0, 80);
        fontSizeSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {

            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                if (!isFromUser) {
                    return;
                }
                Matcher fontSizeMatcher = Pattern.compile("font-size:(.+?)px").matcher(mCurrentCSS);
                if (fontSizeMatcher.find()) {
                    String fld1 = fontSizeMatcher.group(0);
                    mCurrentCSS = mCurrentCSS.replace(fld1, String.format(Locale.CHINA, "font-size: %.0fpx", leftValue));
                } else {
                    mCurrentCSS = mCurrentCSS.replace("}", String.format(Locale.CHINA, "font-size: %.0fpx;}", leftValue));
                }
                // 字体大小
                Timber.i("set font size fld:%s", leftValue);
                if (!mCacheContent.isEmpty()) {
                    updateCard(mCacheContent);
                }
            // needUploadViewSetting.put(String.valueOf(mCurrentCard.model().getLong("id")), mCurrentCSS.replace(".card", ""));
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
            }
        });
    }
    if (mLayoutConfigDialog.isShowing()) {
        mLayoutConfigDialog.dismiss();
        return;
    }
    if (mCurrentCSS.isEmpty() || mCurrentCSSModelID != mCurrentCard.model().getLong("id")) {
        // 获取preferences是否有保存的布局设置
        String localViewSettingStr = preference.getString(Consts.KEY_LOCAL_LAYOUT_CONFIG, "");
        boolean notSync = false;
        mCurrentCSSModelID = mCurrentCard.model().getLong("id");
        if (localViewSettingStr != null && !localViewSettingStr.isEmpty()) {
            Timber.i("find local view setting:%s", localViewSettingStr);
            needUploadViewSetting = new JSONObject(localViewSettingStr);
            try {
                JSONObject currentModelSetting = needUploadViewSetting.getJSONObject(String.valueOf(mCurrentCSSModelID));
                if (currentModelSetting != null) {
                    mCurrentCSS = convertJson2Css(currentModelSetting, true);
                } else {
                    notSync = true;
                    // 没这个记录,新建一个默认模板
                    mCurrentCSS = ".card {}";
                }
            } catch (Exception e) {
                e.printStackTrace();
                notSync = true;
                mCurrentCSS = ".card {}";
            // needUploadViewSetting.put()
            }
        } else {
            // needUploadViewSetting = new JSONObject();
            mCurrentCSS = ".card {}";
        }
    } else {
        // 将dialog改为原设布局
        convertJson2Css(convertCss2JsonAndSave(), true);
    }
    // 保存刚载入时的css,留着备用
    mCurrentEditingDefaultCSS = mCurrentCSS;
    // if (localViewSettingStr == null || localViewSettingStr.isEmpty() || notSync) {
    // //本地没数据或当前model未保存,则从数据库model里的css里找 //不找了。
    // mCurrentCSS = mCurrentCard.css().replace("<style>", "").replace("</style>", "");
    // //            Timber.i("show updateTypeAnswerInfo");
    // JSONObject temp = convertCss2JsonAndSave();
    // fontSizeSeekBar.setProgress(Float.parseFloat(temp.getString("font_size")) - 8);
    // updateLayoutConfigDialog(TYPE_ALIGN, temp.getString("text_align"));
    // updateLayoutConfigDialog(TYPE_LAYER, temp.getString("layout"));
    // updateLayoutConfigDialog(TYPE_BG, temp.getString("background_color"));
    // if (needUploadViewSetting == null) {
    // needUploadViewSetting = new JSONObject();
    // }
    // if (notSync) {
    // needUploadViewSetting.put(String.valueOf(mCurrentCSSModelID), temp.toString());
    // }
    // }
    mLayoutConfigDialog.show();
}
Also used : Window(android.view.Window) Call(okhttp3.Call) RangeSeekBar(com.jaygoo.widget.RangeSeekBar) OnRangeChangedListener(com.jaygoo.widget.OnRangeChangedListener) SharedPreferences(android.content.SharedPreferences) Matcher(java.util.regex.Matcher) FormBody(okhttp3.FormBody) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) AudioView(com.ichi2.anki.multimediacard.AudioView) JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) ActivityNotFoundException(android.content.ActivityNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) WebResourceResponse(android.webkit.WebResourceResponse) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) TagsDialog(com.ichi2.anki.dialogs.TagsDialog) Dialog(android.app.Dialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) JSONObject(com.ichi2.utils.JSONObject) RequestBody(okhttp3.RequestBody)

Aggregations

TextView (android.widget.TextView)3 OnRangeChangedListener (com.jaygoo.widget.OnRangeChangedListener)3 RangeSeekBar (com.jaygoo.widget.RangeSeekBar)3 View (android.view.View)2 ImageView (android.widget.ImageView)2 Dialog (android.app.Dialog)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 SharedPreferences (android.content.SharedPreferences)1 TypedArray (android.content.res.TypedArray)1 Window (android.view.Window)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 WebResourceResponse (android.webkit.WebResourceResponse)1 WebView (android.webkit.WebView)1 AdapterView (android.widget.AdapterView)1 OnItemSelectedListener (android.widget.AdapterView.OnItemSelectedListener)1 Button (android.widget.Button)1 PopupWindow (android.widget.PopupWindow)1 RelativeLayout (android.widget.RelativeLayout)1 Spinner (android.widget.Spinner)1 ListPopupWindow (androidx.appcompat.widget.ListPopupWindow)1