You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Comrad/client/main.py

178 lines
5.1 KiB
Python

4 years ago
from kivy.uix.screenmanager import Screen,ScreenManager
from kivymd.app import MDApp
4 years ago
from kivymd.uix.button import MDFillRoundFlatButton, MDIconButton
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.screen import MDScreen
4 years ago
from kivy.lang import Builder
4 years ago
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
4 years ago
from kivymd.uix.list import OneLineListItem
from kivymd.uix.card import MDCard, MDSeparator
from kivymd.uix.boxlayout import MDBoxLayout
4 years ago
from kivy.uix.gridlayout import GridLayout
from kivy.metrics import dp
from kivy.properties import NumericProperty
4 years ago
from kivymd.uix.list import * #MDList, ILeftBody, IRightBody, ThreeLineAvatarListItem, TwoLineAvatarListItem, BaseListItem, ImageLeftWidget
from kivy.uix.image import Image, AsyncImage
4 years ago
import requests,json
from kivy.storage.jsonstore import JsonStore
from kivy.core.window import Window
Window.size = (640, 1136) #(2.65 * 200, 5.45 * 200)
4 years ago
4 years ago
4 years ago
root = None
app = None
4 years ago
4 years ago
def log(x):
with open('log.txt','a+') as of:
of.write(str(x)+'\n')
4 years ago
class MyLayout(MDBoxLayout):
4 years ago
scr_mngr = ObjectProperty(None)
4 years ago
4 years ago
def change_screen(self, screen, *args):
self.scr_mngr.current = screen
4 years ago
class MyBoxLayout(MDBoxLayout): pass
4 years ago
class MyLabel(MDLabel): pass
4 years ago
4 years ago
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}
4 years ago
4 years ago
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)
4 years ago
4 years ago
class ProtectedScreen(MDScreen):
def on_pre_enter(self):
global app
if not app.is_logged_in():
4 years ago
app.root.change_screen('login')
class WelcomeScreen(ProtectedScreen): pass
4 years ago
class LoginScreen(MDScreen): pass
class PeopleScreen(ProtectedScreen): pass
class EventsScreen(ProtectedScreen): pass
class MessagesScreen(ProtectedScreen): pass
class NotificationsScreen(ProtectedScreen): pass
4 years ago
4 years ago
class FeedScreen(ProtectedScreen):
4 years ago
def on_enter(self):
4 years ago
i=0
lim=5
4 years ago
with open('tweets.txt') as f:
4 years ago
for ln in f:
if ln.startswith('@') or ln.startswith('RT '): continue
i+=1
4 years ago
if i>lim: break
4 years ago
#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)
4 years ago
4 years ago
4 years ago
class MainApp(MDApp):
title = 'Komrade'
4 years ago
api = 'http://localhost:5555/api'
logged_in=False
store = JsonStore('komrade.json')
login_expiry = 60 * 60 * 24 * 7 # once a week
#login_expiry = 5 # 5 seconds
4 years ago
def build(self):
4 years ago
global app,root
4 years ago
app = self
4 years ago
self.root = root = Builder.load_file('main.kv')
if not self.is_logged_in():
self.root.change_screen('login')
else:
self.root.change_screen('welcome')
4 years ago
return self.root
4 years ago
def is_logged_in(self):
if self.logged_in: return True
if not self.store.exists('user'): return False
if self.store.get('user')['logged_in']:
if time.time() - self.store.get('user')['logged_in_when'] < self.login_expiry:
self.logged_in=True
return True
return False
def do_login(self):
self.logged_in=True
self.store.put('user',logged_in=True,logged_in_when=time.time())
self.root.change_screen('welcome')
4 years ago
4 years ago
def login(self,un,pw):
url = self.api+'/login'
res = requests.post(url, json={'name':un, 'passkey':pw})
if res.status_code==200:
self.do_login()
4 years ago
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.do_login()
4 years ago
else:
self.root.ids.login_status.text=res.text
4 years ago
if __name__ == '__main__':
App = MainApp()
App.run()