2020-09-22 19:34:03 +00:00
import os , sys ; sys . path . append ( os . path . abspath ( os . path . join ( os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' ) ) , ' .. ' ) ) )
2020-09-29 13:09:56 +00:00
from comrad import *
2020-09-22 19:34:03 +00:00
2020-08-20 11:48:26 +00:00
from screens . base import ProtectedScreen , BaseScreen
2020-08-10 19:44:34 +00:00
from plyer import filechooser
2020-08-22 00:27:20 +00:00
from kivy . uix . button import Button
2020-08-22 17:51:09 +00:00
from kivymd . uix . button import *
2020-08-12 19:23:23 +00:00
from kivymd . uix . label import MDLabel
from kivymd . uix . textfield import MDTextField
from kivymd . uix . boxlayout import MDBoxLayout
2020-08-21 21:15:20 +00:00
from kivymd . uix . stacklayout import MDStackLayout
from kivymd . uix . button import MDRectangleFlatButton , MDIconButton , MDRaisedButton , MDFillRoundFlatButton , MDRoundFlatIconButton
2020-08-10 20:38:00 +00:00
from kivy . properties import ListProperty , ObjectProperty
2020-08-10 19:44:34 +00:00
from kivy . app import App
2020-08-10 20:38:00 +00:00
from screens . feed . feed import *
2020-08-12 19:23:23 +00:00
import os , time , threading
from threading import Thread
from kivymd . uix . dialog import MDDialog
2020-08-17 20:40:48 +00:00
from kivy . core . image import Image as CoreImage
2020-08-21 21:15:20 +00:00
from kivymd . uix . gridlayout import MDGridLayout
2020-08-20 10:59:53 +00:00
import io , shutil , asyncio
2020-08-21 21:15:20 +00:00
from kivymd . uix . chip import MDChip
from main import rgb , COLOR_TEXT , COLOR_ACCENT , COLOR_CARD , COLOR_INACTIVE , COLOR_ACTIVE
from misc import *
from kivy . animation import Animation
from kivy . lang import Builder
from kivy . metrics import dp
2020-08-22 00:27:20 +00:00
from kivymd . uix . dropdownitem import MDDropDownItem
from kivymd . uix . menu import MDDropdownMenu
2020-08-21 21:15:20 +00:00
from kivy . properties import (
BooleanProperty ,
ListProperty ,
NumericProperty ,
ObjectProperty ,
StringProperty ,
)
from kivy . uix . boxlayout import BoxLayout
from kivymd . theming import ThemableBehavior
from kivymd . uix . button import MDIconButton
from kivymd . uix . stacklayout import MDStackLayout
from main import COLOR_TEXT , rgb , COLOR_ICON , COLOR_ACCENT , COLOR_INACTIVE
2020-08-12 19:23:23 +00:00
2020-08-22 17:51:09 +00:00
def logg ( * x ) :
with open ( ' log.txt ' , ' a+ ' ) as of : of . write ( ' ' . join ( str ( y ) for y in x ) + ' \n ' )
2020-08-12 19:23:23 +00:00
class ProgressPopup ( MDDialog ) : pass
class MessagePopup ( MDDialog ) : pass
class UploadButton ( MDRectangleFlatButton ) :
2020-08-10 19:44:34 +00:00
'''
Button that triggers ' filechooser.open_file() ' and processes
the data response from filechooser Activity .
'''
selection = ListProperty ( [ ] )
def choose ( self ) :
'''
Call plyer filechooser API to run a filechooser Activity .
'''
filechooser . open_file ( on_selection = self . handle_selection )
def handle_selection ( self , selection ) :
'''
Callback function for handling the selection response from Activity .
'''
self . selection = selection
2020-08-12 19:23:23 +00:00
2020-08-10 19:44:34 +00:00
def on_selection ( self , * a , * * k ) :
'''
Update TextInput . text after FileChoose . selection is changed
via FileChoose . handle_selection .
'''
pass
#App.get_running_app().root.ids.result.text = str(self.selection)
2020-08-12 19:23:23 +00:00
class AddPostTextField ( MDTextField ) : pass
class ButtonLayout ( MDBoxLayout ) : pass
class PostButton ( MDRectangleFlatButton ) : pass
class PostStatus ( MDRectangleFlatButton ) : pass
2020-08-21 21:15:20 +00:00
2020-08-17 16:23:40 +00:00
class PostScreen ( ProtectedScreen ) :
2020-08-10 20:38:00 +00:00
post_id = ObjectProperty ( )
2020-08-12 19:23:23 +00:00
2020-08-22 18:58:11 +00:00
def open_author_option ( self ) :
2020-08-22 19:39:56 +00:00
import kivy
try :
self . post_card . add_widget ( self . to_whom_btn , 1 )
self . to_whom_btn . height = ' 100dp '
self . to_whom_btn . size_hint_y = None
2020-10-02 14:33:44 +00:00
self . log ( ' wtf????? ' )
except kivy . uix . widget . WidgetException as e :
self . log ( ' wtf ' , e )
2020-08-22 19:39:56 +00:00
return
2020-08-22 18:58:11 +00:00
def close_author_option ( self ) :
2020-08-22 19:39:56 +00:00
self . post_card . remove_widget ( self . to_whom_btn )
2020-08-22 18:58:11 +00:00
self . to_whom_btn . height = ' 0dp '
self . to_whom_btn . size_hint_y = None
2020-08-22 17:51:09 +00:00
2020-08-12 19:23:23 +00:00
def on_pre_enter ( self ) :
2020-09-26 10:12:16 +00:00
if not super ( ) . on_pre_enter ( ) : return
2020-08-22 00:27:20 +00:00
self . to_channels = { }
2020-08-19 19:33:25 +00:00
2020-08-12 19:23:23 +00:00
# clear
if hasattr ( self , ' post_status ' ) : self . remove_widget ( self . post_status )
if hasattr ( self , ' post_textfield ' ) : self . post_textfield . text = ' '
post_json = { ' author ' : self . app . username , ' timestamp ' : time . time ( ) }
2020-08-22 21:33:58 +00:00
keys = [ k for k in self . app . keys if k != self . app . username ]
2020-08-23 09:59:52 +00:00
if False : # ??
2020-08-22 21:33:58 +00:00
key = keys [ 0 ]
post_json [ ' to_name ' ] = key
self . recipient = key
else :
post_json [ ' to_name ' ] = ' ... '
2020-08-22 17:51:09 +00:00
2020-08-12 19:23:23 +00:00
self . post_card = post = PostCard ( post_json )
2020-08-22 00:27:20 +00:00
self . post_card . add_widget ( get_separator ( ' 15sp ' ) , 1 )
self . post_card . add_widget ( get_separator ( ' 15sp ' ) , 1 )
2020-08-12 19:23:23 +00:00
self . post_textfield = post_TextField = AddPostTextField ( )
2020-08-20 00:34:14 +00:00
post_TextField . line_color_focus = rgb ( * COLOR_TEXT )
post_TextField . line_color_normal = rgb ( * COLOR_TEXT )
post_TextField . current_hint_text_color = rgb ( * COLOR_TEXT )
2020-08-12 19:23:23 +00:00
post_TextField . font_name = ' assets/overpass-mono-regular.otf '
post_TextField . hint_text = ' word? '
2020-08-23 15:04:56 +00:00
post_TextField . font_size = ' 20sp '
2020-08-12 19:23:23 +00:00
2020-08-23 14:40:02 +00:00
self . post_card . to_whom_btn = self . to_whom_btn = DropDownWidget (
pos_hint = { ' center_x ' : 0.5 , ' center_y ' : 0.5 } ,
2020-08-22 18:58:11 +00:00
size_hint = ( None , None ) ,
2020-08-23 14:40:02 +00:00
height = ' 200sp ' ,
width = ' 100sp '
2020-08-22 18:58:11 +00:00
)
inp_towhom = self . to_whom_btn . ids . txt_input
inp_towhom . size_hint = ( None , None )
2020-08-22 19:39:56 +00:00
inp_towhom . width = ' 100sp '
inp_towhom . font_name = ' assets/font.otf '
2020-08-22 18:58:11 +00:00
# inp_towhom.height = '75sp'
2020-08-23 14:40:02 +00:00
2020-08-22 18:58:11 +00:00
inp_towhom . adaptive_height = True
2020-08-23 14:40:02 +00:00
inp_towhom . background_color = rgb ( * COLOR_CARD )
inp_towhom . color = rgb ( * COLOR_CARD )
inp_towhom . text_color = rgb ( * COLOR_CARD )
inp_towhom . theme_text_color = ' Custom '
2020-08-22 18:58:11 +00:00
# self.post_card.author_section_layout.md_bg_color=1,0,0,1
self . to_whom_layo = MDBoxLayout ( )
self . to_whom_layo . cols = 1
2020-08-22 19:39:56 +00:00
# self.to_whom_layo.size_hint=(None,None)
self . to_whom_layo . width = ' 300sp '
self . to_whom_layo . height = ' 2000sp '
self . to_whom_layo . md_bg_color = 1 , 1 , 0 , 1
2020-08-22 18:58:11 +00:00
# self.post_card.author_section_layout.add_widget(MDLabel(text='-->'),1)
2020-08-22 19:39:56 +00:00
# self.post_card.author_section_layout.add_widget(self.to_whom_btn)
# self.to_whom_layo.add_widget(self.to_whom_btn)
2020-08-23 14:40:02 +00:00
# self.tmp_msg = MDLabel(text='to')
# self.post_card.author_section_layout.add_widget(self.tmp_msg,1)
self . post_card . author_section_layout . add_widget ( self . to_whom_btn , 1 )
2020-08-22 19:39:56 +00:00
2020-09-29 13:09:56 +00:00
from comrad . constants import WORLD_NAME
2020-09-26 12:11:56 +00:00
self . to_whom_btn . ids . txt_input . text = ' @ ' + WORLD_NAME
2020-08-22 18:58:11 +00:00
#self.to_whom_btn.adaptive_height = True
self . to_whom_btn . ids . txt_input . word_list = [ ' @ ' + k for k in self . app . keys if k != self . app . username ]
self . to_whom_btn . ids . txt_input . starting_no = 1
# self.post_card.author_section_layout.add_widget(get_separator('1sp'))
#self.post_card.author_section_layout.add_widget(self.to_whom_btn,3)
# close for now
2020-08-22 19:39:56 +00:00
# self.close_author_option()
# self.post_card.remove_widget(self.to_whom_btn)
# self.to_whom_btn.height='0dp'
# self.to_whom_btn.size_hint_y=None
2020-08-22 00:27:20 +00:00
2020-08-22 17:51:09 +00:00
# remove content, add text input
2020-08-12 19:53:59 +00:00
post . scroller . remove_widget ( post . post_content )
2020-08-22 18:58:11 +00:00
# self.post_card.author_section_layout.add_widget(get_separator('1dp'))
# self.post_card.author_section_layout.add_widget(self.to_whom_layo,1)
2020-08-12 19:53:59 +00:00
post . scroller . add_widget ( post_TextField )
post . scroller . size = ( ' 300dp ' , ' 300dp ' )
2020-08-12 19:23:23 +00:00
self . add_widget ( post )
self . button_layout = ButtonLayout ( )
self . upload_button = UploadButton ( )
self . upload_button . screen = self
self . post_button = PostButton ( )
self . post_button . screen = self
self . post_status = PostStatus ( )
self . post_status_added = False
2020-08-23 14:40:02 +00:00
2020-08-12 19:23:23 +00:00
self . button_layout . add_widget ( self . upload_button )
self . button_layout . add_widget ( self . post_button )
2020-08-22 19:39:56 +00:00
2020-08-23 14:40:02 +00:00
self . upload_button . font_size = ' 8sp '
self . post_button . font_size = ' 8sp '
2020-08-12 19:23:23 +00:00
2020-08-23 14:40:02 +00:00
self . post_button . md_bg_color = rgb ( * COLOR_CARD )
self . upload_button . md_bg_color = rgb ( * COLOR_CARD )
2020-08-22 19:39:56 +00:00
self . post_status . md_bg_color = rgb ( * COLOR_CARD )
2020-08-12 19:23:23 +00:00
2020-08-23 14:40:02 +00:00
post . add_widget ( self . button_layout )
2020-08-12 19:23:23 +00:00
# self.add_widget(self.post_status)
def write_post_status ( self , x ) :
self . post_status . text = str ( x )
if not self . post_status_added :
self . add_widget ( self . post_status )
self . post_status_added = True
def open_dialog ( self , msg ) :
if not hasattr ( self , ' dialog ' ) or not self . dialog :
self . dialog = ProgressPopup ( )
self . dialog . ids . progress_label . text = msg
self . dialog . open ( )
def open_msg_dialog ( self , msg ) :
if not hasattr ( self , ' msg_dialog ' ) or not self . msg_dialog :
self . msg_dialog = MessagePopup ( )
self . msg_dialog . ids . msg_label . text = msg
self . msg_dialog . open ( )
def close_dialog ( self ) :
if hasattr ( self , ' dialog ' ) :
self . dialog . dismiss ( )
def close_msg_dialog ( self ) :
if hasattr ( self , ' msg_dialog ' ) :
self . msg_dialog . dismiss ( )
def choose ( self ) :
# time.sleep(5)
self . upload_button . choose ( )
self . orig_img_src = self . upload_button . selection
2020-08-17 20:40:48 +00:00
# self.open_dialog('uploading')
2020-08-12 19:23:23 +00:00
# self.upload()
# self.close_dialog()
2020-08-18 17:25:15 +00:00
#mythread = threading.Thread(target=self.upload)
#mythread.start()
self . upload ( )
2020-08-12 19:23:23 +00:00
def upload ( self ) :
2020-08-17 20:40:48 +00:00
# get file id
filename = self . orig_img_src [ 0 ] if self . orig_img_src and os . path . exists ( self . orig_img_src [ 0 ] ) else ' '
if not filename : return
self . img_id = file_id = get_random_id ( )
self . img_ext = os . path . splitext ( filename ) [ - 1 ] [ 1 : ]
# cache
2020-08-17 22:06:31 +00:00
tmp_img_fn = ' cache/ ' + self . img_id [ : 3 ] + ' / ' + self . img_id [ 3 : ] + ' . ' + self . img_ext
2020-08-17 20:40:48 +00:00
tmp_img_dir = os . path . dirname ( tmp_img_fn )
if not os . path . exists ( tmp_img_dir ) : os . makedirs ( tmp_img_dir )
shutil . copyfile ( filename , tmp_img_fn )
2020-08-17 22:06:31 +00:00
# add
2020-08-17 20:40:48 +00:00
self . add_image ( tmp_img_fn )
2020-08-17 22:06:31 +00:00
# upload
#def do_upload():
2020-08-20 10:59:53 +00:00
asyncio . create_task ( self . app . upload ( tmp_img_fn , file_id = file_id ) )
2020-08-17 22:06:31 +00:00
# Thread(target=do_upload).start()
2020-08-17 20:40:48 +00:00
# self.close_dialog()
def add_image ( self , filename ) :
if hasattr ( self , ' image_layout ' ) :
self . post_card . remove_widget ( self . image_layout )
self . image_layout = image_layout = PostImageLayout ( )
self . image = image = PostImage ( source = filename )
# self.image.texture = img.texture
self . image . height = ' 300dp '
self . image_layout . add_widget ( self . image )
self . image_layout . height = ' 300dp '
self . post_card . add_widget ( self . image_layout , index = 1 )
2020-08-12 19:23:23 +00:00
def post ( self ) :
# check?
2020-08-12 19:53:59 +00:00
maxlen = 500
2020-08-12 19:23:23 +00:00
content = self . post_textfield . text
2020-08-12 19:53:59 +00:00
lencontent = content . strip ( ) . replace ( ' ' , ' ' ) . count ( ' ' )
# maxlen = int(self.post_textfield.max_text_length)
2020-08-12 19:23:23 +00:00
lendiff = lencontent - maxlen
if lendiff > 0 :
2020-08-12 19:53:59 +00:00
self . open_msg_dialog ( f ' Text is currently { lencontent } words long, which is { lendiff } over the maximum text length of { maxlen } words. \n \n ( { lencontent } / { maxlen } ) ' )
2020-08-12 19:23:23 +00:00
return
2020-08-22 19:39:56 +00:00
#channels = [k[1:] for k,v in self.to_channels.items() if v]
2020-08-22 20:11:19 +00:00
recipient = self . to_whom_btn . ids . txt_input . text
self . log ( ' RECIPIENT: ' , recipient )
if not recipient : ##not hasattr(self,'recipient') or not self.recipient or self.app.username==self.recipient:
2020-08-22 19:39:56 +00:00
self . log ( ' no recipient was selected ' )
2020-08-22 00:27:20 +00:00
# self.='No place was selected'
return
2020-08-23 14:40:02 +00:00
if recipient . startswith ( ' @ ' ) : recipient = recipient [ 1 : ]
2020-08-22 20:11:19 +00:00
self . recipient = recipient
2020-08-22 00:27:20 +00:00
2020-08-12 19:23:23 +00:00
# log('?????????????????'+self.media_uid)
2020-08-17 20:59:53 +00:00
# if not hasattr(self,'img_id') and self.upload_button.selection:
# log('REUPLOADING')
# self.upload()
2020-08-19 14:07:12 +00:00
async def do_post ( ) :
2020-08-17 22:06:31 +00:00
file_id = self . img_id if hasattr ( self , ' img_id ' ) else None
file_ext = self . img_ext if hasattr ( self , ' img_ext ' ) else None
2020-09-26 10:12:16 +00:00
#await self.app.post(content=content, channel = channel, file_id=file_id, file_ext=file_ext)
# post?
2020-09-30 14:09:07 +00:00
res = await self . app . comrad . post_async ( content )
self . log ( ' GOT BACK RES: ' , res )
2020-09-30 17:59:06 +00:00
2020-09-30 14:09:07 +00:00
2020-08-17 22:06:31 +00:00
import time
self . close_dialog ( )
2020-09-26 10:12:16 +00:00
if hasattr ( self . app , ' map ' ) and self . app . map :
self . app . map . dismiss ( )
self . app . map = None
self . app . change_screen ( ' feed ' )
2020-08-17 22:06:31 +00:00
2020-08-22 21:20:27 +00:00
# self.open_dialog('')
2020-08-18 17:25:15 +00:00
#Thread(target=do_post).start()
2020-08-19 14:07:12 +00:00
asyncio . create_task ( do_post ( ) )
2020-08-10 20:38:00 +00:00
2020-08-17 20:40:48 +00:00
def get_random_id ( ) :
import uuid
2020-08-22 19:39:56 +00:00
return uuid . uuid4 ( ) . hex
class MessagePopup ( MDDialog ) : pass