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 12, 2024
1 parent 5506da7 commit ebdc11a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 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 @@ -5,9 +5,14 @@

package org.chromium.chrome.browser.settings;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
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,48 @@ 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
17 changes: 17 additions & 0 deletions android/java/res/layout/brave_leo_clear_history_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
style="@style/AlertDialogContent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/brave_leo_clear_history_confirmation" />

</LinearLayout>
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

#include "src/chrome/browser/flags/android/chrome_feature_list.cc"
#undef kForceWebContentsDarkMode
#undef BRAVE_AI_CHAT_FLAG
#undef BRAVE_AI_CHAT_FLAGS

namespace chrome {
namespace android {
Expand Down

0 comments on commit ebdc11a

Please sign in to comment.