-
Notifications
You must be signed in to change notification settings - Fork 1
/
explore.py
282 lines (234 loc) · 8.61 KB
/
explore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import kivy
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivymd.uix.card import MDCard
from kivymd.uix.list import IRightBodyTouch
from kivy.clock import Clock
from kivymd.utils import asynckivy
from helpers import DrawerList, ContentNavigationDrawer, ItemDrawer
import firebase_admin
from firebase_admin import firestore
import db
import random
db = firestore.client()
kivy.require("2.0.0")
explore_KV = '''
<ItemDrawer>:
theme_text_color: "Custom"
on_release: self.parent.set_color_item(self)
on_press: lambda x: self.on_press()
IconLeftWidget:
id: icon
icon: root.icon
theme_text_color: "Custom"
text_color: root.text_color
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
spacing: "8dp"
ScrollView:
DrawerList:
ItemDrawer:
icon: "earth"
text: "World Wide"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 1"
ItemDrawer:
icon: "map-marker-outline"
text: "My location"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"
<UserCard>:
orientation: "vertical"
padding: '15dp'
spacing: '20dp'
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: 1,None
size: "280dp", "210dp"
radius: [15,15,15,15]
TwoLineAvatarIconListItem:
id: username
text: "nilayc"
secondary_text: 'happy'
font_style: 'H6'
size_hint_y: None
IconLeftWidget:
icon: "account-plus-outline"
on_release:
root.add_friend()
Container:
id: container
MDIcon:
icon: "fire"
theme_text_color: 'Custom'
text_color: (234.0/255,35.0/255,0.0/255,1)
MDLabel:
id: streak
text: '5'
halign: 'center'
MDLabel:
id: bio
text: 'Hello, im nilay. I take photos. I like smart and funny people. I like talking about countries. text me!'
theme_text_color: 'Secondary'
size_hint_y: .8
size_hint_x: .9
pos_hint: {"center_x": .5, "center_y": 1}
MDLabel:
id: tag
text: 'tag1 tag2 tag3 tag4 tag5'
theme_text_color: 'Secondary'
size_hint_x: .9
pos_hint: {"center_x": .5, "center_y": 1}
Screen:
MDToolbar:
id: toolbar
halign: 'center'
title: 'explore'
md_bg_color: .2, .2, .2, 1
specific_text_color: 1, 1, 1, 1
pos_hint: {"top": 1}
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
NavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
Screen:
name: 'scr 1'
MDScrollViewRefreshLayout:
id: refresh_layout
refresh_callback: app.refresh_callback
root_layout: root
size_hint: (1, .90)
MDGridLayout:
id: layout
cols: 2
spacing: 50
adaptive_height: True
padding: 50
md_bg_color : (39.0/255,41.0/255,43.0/255,1)
Screen:
name: 'scr 2'
MDScrollViewRefreshLayout:
id: refresh_layout2
refresh_callback: app.refresh_callback_loc
root_layout: root
size_hint: (1, .90)
MDGridLayout:
id: layout2
cols: 2
spacing: 50
adaptive_height: True
padding: 50
md_bg_color : (39.0/255,41.0/255,43.0/255,1)
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
'''
class UserCard(MDCard):
def add_friend(self):
close_button = MDFlatButton(text="close",on_release= self.close_dialog)
more_button = MDFlatButton(text="more")
self.dialog = MDDialog(title="Message Sent", text='Keep Exploring!', size_hint=(0.7,1),buttons=[close_button,more_button])
self.dialog.open()
print("Friend Added")
#print(self.username.text)
def close_dialog(self,obj):
self.dialog.dismiss()
class Container(IRightBodyTouch, MDBoxLayout):
adaptive_width = True
class Explore(MDApp):
screen = None
def build(self):
#theme
self.theme_cls.primary_palette = "Gray"
self.theme_cls.primary_hue ='200'
self.theme_cls.theme_style="Light"
self.explorescreen = Builder.load_string(explore_KV)
self.username = "nilay"
self.user_ref = db.collection(u'users').document(self.username)
self.user = self.get_by_username(self.username)
self.friend_match_arr = self.tag_match(self.user['tags'])
if len(self.friend_match_arr) < 3:
self.friend_match_arr += self.get_all()
self.set_list()
self.set_list_loc()
return self.explorescreen
def set_list(self):
self.friend_match_arr = self.tag_match(self.user['tags'])
if len(self.friend_match_arr) < 3:
self.friend_match_arr += self.get_all()
random.shuffle(self.friend_match_arr)
async def set_list():
for u in self.friend_match_arr:
if u['username']==self.username:
self.friend_match_arr.remove(u)
continue
await asynckivy.sleep(0)
userbox = UserCard()
userbox.ids.username.text = u['username']
userbox.ids.username.secondary_text = u['status']
userbox.ids.bio.text = u['bio']
userbox.ids.streak.text = str(u['streak'])
userbox.ids.tag.text = ', '.join(u['tags'])
self.explorescreen.ids.layout.add_widget(userbox)
asynckivy.start(set_list())
def set_list_loc(self):
async def set_list_loc():
for i in range(20):
await asynckivy.sleep(0)
userbox = UserCard()
userbox.ids.username.text = f'usernameloc {i}'
self.explorescreen.ids.layout2.add_widget(userbox)
asynckivy.start(set_list_loc())
def refresh_callback(self, *args,**kwargs):
'''A method that updates the state of your application
while the spinner remains on the screen.'''
def refresh_callback(interval):
self.explorescreen.ids.layout.clear_widgets()
self.set_list()
self.explorescreen.ids.refresh_layout.refresh_done()
self.tick = 0
Clock.schedule_once(refresh_callback, 1)
def refresh_callback_loc(self, *args,**kwargs):
'''A method that updates the state of your application
while the spinner remains on the screen.'''
def refresh_callback_loc(interval):
self.explorescreen.ids.layout2.clear_widgets()
self.set_list_loc()
self.explorescreen.ids.refresh_layout2.refresh_done()
self.tick = 0
Clock.schedule_once(refresh_callback_loc, 1)
# get all the data in a collection
def get_all(self,collection="users"):
collection_ref = db.collection(f'{collection}')
docs = collection_ref.stream()
arr = []
for doc in docs:
print(f'{doc.id} => {doc.to_dict()}')
arr.append(doc.to_dict())
return arr
# people with similar interests
def tag_match(self,tag=[]):
collection_ref = db.collection(u'users')
query = collection_ref.where(u'tags', u'array_contains_any', tag)
result = query.stream()
arr = []
for r in result:
# print(f'{r.id} => {r.to_dict()}')
arr.append(r.to_dict())
return arr
def get_by_username(self, username):
doc = db.collection(u'users').where(u'username', u'==',username).stream()
for d in doc:
return d.to_dict()
if __name__ == '__main__':
Explore().run()