~renatofilho/address-book-app/fix-1511477

« back to all changes in this revision

Viewing changes to src/imports/ABContactListPage.qml

  • Committer: CI Train Bot
  • Author(s): Florian Boucault
  • Date: 2015-10-26 15:05:46 UTC
  • mfrom: (466.1.46 converged_bottom_edge)
  • Revision ID: ci-train-bot@canonical.com-20151026150546-ns9uusgkk5iyh85a
Make bottom edge usable with a mouse.
Make it smarter when more than one column is available. Fixes: #1432696

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
 
1
/*
2
2
 * Copyright (C) 2012-2015 Canonical, Ltd.
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify
14
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 */
16
16
 
17
 
import QtQuick 2.2
 
17
import QtQuick 2.4
18
18
import QtContacts 5.0
19
19
 
20
 
import Ubuntu.Components 1.2
21
 
import Ubuntu.Components.ListItems 1.0 as ListItem
22
 
import Ubuntu.Components.Popups 1.0 as Popups
 
20
import Ubuntu.Components 1.3
 
21
import Ubuntu.Components.ListItems 1.3 as ListItem
 
22
import Ubuntu.Components.Popups 1.3 as Popups
23
23
import Ubuntu.Contacts 0.1 as ContactsUI
24
24
import Ubuntu.Content 1.1 as ContentHub
25
25
 
26
26
import Ubuntu.AddressBook.Base 0.1
27
27
import Ubuntu.AddressBook.ContactShare 0.1
28
28
 
29
 
