~majster-pl/+junk/uShopper

« back to all changes in this revision

Viewing changes to .ubuntu-sdk-deploy/backEnd/MainListMODEL.qml

  • Committer: Szymon Waliczek
  • Date: 2015-11-23 21:27:03 UTC
  • Revision ID: majsterrr@gmail.com-20151123212703-2kl0wxd7kojpowfx
initial release, still long way to go... ;)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Szymon Waliczek.
 
3
 *
 
4
 * Authors:
 
5
 *  Szymon Waliczek <majsterrr@gmail.com>
 
6
 *
 
7
 * This file is part of SocketWorld app for Ubuntu Touch
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU Lesser General Public License as published by
 
11
 * the Free Software Foundation; version 3.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public License
 
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
 
 
23
import QtQuick 2.4
 
24
 
 
25
ListModel {
 
26
    id: root
 
27
 
 
28
    property var main_list: []
 
29
 
 
30
 
 
31
    Component.onCompleted: load() //load list
 
32
 
 
33
    //Loading main_list
 
34
    function load() {
 
35
        if (mainShopListDB.contents.mainList) {
 
36
            main_list = JSON.parse(mainShopListDB.contents.mainList)
 
37
            main_shopping_list_MODEL.append(main_list)
 
38
            if (main_list.length == 0){
 
39
                    print("List is empty.")
 
40
            } else {
 
41
                    print("Loading " + main_list.length + " item/s to main_list.")
 
42
            }
 
43
        } else {
 
44
                print("List is empty, nothingk to load.")
 
45
        }
 
46
    }
 
47
 
 
48
 
 
49
 
 
50
 
 
51
    // Adding new List to mainShopListModel
 
52
    function addMainList(list_name, default_color) {
 
53
        var date = new Date()
 
54
        var colorBG = Math.floor((Math.random() * 10) + 1);
 
55
        main_list.push({"name": list_name, "lista": [], "def_color": default_color, "create_date": date.toString(), "last_edit": date.toString()})
 
56
        main_shopping_list_MODEL.clear()
 
57
        main_shopping_list_MODEL.append(main_list)
 
58
        saveMainList()
 
59
    }
 
60
 
 
61
    // edit list in main_list
 
62
    function editListNameAndColor(index, new_name, new_color) {
 
63
        var edited_list = main_list[index]
 
64
        var date = new Date()
 
65
    //    print(main_list.length)
 
66
    //    print(new_color)
 
67
        main_list[index] = {"name": new_name, "lista": main_list[index]['lista'], "def_color": new_color, "create_date": main_list[index].create_date, "last_edit": date.toString()}
 
68
        saveMainList()
 
69
    }
 
70
 
 
71
 
 
72
 
 
73
 
 
74
    // Adding item to the list
 
75
    function addItemToList(item_name, item_name_org) {
 
76
        var edited_list = main_list[currentMainListIndex]['lista']
 
77
        main_list[currentMainListIndex]['lista'].push({"itemName": item_name, "itemNameOrg": item_name_org, "isInBasket": 0})
 
78
        saveMainList()
 
79
        loadCurrentList()
 
80
        print(main_list[currentMainListIndex]['lista'][0].itemName)
 
81
    }
 
82
 
 
83
 
 
84
 
 
85
 
 
86
 
 
87
 
 
88
 
 
89
 
 
90
    // load main_list to main_shopping_list_MODEL
 
91
    function loadMainList() {
 
92
        main_list = JSON.parse(mainShopListDB.contents.mainList)
 
93
        main_shopping_list_MODEL.append(main_list)
 
94
    }
 
95
 
 
96
 
 
97
    // remove list from main_list
 
98
    function removeMainList(index) {
 
99
        var list_name = main_shopping_list_MODEL.get(index).name
 
100
        print("removeing list...  " + list_name)
 
101
        var list = main_list
 
102
        list.splice(index, 1)
 
103
        main_list = list
 
104
        saveMainListSilent()
 
105
    }
 
106
 
 
107
 
 
108
    // remove multiple lists from main_list
 
109
    function removeMultipleMainLists(to_remove) {
 
110
        var new_list = []
 
111
        if(to_remove.length === main_list.length) {
 
112
            main_shopping_list_MODEL.clear()
 
113
        } else {
 
114
            for(var e = 0; e < main_list.length; e++) {
 
115
                if(to_remove.indexOf(e) === -1) {
 
116
                    print(main_list[e].name)
 
117
                    new_list.push(main_list[e])
 
118
                } else {
 
119
                    main_shopping_list_MODEL.remove(e)
 
120
                }
 
121
            }
 
122
 
 
123
        }
 
124
        main_list = new_list
 
125
        saveMainListSilent()
 
126
    }
 
127
 
 
128
 
 
129
 
 
130
    // return list length
 
131
    function mainListLengthInArray() {
 
132
        var list1 = []
 
133
        for(var i=0; i < main_list.length; i++) {
 
134
            list1.push(i)
 
135
        }
 
136
        return list1
 
137
    }
 
138
 
 
139
 
 
140
    // list_name validator
 
141
    function listNameValidator(new_list_name){
 
142
        var list_names = []
 
143
        for(var i=0; i<main_list.length; i++){
 
144
            list_names.push(main_list[i].name.toLowerCase())
 
145
        }
 
146
        if(list_names.indexOf(new_list_name.toLowerCase()) > -1) {
 
147
            return false
 
148
        } else {
 
149
            return true
 
150
        }
 
151
    }
 
152
 
 
153
 
 
154
    // get current date
 
155
    function getCurrentDate() {
 
156
        var date = new Date()
 
157
        var test = Date()
 
158
        return new Date(main_shopping_list_MODEL.get(0).create_date).toLocaleString();
 
159
    }
 
160
 
 
161
 
 
162
 
 
163
 
 
164
    /////////////// LOADING INDIVIDUAL LISTs////////////////
 
165
 
 
166
    function loadCurrentList() {
 
167
//        var curList = []
 
168
//        curList = JSON.parse(main_list[currentMainListIndex]['lista'])
 
169
        currentListMODEL.clear()
 
170
        currentListMODEL.append(main_list[currentMainListIndex]['lista'])
 
171
    }
 
172
 
 
173
 
 
174
 
 
175
 
 
176
 
 
177
    ///// SAVING LIST
 
178
    function saveMainList() {
 
179
        var tempMainList = {}
 
180
        tempMainList = mainShopListDB.contents
 
181
        tempMainList.mainList = JSON.stringify(main_list)
 
182
//        print("++++++++++++++++++++++++")
 
183
//        print(tempMainList.mainList)
 
184
//        print("++++++++++++++++++++++++")
 
185
        mainShopListDB.contents = tempMainList
 
186
        main_shopping_list_MODEL.clear()
 
187
        loadMainList()
 
188
        print("Total Lists: " + main_shopping_list_MODEL.count)
 
189
    }
 
190
 
 
191
 
 
192
    function saveMainListSilent() {
 
193
        var tempMainList = {}
 
194
        tempMainList = mainShopListDB.contents
 
195
        tempMainList.mainList = JSON.stringify(main_list)
 
196
//        print("++++++++++++++++++++++++")
 
197
//        print(tempMainList.mainList)
 
198
//        print("++++++++++++++++++++++++")
 
199
        mainShopListDB.contents = tempMainList
 
200
    }
 
201
 
 
202
 
 
203
}