refactor: change PrivateChatPopup to use the new ContactListAndSearch
This commit is contained in:
parent
c42bd1ea78
commit
303e6e9893
|
@ -7,61 +7,6 @@ import "../../../../shared/status"
|
||||||
import "./"
|
import "./"
|
||||||
|
|
||||||
ModalPopup {
|
ModalPopup {
|
||||||
property string validationError: ""
|
|
||||||
|
|
||||||
property string pubKey : "";
|
|
||||||
property string ensUsername : "";
|
|
||||||
|
|
||||||
function validate() {
|
|
||||||
if (!Utils.isChatKey(chatKey.text) && !Utils.isValidETHNamePrefix(chatKey.text)) {
|
|
||||||
//% "Enter a valid chat key or ENS username"
|
|
||||||
validationError = qsTrId("enter-a-valid-chat-key-or-ens-username");
|
|
||||||
pubKey = ""
|
|
||||||
ensUsername.text = "";
|
|
||||||
} else if (profileModel.profile.pubKey === chatKey.text) {
|
|
||||||
//% "Can't chat with yourself"
|
|
||||||
validationError = qsTrId("can-t-chat-with-yourself");
|
|
||||||
} else {
|
|
||||||
validationError = ""
|
|
||||||
}
|
|
||||||
return validationError === ""
|
|
||||||
}
|
|
||||||
|
|
||||||
property var resolveENS: Backpressure.debounce(popup, 500, function (ensName){
|
|
||||||
noContactsRect.visible = false
|
|
||||||
searchResults.loading = true
|
|
||||||
searchResults.showProfileNotFoundMessage = false
|
|
||||||
chatsModel.resolveENS(ensName)
|
|
||||||
});
|
|
||||||
|
|
||||||
function onKeyReleased(){
|
|
||||||
searchResults.pubKey = ""
|
|
||||||
if (!validate()) {
|
|
||||||
searchResults.showProfileNotFoundMessage = false
|
|
||||||
noContactsRect.visible = false
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
chatKey.text = chatKey.text.trim();
|
|
||||||
|
|
||||||
if (Utils.isChatKey(chatKey.text)){
|
|
||||||
pubKey = chatKey.text;
|
|
||||||
if (!profileModel.contacts.isAdded(pubKey)) {
|
|
||||||
searchResults.username = utilsModel.generateAlias(pubKey)
|
|
||||||
searchResults.userAlias = Utils.compactAddress(pubKey, 4)
|
|
||||||
searchResults.pubKey = pubKey
|
|
||||||
}
|
|
||||||
noContactsRect.visible = false
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Qt.callLater(resolveENS, chatKey.text)
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateAndJoin(pk, ensName) {
|
|
||||||
if (!validate() || pk.trim() === "" || validationError !== "") return;
|
|
||||||
doJoin(pk, ensName)
|
|
||||||
}
|
|
||||||
function doJoin(pk, ensName) {
|
function doJoin(pk, ensName) {
|
||||||
if(Utils.isChatKey(pk)){
|
if(Utils.isChatKey(pk)){
|
||||||
chatsModel.joinChat(pk, Constants.chatTypeOneToOne);
|
chatsModel.joinChat(pk, Constants.chatTypeOneToOne);
|
||||||
|
@ -77,116 +22,28 @@ ModalPopup {
|
||||||
title: qsTrId("new-chat")
|
title: qsTrId("new-chat")
|
||||||
|
|
||||||
onOpened: {
|
onOpened: {
|
||||||
chatKey.text = "";
|
contactFieldAndList.chatKey.text = ""
|
||||||
pubKey = "";
|
contactFieldAndList.pubKey = ""
|
||||||
ensUsername.text = "";
|
contactFieldAndList.ensUsername = ""
|
||||||
chatKey.forceActiveFocus(Qt.MouseFocusReason)
|
contactFieldAndList.chatKey.forceActiveFocus(Qt.MouseFocusReason)
|
||||||
existingContacts.visible = profileModel.contacts.list.hasAddedContacts()
|
contactFieldAndList.existingContacts.visible = profileModel.contacts.list.hasAddedContacts()
|
||||||
noContactsRect.visible = !existingContacts.visible
|
contactFieldAndList.noContactsRect.visible = !contactFieldAndList.existingContacts.visible
|
||||||
}
|
}
|
||||||
|
|
||||||
Input {
|
ContactsListAndSearch {
|
||||||
id: chatKey
|
id: contactFieldAndList
|
||||||
//% "Enter ENS username or chat key"
|
anchors.top: parent.top
|
||||||
placeholderText: qsTrId("enter-contact-code")
|
anchors.bottom: parent.bottom
|
||||||
Keys.onEnterPressed: validateAndJoin(popup.pubKey, chatKey.text)
|
onUserClicked: function (isContact, pubKey, ensName) {
|
||||||
Keys.onReturnPressed: validateAndJoin(popup.pubKey, chatKey.text)
|
if(Utils.isChatKey(pubKey)){
|
||||||
Keys.onReleased: {
|
chatsModel.joinChat(pubKey, Constants.chatTypeOneToOne);
|
||||||
onKeyReleased();
|
} else {
|
||||||
}
|
chatsModel.joinChatWithENS(pubKey, ensName);
|
||||||
textField.anchors.rightMargin: clearBtn.width + Style.current.padding + 2
|
|
||||||
|
|
||||||
Connections {
|
|
||||||
target: chatsModel
|
|
||||||
onEnsWasResolved: {
|
|
||||||
if(chatKey.text == ""){
|
|
||||||
ensUsername.text = "";
|
|
||||||
pubKey = "";
|
|
||||||
} else if(resolvedPubKey == ""){
|
|
||||||
ensUsername.text = "";
|
|
||||||
searchResults.pubKey = pubKey = "";
|
|
||||||
searchResults.showProfileNotFoundMessage = true
|
|
||||||
} else {
|
|
||||||
if (profileModel.profile.pubKey === resolvedPubKey) {
|
|
||||||
//% "Can't chat with yourself"
|
|
||||||
popup.validationError = qsTrId("can-t-chat-with-yourself");
|
|
||||||
} else {
|
|
||||||
searchResults.username = chatsModel.formatENSUsername(chatKey.text)
|
|
||||||
let userAlias = utilsModel.generateAlias(resolvedPubKey)
|
|
||||||
userAlias = userAlias.length > 20 ? userAlias.substring(0, 19) + "..." : userAlias
|
|
||||||
searchResults.userAlias = userAlias + " • " + Utils.compactAddress(resolvedPubKey, 4)
|
|
||||||
searchResults.pubKey = pubKey = resolvedPubKey;
|
|
||||||
}
|
|
||||||
searchResults.showProfileNotFoundMessage = false
|
|
||||||
}
|
|
||||||
searchResults.loading = false;
|
|
||||||
noContactsRect.visible = pubKey === "" && ensUsername.text === "" && !profileModel.contacts.list.hasAddedContacts() && !profileNotFoundMessage.visible
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
StatusIconButton {
|
popup.close();
|
||||||
id: clearBtn
|
|
||||||
icon.name: "close-icon"
|
|
||||||
type: "secondary"
|
|
||||||
visible: chatKey.text !== ""
|
|
||||||
icon.width: 14
|
|
||||||
icon.height: 14
|
|
||||||
width: 14
|
|
||||||
height: 14
|
|
||||||
anchors.right: parent.right
|
|
||||||
anchors.rightMargin: Style.current.padding
|
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
|
||||||
onClicked: {
|
|
||||||
chatKey.text = ""
|
|
||||||
chatKey.forceActiveFocus(Qt.MouseFocusReason)
|
|
||||||
searchResults.showProfileNotFoundMessage = false
|
|
||||||
searchResults.pubKey = popup.pubKey = ""
|
|
||||||
noContactsRect.visible = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
|
||||||
id: validationErrorMessage
|
|
||||||
text: popup.validationError
|
|
||||||
visible: popup.validationError !== ""
|
|
||||||
font.pixelSize: 13
|
|
||||||
color: Style.current.danger
|
|
||||||
anchors.top: chatKey.bottom
|
|
||||||
anchors.topMargin: Style.current.smallPadding
|
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
|
||||||
}
|
|
||||||
|
|
||||||
ExistingContacts {
|
|
||||||
id: existingContacts
|
|
||||||
anchors.topMargin: this.height > 0 ? Style.current.xlPadding : 0
|
|
||||||
anchors.top: chatKey.bottom
|
|
||||||
filterText: chatKey.text
|
|
||||||
onContactClicked: function (contact) {
|
|
||||||
doJoin(contact.pubKey, profileModel.contacts.addedContacts.userName(contact.pubKey, contact.name))
|
|
||||||
}
|
|
||||||
expanded: !searchResults.loading && popup.pubKey === "" && !searchResults.showProfileNotFoundMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
SearchResults {
|
|
||||||
id: searchResults
|
|
||||||
anchors.top: existingContacts.visible ? existingContacts.bottom : chatKey.bottom
|
|
||||||
anchors.topMargin: Style.current.padding
|
|
||||||
hasExistingContacts: existingContacts.visible
|
|
||||||
loading: false
|
|
||||||
|
|
||||||
onResultClicked: validateAndJoin(popup.pubKey, chatKey.text)
|
|
||||||
onAddToContactsButtonClicked: profileModel.contacts.addContact(popup.pubKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
NoFriendsRectangle {
|
|
||||||
id: noContactsRect
|
|
||||||
anchors.top: chatKey.bottom
|
|
||||||
anchors.topMargin: Style.current.xlPadding * 3
|
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*##^##
|
/*##^##
|
||||||
|
|
Loading…
Reference in New Issue