ContactsUI.PageWithBottomEdge {
 
29
Page {
30
30
    id: mainPage
31
31
    objectName: "contactListPage"
32
32
 
36
36
    property QtObject contactIndex: null
37
37
    property string newPhoneToAdd: ""
38
38
    property alias contactManager: contactList.manager
 
39
    property Page contactViewPage: null
 
40
    property Page contactEditorPage: null
39
41
 
 
42
    readonly property bool bottomEdgePageOpened: bottomEdge.opened && bottomEdge.fullLoaded
40
43
    readonly property bool isEmpty: (contactList.count === 0)
41
44
    readonly property bool allowToQuit: (application.callbackApplication.length > 0)
42
45
    readonly property var contactModel: contactList.listModel ? contactList.listModel : null
55
58
 
56
59
    function createContactWithPhoneNumber(phoneNumber)
57
60
    {
58
 
        var newContact = ContactsJS.createEmptyContact(phoneNumber, mainPage)
59
 
        //WORKAROUND: SKD changes the page header as soon as the page get created
60
 
        // setting active false will avoid that
61
 
        if (bottomEdgeEnabled) {
62
 
            mainPage.showBottomEdgePage(Qt.resolvedUrl("ABContactEditorPage.qml"),
63
 
                                        {model: contactList.listModel,
64
 
                                         contact: newContact,
65
 
                                         active: false,
66
 
                                         enabled: false,
67
 
                                         initialFocusSection: "name"})
68
 
        } else {
69
 
            pageStack.push(Qt.resolvedUrl("ABContactEditorPage.qml"),
70
 
                           {model: contactList.listModel,
71
 
                            contact: newContact,
72
 
                            initialFocusSection: "name"})
73
 
        }
74
 
    }
75
 
 
76
 
    function showContact(contactId)
77
 
    {
78
 
        pageStack.push(Qt.resolvedUrl("ABContactViewPage.qml"),
79
 
                       {model: contactList.listModel, contactId: contactId})
 
61
        var newContact = ContactsJS.createEmptyContact(phoneNumber, mainPage);
 
62
        openEditPage({model: contactList.listModel,
 
63
                      contact: newContact,
 
64
                      initialFocusSection: "name"},
 
65
                     mainPage);
 
66
    }
 
67
 
 
68
    function openEditPage(editPageProperties, sourcePage) {
 
69
        var incubator = pageStack.addPageToNextColumn(sourcePage,
 
70
                                          Qt.resolvedUrl("ABContactEditorPage.qml"),
 
71
                                          editPageProperties);
 
72
        incubator.onStatusChanged = function(status) {
 
73
            if (status == Component.Ready) {
 
74
                mainPage.contactEditorPage = incubator.object;
 
75
            }
 
76
        }
 
77
    }
 
78
 
 
79
    function openViewPage(viewPageProperties) {
 
80
        var incubator = pageStack.addPageToNextColumn(mainPage,
 
81
                                      Qt.resolvedUrl("ABContactViewPage.qml"),
 
82
                                      viewPageProperties);
 
83
        incubator.onStatusChanged = function(status) {
 
84
            if (status == Component.Ready) {
 
85
                contactViewPage = incubator.object;
 
86
            }
 
87
        }
 
88
    }
 
89
 
 
90
    function showContact(contact)
 
91
    {
 
92
        mainPage.state = "default";
 
93
        openViewPage({model: contactList.listModel,
 
94
                      contact: contact});
 
95
    }
 
96
 
 
97
    function showContactWithId(contactId)
 
98
    {
 
99
        openViewPage({model: contactList.listModel,
 
100
                      contactId: contactId});
80
101
    }
81
102
 
82
103
    function addPhoneToContact(contactId, phoneNumber)
83
104
    {
84
 
        pageStack.push(Qt.resolvedUrl("ABContactViewPage.qml"),
85
 
                       {model: contactList.listModel,
86
 
                        contactId: contactId,
87
 
                        addPhoneToContact: phoneNumber})
 
105
        openViewPage({model: contactList.listModel,
 
106
                      contactId: contactId,
 
107
                      addPhoneToContact: phoneNumber});
88
108
    }
89
109
 
90
110
    function importContact(urls)
124
144
        contactList.reset()
125
145
    }
126
146
 
 
147
    function showContactEditorPage(editorPage) {
 
148
        contactList.currentIndex = -1;
 
149
        mainPage.contactEditorPage = editorPage;
 
150
        pageStack.addPageToNextColumn(mainPage, editorPage);
 
151
        editorPage.ready();
 
152
        editorPage.contactSaved.connect(onNewContactSaved);
 
153
    }
 
154
 
 
155
    function onNewContactSaved(contact) {
 
156
        if (pageStack.columns > 1) {
 
157
            showContact(contact);
 
158
        }
 
159
    }
 
160
 
127
161
    title: i18n.tr("Contacts")
128
 
    bottomEdgeTitle: "+"
129
 
    bottomEdgeEnabled: !contactList.isInSelectionMode
130
162
 
131
163
    flickable: null
132
164
    ContactsUI.ContactListView {
133
165
        id: contactList
134
166
        objectName: "contactListView"
135
167
        showImportOptions:  !mainPage.pickMode &&
136
 
                            mainPage.newPhoneToAdd === ""
 
168
                            mainPage.newPhoneToAdd === "" &&
 
169
                            (!mainPage.contactEditorPage || !mainPage.contactEditorPage.active)
137
170
        anchors {
138
171
            top: parent.top
139
172
            left: parent.left
143
176
        filterTerm: searchField.text
144
177
        multiSelectionEnabled: true
145
178
        multipleSelection: (mainPage.pickMode && mainPage.pickMultipleContacts) || !mainPage.pickMode
 
179
        highlightedContact: contactViewPage ? contactViewPage.contact :
 
180
                            contactEditorPage ? contactEditorPage.contact : null
146
181
 
147
182
        onAddContactClicked: mainPage.createContactWithPhoneNumber(label)
148
183
        onAddNewContactClicked: mainPage.createContactWithPhoneNumber(mainPage.newPhoneToAdd)
149
184
 
150
185
        onContactClicked: {
151
 
            mainPage.state = "default"
152
 
            pageStack.push(Qt.resolvedUrl("ABContactViewPage.qml"),
153
 
                           {model: contactList.listModel,
154
 
                            contact: contact})
 
186
            showContact(contact);
155
187
        }
156
188
        onIsInSelectionModeChanged: mainPage.state = isInSelectionMode ? "selection"  : "default"
157
189
        onSelectionCanceled: {
241
273
                Action {
242
274
                    text: i18n.tr("Settings")
243
275
                    iconName: "settings"
244
 
                    onTriggered: pageStack.push(Qt.resolvedUrl("./Settings/SettingsPage.qml"),
245
 
                                                {"contactListModel": contactList.listModel})
 
276
                    onTriggered: pageStack.addPageToNextColumn(mainPage,
 
277
                                                               Qt.resolvedUrl("./Settings/SettingsPage.qml"),
 
278
                                                               {"contactListModel": contactList.listModel})
246
279
                }
247
280
            ]
248
281
            PropertyChanges {
347
380
                actions: selectionState.actions
348
381
            }
349
382
            PropertyChanges {
350
 
                target: mainPage
351
 
                bottomEdgeEnabled: false
352
 
                title: " "
 
383
                target: bottomEdge
 
384
                enabled: false
353
385
            }
354
386
        },
355
387
        PageHeadState {
362
394
            }
363
395
            PropertyChanges {
364
396
                target: mainPage
365
 
                bottomEdgeEnabled: false
366
397
                title: i18n.tr("Add contact")
367
398
            }
368
399
            PropertyChanges {
 
400
                target: bottomEdge
 
401
                enabled: false
 
402
            }
 
403
            PropertyChanges {
369
404
                target: contactList
370
405
                detailToPick: -1
371
406
            }
380
415
                showAddNewButton: true
381
416
            }
382
417
            PropertyChanges {
383
 
                target: mainPage
384
 
                bottomEdgeEnabled: false
 
418
                target: bottomEdge
 
419
                enabled: false
385
420
            }
386
421
        }
