################################################### the py file ############################################
import json
import requests
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivy.utils import rgba
from
kivymd.app import MDApp
from View.card_component import SingleCard
from config import load_token, LOCAL_HOST
Window.size = [320, 640]
Builder.load_file("View/StartScreen/start_screen.kv")
Builder.load_file("View/LoginScreen/login_screen.kv")
Builder.load_file("View/HomeScreen/home_screen.kv")
Builder.load_file("View/ProfileScreen/profile_screen.kv")
Builder.load_file("View/PostScreen/post_screen.kv")
class PeerScreenManager(ScreenManager):
pass
class PeerChat(MDApp):
def __int__(self, **kwargs):
super().__init__(**kwargs)
self.theme_cls.material_style = "M3"
def build(self) -> PeerScreenManager:
global screen_manager
screen_manager = PeerScreenManager()
return screen_manager
def on_clicked_post(self, post):
token = load_token()
params = {
"post": post
}
post_id = int(post)
headers = {
'content-type': 'application/json',
'Authorization': f"Token {token}"
}
response = requests.get(url=LOCAL_HOST + f'drops/{post_id}/',
data=json.dumps(params), headers=headers)
single_data = response.json()
root = SingleCard()
#
root.id = post_id
screen_manager.get_screen('post').ids.post.add_widget(
SingleCard(
id=single_data['id'],
title=single_data['content'],
author=single_data['author']['username'],
avatar=single_data['author']['avatar'],
reacts=str(single_data['reacts']),
comments=str(single_data['comments']),
source=str(single_data["media"]),
date=single_data['date']
)
)
screen_manager.current = "post"
def current_slide(self, index):
for i in range(3):
if index == i:
self.root.get_screen('start').ids[f'slide{index}'].color = rgba(253, 140, 95, 255)
else:
self.root.get_screen('start').ids[f'slide{i}'].color = rgba(253, 237, 240, 255)
def next(self):
self.root.get_screen('start').ids.carousel.load_next(mode="next")
if __name__ == "__main__":
LabelBase.register(name="MCairo", fn_regular="assets/fonts/Cairo/Cairo-Regular.ttf")
LabelBase.register(name="BCairo", fn_regular="assets/fonts/Cairo/Cairo-Bold.ttf")
PeerChat().run()
################################### py file for the list post ##################################
import json
import requests
from kivy.clock import mainthread
from kivy.network.urlrequest import UrlRequest
from kivy.properties import StringProperty, NumericProperty
from kivymd.uix.card import MDCard
from kivymd.uix.list import ThreeLineAvatarListItem
from kivymd.uix.screen import MDScreen
from config import LOCAL_HOST, load_token
class ListItemWithIcon(ThreeLineAvatarListItem):
source = StringProperty()
class PostCard(MDCard):
id = NumericProperty()
title = StringProperty(None)
author = StringProperty()
comments = StringProperty()
reacts = StringProperty()
source = StringProperty()
avatar = StringProperty()
date = StringProperty()
class HomeScreenView(MDScreen):
@mainthread
def on_enter(self):
self.list_posts()
def list_posts(self, *args):
token = load_token()
headers = {
'content-type': 'application/json',
'Authorization': f"Token {token}"
}
data_response = requests.get(url=LOCAL_HOST + 'drops/', headers=headers)
posts = data_response.json()['results']
root = PostCard()
if self.ids.home_list:
for post in posts:
self.ids.home_list.add_widget(
PostCard(
id=post['id'],
title=post['content'],
author=post['author']['username'],
avatar=post['author']['avatar'],
reacts=str(post['reacts']),
comments=str(post['comments']),
source=str(post["media"]),
date=post['date']
)
)
if post['media'] == "":
root.remove_widget(root.ids.post_image)
if post['content'] == "":
print("Post: ", post['content'])
root.remove_widget(root.ids.post_content)
####################################### the kv file for the list page #####################################
#:import get_color_from_hex kivy.utils.get_color_from_hex
<PostCard>
orientation: 'vertical'
size_hint: None, None
padding: "8dp"
size: "300dp", "400dp"
md_bg_color: 1, 0, 1, 0.0
id:
root.id on_release: app.on_clicked_post(
root.id)
canvas:
Color:
rgba: (230, 230, 230, 255)
Rectangle:
pos: self.pos
size: self.size
MDBoxLayout:
padding: 2
adaptive_height: True
OneLineAvatarListItem:
text: f'[b][size=14]{root.author}[/size][/b]'
divider: None
_no_ripple_effect: True
ImageLeftWidget:
source: root.avatar
radius: [20, ]
MDIconButton:
icon: 'dots-horizontal'
# Post
FitImage:
id: post_image
# adaptive_height: self.height if root.source != None else False
source: root.source
MDLabel:
id: post_content
text: root.title if root.title != None else ""
text_size: self.width, None
height: self.texture_size[1]
padding_x: 10
padding_y: 10
font_size: "12sp"
adaptive_height: self.height
# Reactions
MDBoxLayout:
padding: 5, 0, 0, 0
spacing: 2
adaptive_height: True
MDIconButton:
icon: 'heart-outline'
# on_release: root.on_clicked_liked(int(
post_id.id))
# on_release: root.on_clicked_liked()
MDIconButton:
icon: 'comment-outline'
Widget:
MDIconButton:
icon: 'bookmark-outline'
MDBoxLayout:
orientation: 'vertical'
padding: 5, 0, 0, 0
spacing: 2
adaptive_height: True
MyLabel:
text: f'{root.reacts} likes'
bold: True
MyLabel:
markup: True
text: f'[b]{root.author}[/b]'
HalfOpacityLabel:
text: f'View all {root.comments} comments'
MDBoxLayout:
padding: 5, 0, 0, 0
spacing: 2
adaptive_height: True
FitImage:
source: root.avatar
size_hint: None, None
size: 20, 20
radius: [20, ]
HalfOpacityLabel:
text: 'Add a comment...'
MDBoxLayout:
padding: 9, 0, 0, 0
spacing: 0
adaptive_height: True
HalfOpacityLabel:
font_size: 10
text: f'{root.date} ago'
<MyLabel@MDLabel>
adaptive_height: True
font_size: 12
<HalfOpacityLabel@MyLabel>:
theme_text_color: 'Custom'
text_color: 0, 0, 0, 0.5
spacing: 2
<HomeScreenView>
MDBoxLayout:
orientation: "vertical"
MDBoxLayout:
padding: '5dp'
adaptive_height: True
MDIconButton:
icon: 'plus'
MDLabel:
text: 'PeerChat'
halign: 'center'
valign: 'center'
font_size: 30
font_name: "assets/fonts/billabong.ttf"
MDIconButton:
icon: 'facebook-messenger'
MDBoxLayout:
ScrollView:
bar_width: 0
MDBoxLayout:
adaptive_height: True
MDBoxLayout:
orientation: 'vertical'
adaptive_height: True
id: home_list
padding: [10]
spacing: 10
MDBoxLayout:
adaptive_height: True
spacing: 10
padding: 10
MDIconButton:
icon: 'home'
on_release: root.manager.current = 'home'
MDIconButton:
icon: 'magnify'
MDIconButton:
icon: 'movie'
MDIconButton:
icon: 'heart-outline'
MDIconButton:
icon: 'account'
on_release: root.manager.current = "profile"
################################################### the kv file to the detail page ####################################
#:import get_color_from_hex kivy.utils.get_color_from_hex
<SingleCard>
orientation: 'vertical'
size_hint: None, None
padding: "8dp"
size: "300dp", "400dp"
md_bg_color: 1, 0, 1, 0.0
id: post_id
p_image: p_image
p_content: p_content
canvas:
Color:
rgba: (230, 230, 230, 255)
Rectangle:
pos: self.pos
size: self.size
MDBoxLayout:
padding: 2
adaptive_height: True
OneLineAvatarListItem:
text: f'[b][size=14]{root.author}[/size][/b]'
divider: None
_no_ripple_effect: True
ImageLeftWidget:
source: root.avatar
radius: [20, ]
MDIconButton:
icon: 'dots-horizontal'
# Post
FitImage:
id: p_image
# adaptive_height: self.height if root.source != None else False
source: root.source
MDLabel:
id: p_content
text: root.title if root.title != None else ""
text_size: self.width, None
height: self.texture_size[1]
padding_x: 10
padding_y: 10
font_size: "12sp"
adaptive_height: self.height
# Reactions
MDBoxLayout:
padding: 5, 0, 0, 0
spacing: 2
adaptive_height: True
MDIconButton:
icon: 'heart-outline'
# on_release: root.on_clicked_liked(int(
post_id.id))
# on_release: root.on_clicked_liked()
MDIconButton:
icon: 'comment-outline'
Widget:
MDIconButton:
icon: 'bookmark-outline'
MDBoxLayout:
orientation: 'vertical'
padding: 5, 0, 0, 0
spacing: 2
adaptive_height: True
PostLabel:
text: f'{root.reacts} likes'
bold: True
PostLabel:
markup: True
text: f'[b]{root.author}[/b]'
HalfOpacityLabel:
text: f'View all {root.comments} comments'
MDBoxLayout:
padding: 5, 0, 0, 0
spacing: 2
adaptive_height: True
FitImage:
source: root.avatar
size_hint: None, None
size: 20, 20
radius: [20, ]
PostOpacityLabel:
text: 'Add a comment...'
MDBoxLayout:
padding: 9, 0, 0, 0
spacing: 0
adaptive_height: True
PostOpacityLabel:
font_size: 10
text: f'{root.date} ago'
<PostLabel@MDLabel>
adaptive_height: True
font_size: 12
<PostOpacityLabel@MyLabel>:
theme_text_color: 'Custom'
text_color: 0, 0, 0, 0.5
spacing: 2
<PostScreenView>
post: post
MDBoxLayout:
orientation: "vertical"
MDBoxLayout:
padding: '5dp'
adaptive_height: True
MDIconButton:
icon: 'plus'
MDLabel:
text: 'PeerChat'
halign: 'center'
valign: 'center'
font_size: 30
font_name: "assets/fonts/billabong.ttf"
MDIconButton:
icon: 'facebook-messenger'
MDBoxLayout:
ScrollView:
bar_width: 0
MDBoxLayout:
adaptive_height: True
MDBoxLayout:
orientation: 'vertical'
adaptive_height: True
id: post
padding: [10]
spacing: 10
MDBoxLayout:
adaptive_height: True
spacing: 10
padding: 10
MDIconButton:
icon: 'home'
on_release: root.manager.current = 'home'
MDIconButton:
icon: 'magnify'
MDIconButton:
icon: 'movie'
MDIconButton:
icon: 'heart-outline'
MDIconButton:
icon: 'account'
on_release: root.manager.current = "profile"