use of com.ichi2.ui.SettingItem in project AnkiChinaAndroid by ankichinateam.
the class SettingFragment method onCreateView.
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Timber.i("on create view in setting fragment");
if (mRoot == null) {
final SharedPreferences preferences = getPreferences();
mRoot = inflater.inflate(R.layout.fragment_setting, container, false);
rl_login = mRoot.findViewById(R.id.rl_login);
mLl_defaultLink = mRoot.findViewById(R.id.ll_default_link);
mRl_user_name = mRoot.findViewById(R.id.tv_user_name);
mTv_logout = mRoot.findViewById(R.id.tv_quit);
mIv_entrance = mRoot.findViewById(R.id.iv_entrance);
mRl_cloud_space = mRoot.findViewById(R.id.tv_cloud_space);
mLl_switch_server = mRoot.findViewById(R.id.ll_switch_server);
mTv_server_name = mRoot.findViewById(R.id.server_name);
mTv_switch_server = mRoot.findViewById(R.id.tv_switch_server);
rl_login.setOnClickListener(v -> ((DeckPicker) getAnkiActivity()).loginToSyncServer());
mRoot.findViewById(R.id.rl_personal_setting).setOnClickListener(v -> {
Timber.i("Navigating to settings");
mOldColPath = CollectionHelper.getCurrentAnkiDroidDirectory(getAnkiActivity());
// Remember the theme we started with so we can restart the Activity if it changes
mOldTheme = Themes.getCurrentTheme(getContext());
getAnkiActivity().startActivityForResultWithAnimation(new Intent(getAnkiActivity(), Preferences.class), REQUEST_PREFERENCES_UPDATE, ActivityTransitionAnimation.FADE);
});
// mRoot.findViewById(R.id.rl_sync_set).setOnClickListener(v -> {
// Timber.i("AnkiDroid directory inaccessible");
// Intent i = Preferences.getPreferenceSubscreenIntent(getAnkiActivity(), "com.ichi2.anki.prefs.general");
// getAnkiActivity().startActivityWithAnimation(i, ActivityTransitionAnimation.FADE);
// });
mNightModeSwitch = mRoot.findViewById(R.id.switch_dark_mode);
mNightModeSwitch.setChecked(preferences.getBoolean(NIGHT_MODE_PREFERENCE, false));
mNightModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
applyNightMode(isChecked);
});
mAutoSyncSwitch = mRoot.findViewById(R.id.switch_auto_sync);
mAutoSyncSwitch.setChecked(preferences.getBoolean("automaticSyncMode", true));
mAutoSyncSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
preferences.edit().putBoolean("automaticSyncMode", isChecked).apply();
});
mRoot.findViewById(R.id.rl_dark_mode).setOnClickListener(v -> {
Timber.i("Toggling Night Mode");
mNightModeSwitch.performClick();
});
mRoot.findViewById(R.id.vip_power).setOnClickListener(this);
mRoot.findViewById(R.id.rl_anki_course).setOnClickListener(this);
mRoot.findViewById(R.id.rl_team).setOnClickListener(this);
mRoot.findViewById(R.id.rl_version).setOnClickListener(this);
mRoot.findViewById(R.id.rl_feedback).setOnClickListener(this);
mRoot.findViewById(R.id.user_protocol).setOnClickListener(this);
mRoot.findViewById(R.id.user_private).setOnClickListener(this);
mRoot.findViewById(R.id.rl_market_like).setOnClickListener(this);
mLl_switch_server.setOnClickListener(this);
mVipText = mRoot.findViewById(R.id.vip_text);
mVipPower = mRoot.findViewById(R.id.vip_power);
mVipText.setText(mVip ? getVipString(mVipDay) : getNotVipString());
mVipPower.setText(mVip ? "查看权益" : "立即开通");
OKHttpUtil.get(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "configs/1", "", "", new OKHttpUtil.MyCallBack() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, String token, Object arg1, Response response) throws IOException {
if (response.isSuccessful()) {
Timber.i("initMoreDrawerMenuItem successfully!:%s", response.body());
try {
final JSONObject object = new JSONObject(response.body().string());
final JSONArray items = object.getJSONArray("data");
Timber.i("initMoreDrawerMenuItem %d ", items.length());
List<DynamicItem> dynamicItems = new ArrayList<>();
for (int i = 0; i < items.length(); i++) {
JSONObject jsonObject = items.getJSONObject(i);
String title = jsonObject.optString("title");
String image = jsonObject.optString("image_url");
String link = jsonObject.optString("target_url");
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(new URL(image).openStream(), title + ".jpg");
} catch (Exception e) {
e.printStackTrace();
}
dynamicItems.add(new DynamicItem(title, link, drawable));
Timber.i("load drawable result %s,%s ,%s ", title, image, link);
}
getAnkiActivity().runOnUiThread(() -> {
mLl_defaultLink.removeAllViews();
for (int i = 0; i < dynamicItems.size(); i++) {
int finalI = i;
int[] attrs = new int[] { R.attr.settingItemBackgroundTop, R.attr.settingItemBackground, R.attr.settingItemBackgroundBottom, R.attr.settingItemBackgroundRound };
TypedArray ta = getAnkiActivity().obtainStyledAttributes(attrs);
Drawable background;
background = ta.getDrawable(dynamicItems.size() == 1 ? 3 : finalI == 0 ? 0 : finalI == dynamicItems.size() - 1 ? 2 : 1);
DynamicItem dynamicItem = dynamicItems.get(finalI);
SettingItem item = new SettingItem(getContext(), dynamicItem.title, dynamicItem.drawable);
item.findViewById(R.id.rl_root).setBackground(background);
item.setOnClickListener(v -> {
WebViewActivity.openUrlInApp(getAnkiActivity(), dynamicItem.link, "", -1);
});
mLl_defaultLink.addView(item);
}
});
} catch (Exception e) {
e.printStackTrace();
}
} else {
Timber.e("initMoreDrawerMenuItem failed, error code %d", response.code());
}
}
});
}
return mRoot;
}
Aggregations