387
422
    ]
391
426
        }
392
427
    }
393
428
 
394
 
    // We need to reset the page proprerties in case of the page was created pre-populated,
395
 
    // with phonenumber or contact.
396
 
    onBottomEdgeDismissed: {
397
 
        //WORKAROUND: SKD changes the page header as soon as the page get created
398
 
        // setting active false will avoid that
399
 
        var newContact = ContactsUI.ContactsJS.createEmptyContact("", mainPage)
400
 
        mainPage.setBottomEdgePage(Qt.resolvedUrl("ABContactEditorPage.qml"),
401
 
                                   {model: contactList.listModel,
402
 
                                    contact: newContact,
403
 
                                    active: false,
404
 
                                    enabled: false,
405
 
                                    initialFocusSection: "name"})
406
 
    }
407
 
 
408
429
    KeyboardRectangle {
409
430
        id: keyboard
410
431
    }
420
441
                  !contactList.favouritesIsSelected &&
421
442
                  mainPage.isEmpty &&
422
443
                  (mainPage.newPhoneToAdd === "") &&
423
 
                  !(contactList.filterTerm && contactList.filterTerm !== ""))
 
444
                  !(contactList.filterTerm && contactList.filterTerm !== "")) &&
 
445
                  bottomEdge.visible
424
446
 
425
447
        Behavior on visible {
426
448
            SequentialAnimation {
481
503
            // Share contacts to an application chosen by the user
482
504
            if (!mainPage.pickMode) {
483
505
                contactExporter.dismissBusyDialog()
484
 
                pageStack.push(contactShareComponent,
485
 
                               { contactModel: contactExporter.contactModel,
486
 
                                 contacts: contacts })
 
506
                pageStack.addPageToNextColumn(mainPage,
 
507
                                              contactShareComponent,
 
508
                                              {contactModel: contactExporter.contactModel,
 
509
                                               contacts: contacts })
487
510
            }
488
511
        }
489
512
    }
519
542
            contactList.listModel.importContacts("file://" + TEST_DATA)
520
543
        }
521
544
 
522
 
        mainPage.setBottomEdgePage(Qt.resolvedUrl("ABContactEditorPage.qml"),
523
 
                                   {model: contactList.listModel,
524
 
                                    contact: ContactsUI.ContactsJS.createEmptyContact("", mainPage),
525
 
                                    active: false,
526
 
                                    enabled: false,
527
 
                                    initialFocusSection: "name"})
528
 
 
529
545
        if (pageStack) {
530
546
            pageStack.contactListPage = mainPage
531
547
        }
532
548
    }
 
