refactored superapp utils

removed old vk pay and events
This commit is contained in:
gdlbo
2024-02-14 21:45:31 +03:00
parent bdee6c08c0
commit eab5b7074e
@@ -5,74 +5,77 @@ import org.json.JSONException;
import org.json.JSONObject;
import ru.vtosters.hooks.other.Preferences;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class SuperAppUtils {
public static JSONObject menu(JSONObject orig) throws JSONException {
var Special = orig.optJSONArray("special");
var Main = orig.getJSONArray("main");
var Other = orig.optJSONArray("other");
if (Special != null) {
orig.remove("special");
}
public static JSONObject menu(JSONObject orig) {
orig.remove("special");
return orig;
}
public static JSONObject superapp(JSONObject json) throws JSONException {
var superApps = Preferences.getPreferences().getString("superapp_items", "menu,miniapps,vkpay_slim,greeting,promo,holiday,weather,sport,games,informer,food,event,music,vk_run").split(",");
String[] superApps = Preferences.getPreferences().getString("superapp_items", "menu,miniapps,vkpay_slim,greeting,promo,holiday,weather,sport,games,informer,food,event,music,vk_run").split(",");
if (superApps.length == 0) return json;
var oldItems = json.optJSONArray("items");
var newItems = new JSONArray();
JSONArray oldItems = json.optJSONArray("items");
JSONArray newItems = new JSONArray();
if (oldItems != null) {
for (int i = 0; i < oldItems.length(); i++) {
var item = oldItems.optJSONObject(i);
var type = item.optString("type");
for (String superApp : superApps) {
if (type.equals(superApp))
if (superApp.equals("menu")) { // superapp menu fix
String menuitems = "groups,audios,stickers,podcasts,shopping,lives,videos,games,vk_calls,vk_pay,ads_easy_promote,classifieds"; // ,events
JSONObject menuItem = new JSONObject();
JSONArray menuItems = new JSONArray();
Stream<JSONObject> streamOfItems = IntStream.range(0, oldItems.length())
.mapToObj(oldItems::optJSONObject)
.filter(item -> Arrays.asList(superApps).contains(item.optString("type")));
generateMenuItems(menuitems, menuItems);
streamOfItems = streamOfItems.map(SuperAppUtils::apply);
JSONObject object = new JSONObject();
object.put("count", menuitems.split(",").length);
object.put("items", menuItems);
streamOfItems = streamOfItems.sorted(Comparator.comparingInt(item -> Arrays.asList(superApps).indexOf(item.optString("type"))));
menuItem.put("type", "menu");
menuItem.put("object", object);
newItems.put(menuItem);
} else {
newItems.put(item);
}
}
}
for (int i = 0; i < superApps.length; i++) {
for (int j = i; j < newItems.length(); j++) {
var item = newItems.optJSONObject(j);
if (superApps[i].equals(item.optString("type")))
newItems.put(j, newItems.getJSONObject(i))
.put(i, item);
}
}
streamOfItems.forEach(newItems::put);
}
return json.putOpt("items", newItems);
}
private static void generateMenuItems(String str, JSONArray jsonArray) throws JSONException {
String[] strings = str.split(",");
Stream<String> streamOfStrings = Arrays.stream(str.split(","));
for (String string : strings) {
streamOfStrings.map(string -> {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name", string);
} catch (JSONException e) {
throw new RuntimeException(e);
}
return jsonObject;
}).forEach(jsonArray::put);
}
jsonObject.put("name", string);
private static JSONObject apply(JSONObject item) {
if (item.optString("type").equals("menu")) {
String menuitems = "groups,audios,stickers,podcasts,shopping,lives,videos,games,vk_calls,ads_easy_promote,classifieds"; // vk_pay,events
JSONObject menuItem = new JSONObject();
JSONArray menuItems = new JSONArray();
jsonArray.put(jsonObject);
try {
JSONObject object = new JSONObject();
object.put("count", menuitems.split(",").length);
object.put("items", menuItems);
menuItem.put("type", "menu");
menuItem.put("object", object);
generateMenuItems(menuitems, menuItems);
} catch (JSONException e) {
throw new RuntimeException(e);
}
return menuItem;
} else {
return item;
}
}
}
}