-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
499 lines (359 loc) · 10.7 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.base import EventLoop
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivymd.toast import toast
from kivymd.uix.toolbar import MDTopAppBar
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.card import MDCard
from kivymd.utils.set_bars_colors import set_bars_colors
from kivymd.uix.textfield import MDTextField
from kivy.uix.screenmanager import WipeTransition,FadeTransition, ScreenManager
from kivymd.uix.bottomsheet import MDCustomBottomSheet
from kivy.core.clipboard import Clipboard
try:
import webbrowser
except:
pass
try:
from mysql import Database
except:
pass
try:
import shelve
except:
pass
try:
from datetime import datetime
except:
pass
try:
import random
except:
pass
try:
from plyer import notification
except:
pass
try:
import sqlite3
except:
pass
try:
from sqlite3 import Error
except:
pass
# database class
def Notify(title,msg):
notification.notify(title=title,message=msg)
def alert(txt):
toast(gravity=80, y=150, text=str(txt))
class MyBs(MDBoxLayout):
pass
class TransparentBar(MDTopAppBar):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.md_bg_color = 1,1,1, .05
self.elevation = 0
#self.theme_cls.theme_style = "Dark"
class PwdInput(MDBoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def toggle(self, item):
if item.password == True:
item.password = False
else:
item.password = True
class IconText(MDBoxLayout):
pass
class NoteCard(MDCard):
pass
class NoteViewer(MDCard):
pass
class MyScreenManager(ScreenManager):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.transition = FadeTransition()
self.transition.duration = 0.5
class MDTextFieldWithTarget(MDTextField):
pass
class NoteApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Teal"
self.theme_cls.primary_hue = "400"
self.theme_cls.material_style = "M2"
return Builder.load_file("app.kv")
def on_start(self):
print(dir(self.root))
#self.storage_access = False
self.path = "/data/data/org.test.notes/files/app/"
#alert("Starting")
#Notify("Ready", "Ready")
try:
from android.permissions import request_permissions, Permission
request_permissions([Permission.WRITE_EXTERNAL_STORAGE])
request_permissions([Permission.READ_EXTERNAL_STORAGE])
except:
alert("Permission request error")
try:
self.db = Database("MyNotes")
except:
pass
#self.root.ids.sm.current = "changepwd"
try:
#alert("setting bottomnav transition")
self.root.ids.bottomnav.transition = WipeTransition
#alert("setting bottomnav duration")
self.root.ids.bottomnav.transition_duration = 0.6
except:
#alert("setting bottom nav failed")
pass
#self.show_notes
set_bars_colors(
(0,0,0,.5),
(0,0,0,.5),
"Light"
)
#create database connection
try:
self.db.create_table("Notes", "id INTEGER PRIMARY KEY,", "note_title CHAR,", "note_body TEXT,", "added CHAR,", "img CHAR")
except:
alert("Error occured in Database")
try:
#alert("checking notes zero")
if len(self.db.select_all("Notes")) == 0:
self.root.ids.sm2.current = "nonoteview"
else:
#alert("switch to noteview")
self.root.ids.sm2.current = "noteview"
try:
#alert("show notes")
self.show_notes()
except:
pass
except:
#alert("check if notes 0 change screen failed")
pass
#self.root.ids.sm2.current = "noteview"
try:
"""alert("importing shelve file")
import shelve
alert("creating shelve file")"""
self.store = shelve.open("AppConfig")
except:
alert("unknown error")
try:
#alert("changing bar1 to m3")
self.root.ids.bar1.theme_cls.material_style = "M3"
except:
#alert("couldnt changed bar1 to M3")
pass
try:
#alert("creating exit dialog")
self.exit_dialog = MDDialog(
title="Do you want to leave", buttons=[
MDFlatButton(text="Yes", on_press=lambda x: self.get_running_app().stop()),
MDFlatButton(text="No", on_press=lambda x: self.exit_dialog.dismiss())])
except:
#alert("couldnt create exit dialog")
pass
try:
#alert("binding keyboard")
EventLoop.window.bind(on_keyboard=self.hook)
except:
#alert("couldnt bind keyboard to hook function")
pass
self.images = ["a.jpg","b.jpg","c.jpg","d.jpg","e.jpg","f.jpg","g.jpeg"]
#self.show_notes()
def confirm_delete(self, note_title,id):
self.deldialog = MDDialog(auto_dismiss=False,
title=f"""Do you want to delete this note titled : {note_title}""",
buttons=[MDFlatButton(text="Yes", on_press=lambda x: self.remove_note(id)),
MDFlatButton(text="No", on_press=lambda x: self.deldialog.dismiss())]
)
self.deldialog.open()
def remove_note(self, id):
self.db.delete_specific("Notes", ["id", int(id)])
self.deldialog.dismiss()
self.show_notes()
#toast("Note Deleted")
def show_notes(self):
Notes = self.db.select_all("Notes")
self.root.ids.rv.data = []
nice_note = ""
for note in Notes:
#print(note)
limit = 50
if len(note[1]) >= limit:
nice_note = note[1][0:limit] + "..."
else:
nice_note = note[1]
nice_note = nice_note.title()
self.root.ids.rv.data.append(
{"viewclass":"NoteCard",
"title": nice_note,
"date": note[3],
"source":note[4],
"pid":str(note[0]),
"body":note[2],
"actual": note[1]
}
)
if len(self.db.select_all("Notes")) == 0:
self.root.ids.sm2.current = "nonoteview"
else:
self.root.ids.sm2.current = "noteview"
note_num = len(self.db.select_all("Notes"))
self.root.ids.note_count.text = f"Available Notes : {note_num}"
def hook(self, *args):
if args[1] == 27 or args[1] == "27":
if self.root.ids.sm.current == "Login":
self.exit_dialog.open()
elif self.root.ids.sm.current == "Main":
self.exit_dialog.open()
elif self.root.ids.sm.current == "Opennote":
self.root.ids.sm.current = "Main"
elif self.root.ids.sm.current == "edit":
self.root.ids.sm.current = "Main"
elif self.root.ids.sm.current == "changepwd":
self.root.ids.sm.current = "Main"
return True
def set_login_or_login(self, name, pwd):
if name == "" or len(name)== 0 or len(pwd) == 0 or pwd == "":
return True
#if name != self.store["name"]:
#alert("check your input")
# return True
try:
if name == self.store["name"] and pwd == self.store["pwd"]:
self.root.ids.sm.current = "Main"
else:
try:
alert("password or username incorrect")
except:
pass
except:
try:
self.store["name"] = name
self.store["pwd"] = pwd
alert("password and username set")
Notify("Username and Password Set","You may now login")
except:
if self.storage_access == False:
try:
alert("Storage Permission Needed")
except:
pass
else:
alert("Couldn't set password")
self.root.ids.username.text = ""
self.root.ids.password.ids.mtf.text = ""
def save_note(self, title,body):
#alert(str("%s : %s"%(title.text,body.text)))
time_obj = datetime.now()
date_and_time = time_obj.strftime("%d %b %Y | %H:%M")
img = random.choice(self.images)
id = 0
all_ids = self.db.get_column("Notes", "id")
try:
if all_ids == 0:
id = 0
else:
id = max(all_ids)[0] +1
except:
id = 0
if title.text == "" or title.text == " ":
alert("Title cannot be empty")
return True
txt = str(body.text)
#alert(txt)
self.db.insert("Notes", [(id, str(title.text), txt, date_and_time, self.path+img)])
if len(self.db.select_all("Notes")) == 0:
self.root.ids.sm2.current = "nonoteview"
else:
self.root.ids.sm2.current = "noteview"
self.show_notes()
alert("Note Saved")
self.root.ids.bottomnav.switch_tab("view")
self.root.ids.notebody.text = ""
self.root.ids.notetitle.text = ""
def delnote(self, id,title):
id = int(id)
self.confirm_delete(title, id)
def show_content(self, title, body, date,actual):
self.root.ids.nv.title = actual
self.root.ids.nv.date = date
if body == "":
self.root.ids.nv.content = "No Content"
else:
outp = body
self.root.ids.nv.content = outp.replace("\\n","\n")
self.root.ids.sm.current = "Opennote"
def edit_note(self, id,title,body):
self.root.ids.edititle.text = str(title)
#alert(body)
self.root.ids.editbody.text = body.replace("\\n", "\n")
self.root.ids.editbody.target = str(id)
self.root.ids.sm.current = "edit"
def save_changes(self, newtitle, newbody, targetid):
if newtitle == "" or newtitle== " ":
alert("Title cannot be empty")
return True
try:
self.db.update("Notes", ("id", int(targetid)), [("note_title",newtitle), ("note_body",newbody)])
alert("Changes Saved")
except:
alert("Failed to alter note")
self.show_notes()
self.root.ids.sm.current = "Main"
def changepwd(self, old_pwd, new_pwd, conf_pwd):
##print((item.ids.mtf.text))
if old_pwd.ids.mtf.text == "" or new_pwd.ids.mtf.text == "" or conf_pwd.ids.mtf.text == "":
alert("Please Fill In All The Fields")
return True
else:
if old_pwd.ids.mtf.text == self.store["pwd"] and new_pwd.ids.mtf.text == conf_pwd.ids.mtf.text:
self.store["pwd"] = new_pwd.ids.mtf.text
alert("Password Changed Successfully")
Notify("Password Changed Successfully", "You have successfully changed your login password")
self.root.ids.sm.current = "Login"
else:
alert("Please check your input and try again")
def clear_notes(self):
self.delalldialog = MDDialog(auto_dismiss=False,
title="Do you want to clear all notes",
buttons=[MDFlatButton(text="Yes", on_press=lambda x: self.clear()),
MDFlatButton(text="No", on_press=lambda x: self.delalldialog.dismiss())]
)
self.delalldialog.open()
def clear(self):
try:
self.db.delete_all("Notes")
alert("All Notes Cleared")
except:
alert("Deletion Failed")
finally:
self.show_notes()
self.delalldialog.dismiss()
def show_credit(self):
try:
self.bs = MDCustomBottomSheet(screen=MyBs())
self.bs.open()
except:
pass
def whats(self):
try:
webbrowser.open("https://wa.me/+2349079515229")
except:
alert("""Could'nt open whatsapp, please open it manually""")
def copy_note(self, txt):
try:
txt = txt.replace("\\n", "\n")
Clipboard.copy(txt)
alert("Content copied to clipboard")
except:
alert("Failed to copy content")
if __name__ == "__main__":
NoteApp().run()