549
 
 
550
    Component {
 
551
        id: editorPageBottomEdge
 
552
        ABContactEditorPage {
 
553
            backIconName: "down"
 
554
            implicitWidth: mainPage.width
 
555
            implicitHeight: mainPage.height
 
556
            model: contactList.listModel
 
557
            contact: ContactsJS.createEmptyContact("", mainPage)
 
558
            initialFocusSection: "name"
 
559
            enabled: false
 
560
        }
 
561
    }
 
562
 
 
563
    Component {
 
564
        id: emptyContact
 
565
        ContactsUI.ContactDelegate {
 
566
            property Contact contact: Contact {
 
567
                Name {
 
568
                    firstName: i18n.tr("New contact")
 
569
                }
 
570
                Avatar {
 
571
                    imageUrl: "image://theme/contact"
 
572
                }
 
573
            }
 
574
            width: mainPage.width
 
575
        }
 
576
    }
 
577
 
 
578
    BottomEdge {
 
579
        id: bottomEdge
 
580
        objectName: "bottomEdge"
 
581
 
 
582
        property var incubator
 
583
 
 
584
        // FIXME: this is a workaround for the lack of fully asynchronous loading
 
585
        // of Pages in AdaptativePageLayout
 
586
        function createObjectAsynchronously(url, properties, callback) {
 
587
            var component = Qt.createComponent(url, Component.Asynchronous);
 
588
            if (component.status == Component.Ready) {
 
589
                incubateObject(component, properties, callback);
 
590
            } else {
 
591
                component.onStatusChanged.connect(function(status) {
 
592
                    if (status == Component.Ready) {
 
593
                        incubateObject(component, properties, callback);
 
594
                    }
 
595
                });
 
596
            }
 
597
        }
 
598
 
 
599
        function incubateObject(component, properties, callback) {
 
600
            if (component.status == Component.Ready) {
 
601
                incubator = component.incubateObject(null,
 
602
                                                     properties,
 
603
                                                     Qt.Asynchronous);
 
604
                incubator.onStatusChanged = function(status) {
 
605
                    if (status == Component.Ready) {
 
606
                        callback(incubator.object);
 
607
                        incubator = null;
 
608
                    }
 
609
                }
 
610
            }
 
611
        }
 
612
 
 
613
        function loadEditorPage() {
 
614
            var newContact = ContactsJS.createEmptyContact("", mainPage);
 
615
            createObjectAsynchronously(Qt.resolvedUrl("ABContactEditorPage.qml"),
 
616
                                       {model: contactList.listModel,
 
617
                                        contact: newContact,
 
618
                                        initialFocusSection: "name"},
 
619
                                       showContactEditorPage);
 
620
        }
 
621
 
 
622
        anchors.fill: parent
 
623
        contentComponent: pageStack.columns == 1 ? editorPageBottomEdge : emptyContact
 
624
        flickable: contactList
 
625
        iconName: "contact-new"
 
626
        enabled: !contactList.isInSelectionMode
 
627
        backGroundEffectEnabled: pageStack.columns === 1
 
628
 
 
629
        onOpenBegin: {
 
630
            contactList.prepareNewContact = true;
 
631
            contactList.positionViewAtBeginning();
 
632
            if (pageStack.columns > 1) {
 
633
                loadEditorPage();
 
634
            }
 
635
        }
 
636
        onOpenEnd: {
 
637
            bottomEdge.visible = false;
 
638
            contactList.showNewContact = true;
 
639
            if (pageStack.columns <= 1) {
 
640
                showContactEditorPage(bottomEdge.content);
 
641
            }
 
642
        }
 
643
 
 
644
        onClicked: {
 
645
            bottomEdge.open();
 
646
        }
 
647
    }
 
648
 
 
649
    Connections {
 
650
        target: mainPage.contactViewPage
 
651
        onEditContact: {
 
652
            openEditPage(editPageProperties, mainPage.contactViewPage);
 
653
        }
 
654
    }
 
655
 
 
656
    Connections {
 
657
        target: mainPage.contactEditorPage
 
658
        onActiveChanged: {
 
659
            if (!mainPage.contactEditorPage.active) {
 
660
                contactList.prepareNewContact = false;
 
661
                contactList.showNewContact = false;
 
662
                bottomEdge.visible = true;
 
663
                bottomEdge.close();
 
664
            }
 
665
        }
 
666
    }
533
667
}