From be3f4c14e76b8a66921d2f4e2d942968ad4fc730 Mon Sep 17 00:00:00 2001 From: irov Date: Tue, 22 Oct 2024 21:33:11 +0300 Subject: [PATCH] fix AccountService --- .../org/Mengine/Base/MengineActivity.java | 5 +- .../java/org/Mengine/Base/MengineUtils.java | 144 ++++++++++-------- .../AccountService/AccountService.cpp | 37 +++-- 3 files changed, 103 insertions(+), 83 deletions(-) diff --git a/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineActivity.java b/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineActivity.java index fc6767def1..21add5db36 100644 --- a/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineActivity.java +++ b/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineActivity.java @@ -1400,7 +1400,7 @@ public boolean linkingOpenMail(String email, String subject, String body) { return true; } - public void linkingOpenDeleteAccount() { + public boolean linkingOpenDeleteAccount() { MengineLog.logMessage(TAG, "request delete account"); MengineUtils.showAreYouSureAlertDialog(this @@ -1420,8 +1420,11 @@ public void linkingOpenDeleteAccount() { , () -> { //Cancel MengineLog.logMessage(TAG, "delete account [CANCEL]"); } + , 3000 , "Delete Account" , "Click 'YES' will delete all account data. All game progress, virtual goods, and currency will be permanently removed and unrecoverable." ); + + return true; } } diff --git a/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineUtils.java b/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineUtils.java index a09256d1fa..f55c1716aa 100644 --- a/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineUtils.java +++ b/gradle/libraries/Mengine/src/main/java/org/Mengine/Base/MengineUtils.java @@ -18,8 +18,10 @@ import android.os.Build; import android.os.Handler; import android.os.Looper; +import android.text.Layout; import android.text.Spannable; import android.text.SpannableString; +import android.text.style.AlignmentSpan; import android.text.style.StyleSpan; import android.util.DisplayMetrics; import android.view.Display; @@ -546,104 +548,112 @@ public static boolean openUrl(Context context, String url) { } public static void showToast(Context context, String format, Object ... args) { - String message = MengineLog.buildTotalMsg(format, args); + MengineUtils.performOnMainThread(() -> { + String message = MengineLog.buildTotalMsg(format, args); - Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); + Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); - toast.show(); + toast.show(); + }); } public static void showOkAlertDialog(Context context, Runnable ok, String title, String format, Object ... args) { - String message = MengineLog.buildTotalMsg(format, args); + MengineUtils.performOnMainThread(() -> { + String message = MengineLog.buildTotalMsg(format, args); - MengineLog.logMessage(TAG, "show OK alert dialog title: %s message: %s" - , title - , message - ); + MengineLog.logMessage(TAG, "show OK alert dialog title: %s message: %s" + , title + , message + ); - AlertDialog.Builder builder = new AlertDialog.Builder(context); + AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(title); - builder.setMessage(message); - builder.setCancelable(false); - builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - MengineLog.logMessage(TAG, "show OK alert dialog OK clicked"); + builder.setTitle(title); + builder.setMessage(message); + builder.setCancelable(false); + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + MengineLog.logMessage(TAG, "show OK alert dialog OK clicked"); - ok.run(); - dialog.dismiss(); - } - }); + ok.run(); + dialog.dismiss(); + } + }); - AlertDialog alert = builder.create(); + AlertDialog alert = builder.create(); - alert.show(); + alert.show(); + }); } - public static void showAreYouSureAlertDialog(Context context, Runnable yes, Runnable cancel, String title, String format, Object ... args) { - String messageText = MengineLog.buildTotalMsg(format, args); + public static void showAreYouSureAlertDialog(Context context, Runnable yes, Runnable cancel, long delayMillis, String title, String format, Object ... args) { + MengineUtils.performOnMainThread(() -> { + String messageText = MengineLog.buildTotalMsg(format, args); - MengineLog.logMessage(TAG, "show YES|CANCEL alert dialog title: %s message: %s" - , title - , messageText - ); + MengineLog.logMessage(TAG, "show YES|CANCEL alert dialog title: %s message: %s" + , title + , messageText + ); - String AreYouSureText = "\n\nAre you sure?"; + String AreYouSureText = "\n\nAre you sure?"; - SpannableString spannableMessage = new SpannableString(messageText + AreYouSureText); + SpannableString spannableMessage = new SpannableString(messageText + AreYouSureText); - spannableMessage.setSpan(new StyleSpan(Typeface.BOLD), messageText.length(), (messageText + AreYouSureText).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + spannableMessage.setSpan(new StyleSpan(Typeface.BOLD), messageText.length(), (messageText + AreYouSureText).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + spannableMessage.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), messageText.length(), spannableMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - AlertDialog.Builder builder = new AlertDialog.Builder(context); + AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(title); - builder.setMessage(spannableMessage); - builder.setCancelable(false); - builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - MengineLog.logMessage(TAG, "show YES|CANCEL alert dialog YES clicked"); + builder.setTitle(title); + builder.setMessage(spannableMessage); + builder.setCancelable(false); + builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + MengineLog.logMessage(TAG, "show YES|CANCEL alert dialog YES clicked"); - yes.run(); - dialog.dismiss(); - } - }); - builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - MengineLog.logMessage(TAG, "show YES|CANCEL alert dialog CANCEL clicked"); + yes.run(); + dialog.dismiss(); + } + }); + builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + MengineLog.logMessage(TAG, "show YES|CANCEL alert dialog CANCEL clicked"); - cancel.run(); - dialog.dismiss(); - } - }); - - AlertDialog alert = builder.create(); + cancel.run(); + dialog.dismiss(); + } + }); - alert.show(); + AlertDialog alert = builder.create(); - int darker_gray = ContextCompat.getColor(context, android.R.color.darker_gray); + alert.show(); - alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(darker_gray); - alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); - - MengineUtils.performOnMainThreadDelayed(() -> { + int darker_gray = ContextCompat.getColor(context, android.R.color.darker_gray); int holo_red_light = ContextCompat.getColor(context, android.R.color.holo_red_light); - alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(holo_red_light); - alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); - }, 3000); + if (delayMillis > 0) { + alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(darker_gray); + alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); + + MengineUtils.performOnMainThreadDelayed(() -> { + alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(holo_red_light); + alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); + }, delayMillis); + } else { + alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(holo_red_light); + } + }); } public static void finishActivityWithAlertDialog(Activity activity, String format, Object... args) { MengineLog.logErrorException(TAG, format, args); - MengineUtils.performOnMainThreadDelayed(() -> { - MengineUtils.showOkAlertDialog(activity, () -> { - activity.finishAndRemoveTask(); - }, "Mengine", format, args); - }, 0); + MengineUtils.showOkAlertDialog(activity, () -> { + activity.finishAndRemoveTask(); + }, "Mengine", format, args); } public static void sleep(long millis) { diff --git a/src/Services/AccountService/AccountService.cpp b/src/Services/AccountService/AccountService.cpp index 6bb8a46405..1317e5479b 100644 --- a/src/Services/AccountService/AccountService.cpp +++ b/src/Services/AccountService/AccountService.cpp @@ -281,6 +281,19 @@ namespace Mengine ////////////////////////////////////////////////////////////////////////// bool AccountService::deleteAccount( const ConstString & _accountId ) { + if( m_currentAccountId.empty() == false ) + { + if( m_currentAccountId == _accountId ) + { + this->unselectCurrentAccount_(); + } + } + + if( m_accountProvider != nullptr ) + { + m_accountProvider->onDeleteAccount( _accountId ); + } + AccountPtr account = m_accounts.erase( _accountId ); if( account == nullptr ) @@ -292,14 +305,6 @@ namespace Mengine return false; } - if( m_currentAccountId.empty() == false ) - { - if( m_currentAccountId == _accountId ) - { - this->unselectCurrentAccount_(); - } - } - LOGGER_INFO( "account", "delete account '%s' UID '%.20s'" , account->getId().c_str() , account->getUID().data @@ -307,13 +312,10 @@ namespace Mengine account->finalize(); - if( m_accountProvider != nullptr ) - { - m_accountProvider->onDeleteAccount( _accountId ); - } - m_invalidateAccounts = true; + this->saveAccounts(); + return true; } ////////////////////////////////////////////////////////////////////////// @@ -371,7 +373,9 @@ namespace Mengine return false; } - if( this->deleteAccount( m_currentAccountId ) == false ) + ConstString deleteAccountId = m_currentAccountId; + + if( this->deleteAccount( deleteAccountId ) == false ) { return false; } @@ -598,7 +602,10 @@ namespace Mengine continue; } - validAccount = account; + if( accountId != m_globalAccountId ) + { + validAccount = account; + } } if( this->hasAccount( selectAccountId ) == false )