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-20 10:02:18 +00:00
|
|
|
from screens.base import ProtectedScreen,BaseScreen
|
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
|
2020-08-11 13:42:55 +00:00
|
|
|
from kivy.app import App
|
2020-08-17 22:06:31 +00:00
|
|
|
from threading import Thread
|
2020-08-19 14:07:12 +00:00
|
|
|
import asyncio
|
2020-08-22 11:16:50 +00:00
|
|
|
from misc import *
|
2020-08-22 13:27:37 +00:00
|
|
|
from kivy.core.window import Window
|
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 15:00:50 +00:00
|
|
|
# self.log('PostCard() got data: '+str(data))
|
2020-08-12 19:23:23 +00:00
|
|
|
self.author = data.get('author','[Anonymous]')
|
2020-08-22 11:16:50 +00:00
|
|
|
self.recipient = data.get('to_name','')
|
2020-08-17 22:06:31 +00:00
|
|
|
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 ''
|
2020-08-11 13:42:55 +00:00
|
|
|
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-22 13:27:37 +00:00
|
|
|
|
2020-08-22 13:52:27 +00:00
|
|
|
|
|
|
|
# minwidth = 400
|
|
|
|
# maxwidth = 800
|
|
|
|
# abouts = int(Window.size[0]/1.5)
|
|
|
|
# if abouts < minwidth: self.width=f'{minwidth}sp'
|
|
|
|
# if abouts > maxwidth: self.width=f'{maxwidth}sp'
|
|
|
|
# self.width=f'{abouts}sp'
|
2020-08-22 13:27:37 +00:00
|
|
|
|
2020-08-10 16:37:42 +00:00
|
|
|
|
2020-08-19 15:00:50 +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-17 22:06:31 +00:00
|
|
|
|
2020-08-10 16:37:42 +00:00
|
|
|
# pieces
|
2020-08-22 00:27:20 +00:00
|
|
|
self.author_section_layout = author_section_layout = PostAuthorLayout()
|
|
|
|
self.author_label = author_label = PostAuthorLabel(text='@'+self.author)
|
2020-08-22 11:16:50 +00:00
|
|
|
self.author_label.font_name='assets/overpass-mono-semibold.otf'
|
|
|
|
if self.recipient:
|
|
|
|
self.author_label.text+='\n[size=14sp][i]to @'+self.recipient+'[/i][/size]'
|
|
|
|
self.author_label.markup=True
|
2020-08-22 00:27:20 +00:00
|
|
|
self.author_label.font_size = '18sp'
|
|
|
|
self.author_avatar = author_avatar = PostAuthorAvatar(source='assets/avatar.jpg') #self.img_src)
|
|
|
|
self.author_section_layout.add_widget(author_avatar)
|
|
|
|
self.author_section_layout.add_widget(author_label)
|
2020-08-22 11:16:50 +00:00
|
|
|
# self.author_section_layout.add_widget(MDSeparator(height='1sp',size_hint=(None,None)))
|
|
|
|
|
|
|
|
# self.recipient_label = author_label = PostAuthorLabel(text='--> @'+self.recipient)
|
|
|
|
# self.recipient_label.font_size = '14sp'
|
|
|
|
# self.author_label.add_widget(self.recipient_label)
|
|
|
|
|
2020-08-12 19:23:23 +00:00
|
|
|
|
|
|
|
# timestamp
|
|
|
|
timestr=''
|
2020-08-17 22:06:31 +00:00
|
|
|
#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")
|
2020-08-22 11:16:50 +00:00
|
|
|
#log('timestr: '+timestr)
|
|
|
|
self.timestamp_label=PostTimestampLabel(text=timestr)
|
|
|
|
self.timestamp_label.font_size='14sp'
|
|
|
|
author_section_layout.add_widget(self.timestamp_label)
|
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)
|
|
|
|
|
2020-08-11 13:42:55 +00:00
|
|
|
if self.cache_img_src:
|
2020-08-11 13:01:48 +00:00
|
|
|
image_layout = PostImageLayout()
|
2020-08-11 13:42:55 +00:00
|
|
|
self.image = image = PostImage(source=self.cache_img_src)
|
2020-08-21 13:12:13 +00:00
|
|
|
image.height = '300sp'
|
2020-08-11 13:01:48 +00:00
|
|
|
image_layout.add_widget(image)
|
2020-08-21 13:12:13 +00:00
|
|
|
image_layout.height='300sp'
|
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'))
|
2020-08-17 22:06:31 +00:00
|
|
|
#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
|
|
|
|
|
2020-08-21 13:12:13 +00:00
|
|
|
scroller.size = ('300sp','%ssp' % estimate_height())
|
2020-08-11 13:01:48 +00:00
|
|
|
|
|
|
|
|
2020-08-21 13:12:13 +00:00
|
|
|
# scroller.bind(size=('300sp',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)
|
2020-08-17 22:06:31 +00:00
|
|
|
if self.cache_img_src and (not os.path.exists(self.cache_img_src) or not os.stat(self.cache_img_src).st_size):
|
2020-08-20 10:49:46 +00:00
|
|
|
async def do_download_later():
|
2020-08-19 10:29:56 +00:00
|
|
|
self.log('downloading...')
|
2020-08-20 10:46:57 +00:00
|
|
|
await self.app.download(self.img_id, self.cache_img_src)
|
2020-08-17 22:06:31 +00:00
|
|
|
self.image.reload()
|
2020-08-20 10:49:22 +00:00
|
|
|
return True
|
2020-08-17 22:06:31 +00:00
|
|
|
|
|
|
|
#self.open_dialog('posting')
|
2020-08-18 17:25:15 +00:00
|
|
|
#Thread(target=do_download).start()
|
2020-08-20 10:49:46 +00:00
|
|
|
asyncio.create_task(do_download_later())
|
2020-08-18 17:25:15 +00:00
|
|
|
|
2020-08-17 22:06:31 +00:00
|
|
|
|
2020-08-11 13:42:55 +00:00
|
|
|
@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!')
|
2020-08-11 13:42:55 +00:00
|
|
|
self.image.reload()
|
|
|
|
self.img_loaded=True
|
|
|
|
|
2020-08-10 16:37:42 +00:00
|
|
|
#####
|
|
|
|
|
|
|
|
|
2020-08-20 10:02:18 +00:00
|
|
|
class FeedScreen(BaseScreen):
|
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 19:33:25 +00:00
|
|
|
super().on_pre_enter()
|
|
|
|
|
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
|
|
|
|
|
|
|
|