Skip to content

Commit

Permalink
Continue implementing online themes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Docile-Alligator committed Nov 7, 2024
1 parent 0f0ef4c commit 7e448ff
Showing 1 changed file with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ml.docilealligator.infinityforreddit.activities;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
Expand Down Expand Up @@ -38,6 +37,7 @@
import ml.docilealligator.infinityforreddit.customtheme.OnlineCustomThemeMetadata;
import ml.docilealligator.infinityforreddit.databinding.ActivityCustomizeThemeBinding;
import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils;
import retrofit2.Call;
import retrofit2.Callback;
Expand Down Expand Up @@ -253,28 +253,53 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.save_theme_options_title)
//.setMessage(R.string.save_theme_options_message)
.setSingleChoiceItems(R.array.save_theme_options, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
option[0] = which;
}
})
.setSingleChoiceItems(R.array.save_theme_options, 0, (dialog, which) -> option[0] = which)
.setPositiveButton(R.string.ok, (dialogInterface, which) -> {
switch (option[0]) {
case 0:
saveThemeLocally(customTheme);
break;
case 1:
saveThemeOnline(customTheme);
saveThemeOnline(customTheme, false);
break;
case 2:
saveThemeLocally(customTheme);
saveThemeOnline(customTheme);
saveThemeOnline(customTheme, false);
break;
}
})
.setNegativeButton(R.string.cancel, null)
.show();
} else {
/*// This custom theme is from the server but not uploaded by the current user, or it is local
final int[] option = {0};
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.save_theme_options_title)
//.setMessage(R.string.save_theme_options_message)
.setSingleChoiceItems(R.array.save_theme_options_anonymous_included, 0, (dialog, which) -> option[0] = which)
.setPositiveButton(R.string.ok, (dialogInterface, which) -> {
switch (option[0]) {
case 0:
saveThemeLocally(customTheme);
break;
case 1:
saveThemeOnline(customTheme, false);
break;
case 2:
saveThemeOnline(customTheme, true);
break;
case 3:
saveThemeLocally(customTheme);
saveThemeOnline(customTheme, false);
break;
case 4:
saveThemeLocally(customTheme);
saveThemeOnline(customTheme, true);
break;
}
})
.setNegativeButton(R.string.cancel, null)
.show();*/
saveThemeLocally(customTheme);
}
}
Expand All @@ -295,12 +320,25 @@ private void saveThemeLocally(CustomTheme customTheme) {
});
}

private void saveThemeOnline(CustomTheme customTheme) {
onlineCustomThemesRetrofit.create(OnlineCustomThemeAPI.class).modifyTheme(
onlineCustomThemeMetadata.id, customTheme.name,
customTheme.getJSONModel(),
('#' + Integer.toHexString(customTheme.colorPrimary)).toUpperCase()
).enqueue(new Callback<>() {
private void saveThemeOnline(CustomTheme customTheme, boolean anonymous) {
Call<String> request;
// TODO server access token
if (onlineCustomThemeMetadata != null) {
request = onlineCustomThemesRetrofit.create(ServerAPI.class).modifyTheme(
APIUtils.getServerHeader("", accountName, anonymous),
onlineCustomThemeMetadata.id,
customTheme.name,
customTheme.getJSONModel()
);
} else {
request = onlineCustomThemesRetrofit.create(ServerAPI.class).createTheme(
APIUtils.getServerHeader("", accountName, anonymous),
customTheme.name,
customTheme.getJSONModel()
);
}

request.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
Expand Down

0 comments on commit 7e448ff

Please sign in to comment.