Skip to content

Commit

Permalink
Closes #591
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantcheese committed Feb 11, 2020
1 parent d168d75 commit a84cbcf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@

import com.github.adamantcheese.chan.core.settings.OptionsSetting;
import com.github.adamantcheese.chan.core.settings.Setting;
import com.github.adamantcheese.chan.core.settings.StringSetting;

import java.util.List;

import static com.github.adamantcheese.chan.core.site.SiteSetting.Type.OPTIONS;
import static com.github.adamantcheese.chan.core.site.SiteSetting.Type.STRING;

/**
* Hacky stuff to give the site settings a good UI.
*/
public class SiteSetting {
public enum Type {
OPTIONS
OPTIONS,
STRING
}

public final String name;
Expand All @@ -37,15 +40,19 @@ public enum Type {

public List<String> optionNames;

private SiteSetting(String name, Setting<?> setting) {
private SiteSetting(String name, Setting<?> setting, Type type) {
this.name = name;
this.type = OPTIONS;
this.type = type;
this.setting = setting;
}

public static SiteSetting forOption(OptionsSetting<?> options, String name, List<String> optionNames) {
SiteSetting setting = new SiteSetting(name, options);
SiteSetting setting = new SiteSetting(name, options, OPTIONS);
setting.optionNames = optionNames;
return setting;
}

public static SiteSetting forString(StringSetting setting, String name) {
return new SiteSetting(name, setting, STRING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -466,6 +465,7 @@ public String getKey() {
}

private OptionsSetting<CaptchaType> captchaType;
public static StringSetting flagType;

public Chan4() {
// we used these before multisite, and lets keep using them.
Expand All @@ -484,14 +484,15 @@ public void initializeSettings() {
super.initializeSettings();

captchaType = new OptionsSetting<>(settingsProvider, "preference_captcha_type_chan4", CaptchaType.class, V2JS);
flagType = new StringSetting(settingsProvider, "preference_flag_chan4", "0");
}

@Override
public List<SiteSetting> settings() {
return Collections.singletonList(SiteSetting.forOption(captchaType,
"Captcha type",
Arrays.asList("Javascript", "Noscript")
));
List<SiteSetting> settings = new ArrayList<>();
settings.add(SiteSetting.forOption(captchaType, "Captcha type", Arrays.asList("Javascript", "Noscript")));
settings.add(SiteSetting.forString(flagType, "Country flag code"));
return settings;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void addParameters(
}
}

if (site instanceof Chan4 && reply.loadable.board.code.equals("pol")) {
formBuilder.addFormDataPart("flag", Chan4.flagType.get());
}

if (reply.file != null) {
attachFile(formBuilder, progressListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@
import com.github.adamantcheese.chan.R;
import com.github.adamantcheese.chan.core.presenter.SiteSetupPresenter;
import com.github.adamantcheese.chan.core.settings.OptionsSetting;
import com.github.adamantcheese.chan.core.settings.StringSetting;
import com.github.adamantcheese.chan.core.site.Site;
import com.github.adamantcheese.chan.core.site.SiteSetting;
import com.github.adamantcheese.chan.ui.controller.settings.SettingsController;
import com.github.adamantcheese.chan.ui.settings.LinkSettingView;
import com.github.adamantcheese.chan.ui.settings.ListSettingView;
import com.github.adamantcheese.chan.ui.settings.SettingsGroup;
import com.github.adamantcheese.chan.ui.settings.StringSettingView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import static com.github.adamantcheese.chan.Chan.inject;
import static com.github.adamantcheese.chan.core.site.SiteSetting.Type.OPTIONS;
import static com.github.adamantcheese.chan.utils.AndroidUtils.getQuantityString;
import static com.github.adamantcheese.chan.utils.AndroidUtils.getString;
import static com.github.adamantcheese.chan.utils.AndroidUtils.inflate;
Expand Down Expand Up @@ -107,23 +108,31 @@ public void showSettings(List<SiteSetting> settings) {
SettingsGroup group = new SettingsGroup("Additional settings");

for (SiteSetting setting : settings) {
if (setting.type == OPTIONS) {

// Turn the SiteSetting for a list of options into a proper setting with a
// name and a list of options, both given in the SiteSetting.
OptionsSetting optionsSetting = (OptionsSetting) setting.setting;

List<ListSettingView.Item<Enum>> items = new ArrayList<>();
Enum[] settingItems = optionsSetting.getItems();
for (int i = 0; i < settingItems.length; i++) {
String name = setting.optionNames.get(i);
Enum anEnum = settingItems[i];
items.add(new ListSettingView.Item<>(name, anEnum));
}

ListSettingView<?> v = getListSettingView(setting, optionsSetting, items);

group.add(v);
switch (setting.type) {
case OPTIONS:
// Turn the SiteSetting for a list of options into a proper setting with a
// name and a list of options, both given in the SiteSetting.
OptionsSetting optionsSetting = (OptionsSetting) setting.setting;

List<ListSettingView.Item<Enum>> items = new ArrayList<>();
Enum[] settingItems = optionsSetting.getItems();
for (int i = 0; i < settingItems.length; i++) {
String name = setting.optionNames.get(i);
Enum anEnum = settingItems[i];
items.add(new ListSettingView.Item<>(name, anEnum));
}

ListSettingView<?> v = getListSettingView(setting, optionsSetting, items);

group.add(v);
break;
case STRING:
// Turn the SiteSetting for a string setting into a proper setting with a name and input
StringSetting stringSetting = (StringSetting) setting.setting;
StringSettingView view = new StringSettingView(this, stringSetting, setting.name, setting.name);

group.add(view);
break;
}
}

Expand Down

0 comments on commit a84cbcf

Please sign in to comment.