2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-09 13:10:44 +00:00
Comrad/app/screens/feed/feed.py

213 lines
7.2 KiB
Python
Raw Normal View History

2020-08-10 16:37:42 +00:00
from kivymd.uix.label import MDLabel
from kivy.uix.gridlayout import GridLayout
2020-08-17 20:40:48 +00:00
from kivy.uix.image import AsyncImage, Image
2020-08-10 16:37:42 +00:00
from kivymd.uix.boxlayout import MDBoxLayout
2020-08-11 13:01:48 +00:00
from kivymd.uix.card import MDCard, MDSeparator
from kivy.uix.scrollview import ScrollView
2020-08-10 16:37:42 +00:00
from screens.base import ProtectedScreen
2020-08-12 07:40:44 +00:00
from kivy.properties import ListProperty
2020-08-12 19:23:23 +00:00
import os,time
from datetime import datetime
from kivy.app import App
from threading import Thread
2020-08-19 14:07:12 +00:00
import asyncio
2020-08-12 19:23:23 +00:00
2020-08-10 16:37:42 +00:00
### POST CODE
class PostTitle(MDLabel): pass
class PostGridLayout(GridLayout): pass
class PostImage(AsyncImage): pass
2020-08-17 20:40:48 +00:00
# class PostImage(CoreImage)
class PostImageBytes(Image): pass
2020-08-10 16:37:42 +00:00
class PostContent(MDLabel):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.bind(width=lambda s, w: s.setter('text_size')(s, (w, None)))
self.bind(texture_size=self.setter('size'))
self.font_name='assets/overpass-mono-regular.otf'
#pass
class PostAuthorLayout(MDBoxLayout): pass
2020-08-11 13:01:48 +00:00
class PostImageLayout(MDBoxLayout): pass
2020-08-12 19:23:23 +00:00
2020-08-10 16:37:42 +00:00
class PostAuthorLabel(MDLabel):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.bind(width=lambda s, w: s.setter('text_size')(s, (w, None)))
self.bind(texture_size=self.setter('size'))
self.font_name='assets/overpass-mono-regular.otf'
pass
2020-08-12 19:23:23 +00:00
class PostTimestampLabel(MDLabel):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.bind(width=lambda s, w: s.setter('text_size')(s, (w, None)))
self.bind(texture_size=self.setter('size'))
self.font_name='assets/overpass-mono-regular.otf'
2020-08-10 16:37:42 +00:00
class PostAuthorAvatar(AsyncImage): pass
2020-08-11 13:01:48 +00:00
class PostLayout(MDBoxLayout): pass
class PostScrollView(ScrollView): pass
2020-08-10 16:37:42 +00:00
class PostCard(MDCard):
2020-08-19 10:29:56 +00:00
@property
def app(self): return App.get_running_app()
def log(self,*x): self.app.log(*x)
2020-08-12 19:23:23 +00:00
def __init__(self, data):
2020-08-10 16:37:42 +00:00
super().__init__()
2020-08-19 10:29:56 +00:00
self.log('PostCard() got data: '+str(data))
2020-08-12 19:23:23 +00:00
self.author = data.get('author','[Anonymous]')
self.img_id = data.get('file_id','')
self.img_ext = data.get('file_ext','')
self.img_src=self.img_id[:3]+'/'+self.img_id[3:]+'.'+self.img_ext if self.img_id else ''
self.cache_img_src = os.path.join('cache',self.img_src) if self.img_src else ''
self.img_loaded = os.path.exists(self.cache_img_src)
2020-08-12 19:23:23 +00:00
self.content = data.get('content','')
self.timestamp = data.get('timestamp',None)
2020-08-10 16:37:42 +00:00
self.bind(minimum_height=self.setter('height'))
2020-08-19 10:29:56 +00:00
self.log('PostCard.img_id =',self.img_id)
self.log('PostCard.img_ext =',self.img_ext)
self.log('PostCard.img_src =',self.img_src)
self.log('PostCard.cache_img_src =',self.cache_img_src)
2020-08-10 16:37:42 +00:00
# pieces
author_section_layout = PostAuthorLayout()
author_label = PostAuthorLabel(text=self.author)
author_label.font_size = '28dp'
2020-08-11 13:01:48 +00:00
author_avatar = PostAuthorAvatar(source='avatar.jpg') #self.img_src)
2020-08-10 16:37:42 +00:00
author_section_layout.add_widget(author_avatar)
author_section_layout.add_widget(author_label)
2020-08-12 19:23:23 +00:00
# timestamp
timestr=''
#log(self.timestamp)
2020-08-12 19:23:23 +00:00
if self.timestamp:
dt_object = datetime.fromtimestamp(self.timestamp)
timestr = dt_object.strftime("%-d %b %Y %H:%M")
#log('timestr: '+timestr)
2020-08-12 19:23:23 +00:00
author_section_layout.add_widget(PostTimestampLabel(text=timestr))
2020-08-10 16:37:42 +00:00
# author_section_layout.add_widget(author_avatar)
2020-08-11 13:01:48 +00:00
# self.add_widget(author_section_layout)
if self.cache_img_src:
2020-08-11 13:01:48 +00:00
image_layout = PostImageLayout()
self.image = image = PostImage(source=self.cache_img_src)
2020-08-11 13:01:48 +00:00
image.height = '300dp'
image_layout.add_widget(image)
image_layout.height='300dp'
2020-08-19 10:29:56 +00:00
# self.log(image.image_ratio)
2020-08-10 16:37:42 +00:00
2020-08-12 19:53:59 +00:00
self.post_content = PostContent(text=self.content)
2020-08-10 16:37:42 +00:00
2020-08-11 13:01:48 +00:00
# post_layout = PostGridLayout()
2020-08-10 16:37:42 +00:00
#content = PostContent()
# add to screen
2020-08-11 13:01:48 +00:00
# self.add_widget(title)
# post_layout.add_widget(author_section_layout)
# post_layout.add_widget(image_layout)
# post_layout.add_widget(content)
2020-08-12 19:23:23 +00:00
self.scroller = scroller = PostScrollView()
2020-08-11 13:01:48 +00:00
self.add_widget(author_section_layout)
# self.add_widget(MDLabel(text='hello'))
#log('img_src ' + str(bool(self.img_src)))
2020-08-11 13:01:48 +00:00
if self.img_src: self.add_widget(image_layout)
def estimate_height(minlen=100,maxlen=500):
num_chars = len(self.content)
# num_lines = num_chars
height = num_chars*1.1
if height>maxlen: height=maxlen
if height<minlen: height=minlen
return height
scroller.size = ('300dp','%sdp' % estimate_height())
# scroller.bind(size=('300dp',scroller.setter('height'))
2020-08-12 19:53:59 +00:00
scroller.add_widget(self.post_content)
2020-08-11 13:01:48 +00:00
self.add_widget(scroller)
# self.add_widget(post_layout)
2020-08-10 16:37:42 +00:00
2020-08-19 10:29:56 +00:00
# self.log('?????',self.cache_img_src, os.path.exists(self.cache_img_src), os.stat(self.cache_img_src).st_size)
if self.cache_img_src and (not os.path.exists(self.cache_img_src) or not os.stat(self.cache_img_src).st_size):
def do_download():
2020-08-19 10:29:56 +00:00
self.log('downloading...')
self.app.download(self.img_id, self.cache_img_src)
self.image.reload()
#self.open_dialog('posting')
2020-08-18 17:25:15 +00:00
#Thread(target=do_download).start()
do_download()
@property
def app(self):
return App.get_running_app()
def load_image(self):
if not self.img_src: return
if self.img_loaded: return
# otherwise load image...
self.app.get_image(self.img_src)
2020-08-19 10:29:56 +00:00
self.log('done getting image!')
self.image.reload()
self.img_loaded=True
2020-08-10 16:37:42 +00:00
#####
class FeedScreen(ProtectedScreen):
2020-08-12 07:40:44 +00:00
posts = ListProperty()
2020-08-12 06:49:14 +00:00
def on_pre_enter(self):
2020-08-19 14:07:12 +00:00
async def go():
# self.log('ids:' +str(self.ids.post_carousel.ids))
for post in self.posts:
self.ids.post_carousel.remove_widget(post)
2020-08-12 07:40:44 +00:00
2020-08-19 14:07:12 +00:00
i=0
lim=25
posts=await self.app.get_posts()
for i,post in enumerate(reversed(posts)):
#if ln.startswith('@') or ln.startswith('RT '): continue
#i+=1
if i>lim: break
#post = Post(title=f'Marx Zuckerberg', content=ln.strip())
self.log('???')
post_obj = PostCard(post)
self.posts.append(post_obj)
self.ids.post_carousel.add_widget(post_obj)
asyncio.create_task(go())
2020-08-12 07:40:44 +00:00
def on_pre_enter_test(self):
2020-08-10 16:37:42 +00:00
i=0
lim=5
with open('tweets.txt') as f:
for ln in f:
if ln.startswith('@') or ln.startswith('RT '): continue
i+=1
if i>lim: break
#post = Post(title=f'Marx Zuckerberg', content=ln.strip())
post = PostCard(
author='Marx Zuckerberg',
title='',
img_src='avatar.jpg',
content=ln.strip())
print(post)
self.ids.post_carousel.add_widget(post)
2020-08-11 13:01:48 +00:00