This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkivy_test.py
139 lines (105 loc) · 4.58 KB
/
kivy_test.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
"""
test area for the kivy components we need (aswell as to test that are underlying nostr client components
do what we need) going to write a encrypted nostr chat app
we'll need
address/profile search - for adding people to chat to
relay details - whre chats are to be posted/ we'll subscribe to get replies
basic encrypted type 4 messages
1 to 1 chat
further dev
wrapped encrypt as clust
group chat via mutiple posts, using clust it might be possible that only orignator know who everyone is...
"""
import time
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton
# importing all necessary modules
# like MDApp, MDLabel Screen, MDTextField
# and MDRectangleFlatButton
from kivymd.app import MDApp
from kivy.clock import Clock
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton
from kivy.lang.builder import Builder
from kivymd.uix.list import OneLineListItem, TwoLineListItem, \
OneLineAvatarIconListItem, TwoLineAvatarIconListItem, ImageLeftWidget
from kivy.uix.screenmanager import ScreenManager
from datetime import datetime, timedelta
from nostr.ident import ProfileEventHandler, ProfileList, Profile
from nostr.client.client import Client
from nostr.client.event_handlers import PersistEventHandler
from db.db import SQLiteDatabase
from kivy_components.screens import SearchContactScreen, MessageScreen
from nostr.event import Event
from nostr.util import util_funcs
from nostr.client.persist import SQLLiteEventStore
DB = SQLiteDatabase('/home/shaun/.nostrpy/nostr-client.db')
class Demo(MDApp):
def __init__(self, **kargs):
self._scr_man = None
self._con_sel_screen = None
self._client = None
self._store = SQLLiteEventStore(DB.file)
self._c_profile = Profile.load_from_db(DB,'firedragon888')
# get current profiles and track any changes
def my_update(evt_p, old_p):
pass
self._peh = ProfileEventHandler(DB, on_update=my_update)
super().__init__(**kargs)
def build(self):
self._scr_man = ScreenManager()
self._con_sel_screen = SearchContactScreen(name='contact_select', contact_selected=self.contact_selected)
# self._con_sel_screen.set_contact_selected(self.contact_selected)
self._scr_man.add_widget(self._con_sel_screen)
def ret_contact_search():
self._scr_man.current = 'contact_select'
def do_message(text, to_profile):
n_event = Event(kind=Event.KIND_TEXT_NOTE,
content=text,
pub_key=self._c_profile.public_key,
tags=[
'#p', to_profile.public_key
])
n_event.sign(self._c_profile.private_key)
self._client.publish(n_event)
self._msg_screen = MessageScreen(name='message_screen',
on_back=ret_contact_search,
on_message=do_message)
self._scr_man.add_widget(self._msg_screen)
self._con_sel_screen.set_profiles(self._peh.profiles)
return self._scr_man
def contact_selected(self, p: Profile):
self._msg_screen.set_converstion(p)
self._scr_man.current = 'message_screen'
def connect(self, the_client):
self._client = the_client
while not self._con_sel_screen:
time.sleep(0.1)
# TODO: to store add method to get the latest tick for event matching filter
the_client.subscribe(handlers=self._peh, filters={
'kinds': Event.KIND_META
# 'since': util_funcs.date_as_ticks(datetime.now()-timedelta(days=1))
})
the_client.subscribe(handlers=PersistEventHandler(self._store), filters={
'kinds': Event.KIND_TEXT_NOTE,
'since': util_funcs.date_as_ticks(datetime.now()-timedelta(days=1))
})
def test_select_contact():
"""
at most basic the select contact screen needs to have a search and allow selection of a single contact
contacts should be autosync to most recent profiles (use nostr.profiles)
"""
# s = Store('/home/shaun/.nostrpy/nostr-client.db')
# s.destroy()
# s.create()
my_app = Demo()
c = Client('ws://localhost:8082/', on_connect=my_app.connect).start()
my_app.run()
c.end()
if __name__ == "__main__":
test_select_contact()