2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-17 21:25:37 +00:00
Comrad/client/main.py

167 lines
4.7 KiB
Python
Raw Normal View History

2020-08-03 10:25:58 +00:00
from kivy.uix.screenmanager import Screen,ScreenManager
from kivymd.app import MDApp
2020-08-03 15:48:47 +00:00
from kivymd.uix.button import MDFillRoundFlatButton, MDIconButton
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.screen import MDScreen
2020-08-03 10:25:58 +00:00
from kivy.lang import Builder
2020-08-03 15:48:47 +00:00
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager
from kivy.properties import ObjectProperty,ListProperty
import time
from collections import OrderedDict
from functools import partial
from kivy.uix.screenmanager import NoTransition
from kivymd.uix.label import MDLabel
from kivy.uix.widget import Widget
2020-08-03 16:29:03 +00:00
from kivymd.uix.list import OneLineListItem
from kivymd.uix.card import MDCard, MDSeparator
2020-08-03 20:16:30 +00:00
from kivy.uix.gridlayout import GridLayout
from kivy.metrics import dp
from kivy.properties import NumericProperty
2020-08-03 21:20:38 +00:00
from kivymd.uix.list import * #MDList, ILeftBody, IRightBody, ThreeLineAvatarListItem, TwoLineAvatarListItem, BaseListItem, ImageLeftWidget
from kivy.uix.image import Image, AsyncImage
2020-08-06 08:46:51 +00:00
import requests,json
2020-08-03 10:25:58 +00:00
2020-08-03 15:48:47 +00:00
root = None
app = None
2020-08-03 10:25:58 +00:00
2020-08-03 15:48:47 +00:00
def log(x):
with open('log.txt','a+') as of:
of.write(str(x)+'\n')
2020-08-03 10:25:58 +00:00
2020-08-03 15:48:47 +00:00
class MyLayout(BoxLayout):
scr_mngr = ObjectProperty(None)
2020-08-06 08:46:51 +00:00
2020-08-03 15:48:47 +00:00
def change_screen(self, screen, *args):
self.scr_mngr.current = screen
2020-08-03 20:16:30 +00:00
2020-08-06 08:46:51 +00:00
2020-08-03 15:48:47 +00:00
class MyLabel(MDLabel):
def __init__(self, *args, **kwargs):
2020-08-03 20:16:30 +00:00
super().__init__(*args, **kwargs)
self.theme_text_color='Custom'
self.text_color=(1,0,0,1)
self.pos_hint = {'center_y': 0.5}
self.halign='center'
self.height=self.texture_size[1]
for k,v in kwargs.items(): setattr(self,k,v)
2020-08-03 21:20:38 +00:00
class ContactPhoto(ILeftBody, AsyncImage):
pass
2020-08-03 20:16:30 +00:00
2020-08-05 09:58:31 +00:00
class PostCard(MDCard):
def __init__(self, title = None, img_src = None, content = None):
super().__init__()
self.orientation="vertical"
self.padding="8dp"
self.size_hint=(0.9, 0.9)
# self.md_bg_color=(1,0,0,1)
self.pos_hint = {"center_x": .5, "center_y": .5}
2020-08-03 21:20:38 +00:00
2020-08-05 09:58:31 +00:00
if title:
sep = MDSeparator()
sep.height='25dp'
self.add_widget(sep)
title = MDLabel(text=title)
# title.theme_text_color="Secondary"
title.size_hint_y=None
title.height=title.texture_size[1]
title.font_style='H5'
title.halign='center'
self.add_widget(title)
# spacing?
sep = MDSeparator()
sep.height='25dp'
self.add_widget(sep)
if img_src:
image = AsyncImage(source=img_src)
self.add_widget(image)
if content:
content=MDLabel(text=content)
content.pos_hint={'center_y':1}
content.font_style='Body1'
self.add_widget(content)
2020-08-03 16:29:03 +00:00
2020-08-06 08:46:51 +00:00
class ProtectedScreen(MDScreen):
def on_pre_enter(self):
global app
if app.logged_on==False:
app.root.change_screen('login')
class WelcomeScreen(MDScreen): pass
class LoginScreen(MDScreen): pass
class PeopleScreen(ProtectedScreen): pass
class EventsScreen(ProtectedScreen): pass
class MessagesScreen(ProtectedScreen): pass
class NotificationsScreen(ProtectedScreen): pass
2020-08-03 10:25:58 +00:00
2020-08-06 08:46:51 +00:00
class FeedScreen(ProtectedScreen):
2020-08-03 16:29:03 +00:00
def on_enter(self):
2020-08-05 09:58:31 +00:00
i=0
lim=5
2020-08-03 20:44:53 +00:00
with open('tweets.txt') as f:
2020-08-05 09:58:31 +00:00
for ln in f:
if ln.startswith('@') or ln.startswith('RT '): continue
i+=1
2020-08-03 20:44:53 +00:00
if i>lim: break
2020-08-05 09:58:31 +00:00
#post = Post(title=f'Marx Zuckerberg', content=ln.strip())
post = PostCard(title='Marx Zuckerberg',img_src='avatar.jpg',content=ln.strip())
print(post)
root.ids.post_carousel.add_widget(post)
2020-08-06 08:46:51 +00:00
2020-08-03 15:48:47 +00:00
2020-08-03 10:25:58 +00:00
class MainApp(MDApp):
2020-08-03 15:48:47 +00:00
title = 'Gyre'
2020-08-06 08:46:51 +00:00
api = 'http://localhost:5555/api'
logged_on=False
2020-08-03 10:25:58 +00:00
def build(self):
2020-08-03 16:29:03 +00:00
global app,root
2020-08-03 15:48:47 +00:00
app = self
2020-08-03 16:29:03 +00:00
self.root = root = Builder.load_file('main.kv')
2020-08-06 08:46:51 +00:00
self.root.change_screen('login')
2020-08-03 16:29:03 +00:00
return self.root
2020-08-03 10:25:58 +00:00
2020-08-06 08:46:51 +00:00
def login(self,un,pw):
url = self.api+'/login'
res = requests.post(url, json={'name':un, 'passkey':pw})
if res.status_code==200:
self.logged_on=True
self.root.change_screen('welcome')
else:
self.root.ids.login_status.text=res.text
def register(self,un,pw):
url = self.api+'/register'
res = requests.post(url, json={'name':un, 'passkey':pw})
if res.status_code==200:
self.logged_on=True
self.root.change_screen('welcome')
else:
self.root.ids.login_status.text=res.text
2020-08-03 10:25:58 +00:00
if __name__ == '__main__':
App = MainApp()
App.run()