Skip to content

Commit

Permalink
[AI Chat]: Add confirmation dialog for clearing history
Browse files Browse the repository at this point in the history
  • Loading branch information
fallaciousreasoning committed Dec 11, 2024
1 parent 5506da7 commit 15a58e4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ brave_java_resources = [
"java/res/layout/brave_custom_tabs_toolbar.xml",
"java/res/layout/brave_dialog_preference.xml",
"java/res/layout/brave_exit_confirmation.xml",
"java/res/layout/brave_leo_clear_history_dialog.xml",
"java/res/layout/brave_leo_reset_dialog.xml",
"java/res/layout/brave_news_card_menu.xml",
"java/res/layout/brave_news_load_new_content.xml",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import android.os.Bundle;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import androidx.appcompat.app.AlertDialog;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
Expand Down Expand Up @@ -43,6 +48,7 @@ public class BraveLeoPreferences extends BravePreferenceFragment
private static final String PREF_DEFAULT_MODEL = "default_model";

private final ObservableSupplierImpl<String> mPageTitle = new ObservableSupplierImpl<>();
private ChromeSwitchPreference mHistory;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Expand Down Expand Up @@ -72,13 +78,13 @@ public void onCreate(Bundle savedInstanceState) {

Preference history = findPreference(PREF_HISTORY);
if (history != null) {
history.setOnPreferenceChangeListener(this);
if (history instanceof ChromeSwitchPreference) {
((ChromeSwitchPreference) history)
.setChecked(BraveLeoPrefUtils.getIsHistoryEnabled());
mHistory = (ChromeSwitchPreference) history;
mHistory.setOnPreferenceChangeListener(this);
mHistory.setChecked(BraveLeoPrefUtils.getIsHistoryEnabled());
mHistory.setVisible(ChromeFeatureList.isEnabled(BraveFeatureList.AI_CHAT_HISTORY));
}
}
history.setVisible(ChromeFeatureList.isEnabled(BraveFeatureList.AI_CHAT_HISTORY));

BraveLeoUtils.verifySubscription(
(subscriptionActive) -> {
Expand Down Expand Up @@ -159,15 +165,44 @@ private void checkLinkPurchase() {
});
}

private void showConfirmClearHistoryDialog() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.brave_leo_clear_history_dialog, null);

DialogInterface.OnClickListener onClickListener = (dialog, button) -> {
if (button == AlertDialog.BUTTON_POSITIVE) {
BraveLeoPrefUtils.setIsHistoryEnabled(false);
mHistory.setChecked(false);
} else {
dialog.dismiss();
}
};

AlertDialog.Builder alert = new AlertDialog.Builder(getContext(), R.style.ThemeOverlay_BrowserUI_AlertDialog);
AlertDialog alertDialog = alert.setTitle(R.string.leo_clear_history_title)
.setView(view)
.setPositiveButton(R.string.brave_leo_confirm_text, onClickListener)
.setNegativeButton(R.string.cancel, onClickListener)
.create();
alertDialog.getDelegate().setHandleNativeActionModesEnabled(false);
alertDialog.show();
}

@Override
public boolean onPreferenceChange(@NonNull Preference preference, Object o) {
String key = preference.getKey();
boolean enabled = (boolean) o;
if (PREF_AUTOCOMPLETE.equals(key)) {
ChromeSharedPreferences.getInstance()
.writeBoolean(BravePreferenceKeys.BRAVE_LEO_AUTOCOMPLETE, (boolean) o);
.writeBoolean(BravePreferenceKeys.BRAVE_LEO_AUTOCOMPLETE, enabled);
}
if (PREF_HISTORY.equals(key)) {
BraveLeoPrefUtils.setIsHistoryEnabled((boolean) o);
if (enabled) {
BraveLeoPrefUtils.setIsHistoryEnabled(enabled);
} else {
showConfirmClearHistoryDialog();
return false;
}
}

return true;
Expand Down
6 changes: 6 additions & 0 deletions browser/ui/android/strings/android_brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -3700,6 +3700,12 @@ If you don't accept this request, VPN will not reconnect and your internet conne
<message name="IDS_BRAVE_LEO_RESET_CONFIRMATION" desc="Title for preference to reset Leo.">
Resetting the Leo assistant will require you to opt-in to use Leo in the future and will also clear your chat history. Clearing your chat history will delete all your previous conversations with Leo. This action cannot be undone.
</message>
<message name="IDS_LEO_CLEAR_HISTORY_TITLE" desc="Title for confirmation dialog to clear Leo history.">
Clear chat history
</message>
<message name="IDS_BRAVE_LEO_CLEAR_HISTORY_CONFIRMATION" desc="Confirmation text for clearing Leo history.">
Disabling chat history will delete all your previous conversations with Leo. This action cannot be undone.
</message>
<message name="IDS_BRAVE_LEO_CONFIRM_TEXT" desc="Text for Leo confirm button text.">
Confirm
</message>
Expand Down

0 comments on commit 15a58e4

Please sign in to comment.