Creating Details page for a post

29 views
Skip to first unread message

FlamzyFlames

unread,
Nov 28, 2022, 5:39:25 AM11/28/22
to kivy-...@googlegroups.com
So am trying to get detail page of a post, where user click on a post and it will go to the post detail, it working fine when I click on any post at first, but clicking on another post, the first post i clicked still shows on the second one.
 
The detail_post2.png show the first post I clicked on and still shows the second one instead of just showing the second only only.
5.png
detail_post.png
detail_post2.png

Elliot Garbus

unread,
Nov 28, 2022, 10:54:53 AM11/28/22
to kivy-...@googlegroups.com

Share you code.  I could not even guess what is causing that problem.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAKteDCDss5P6ceYa-u4uFTigg0S16i54RH9MrmrV%2BWiWx6OOFQ%40mail.gmail.com.

 

Oluwaseyi Obanibi

unread,
Nov 28, 2022, 8:15:01 PM11/28/22
to Kivy users support
################################################### 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"

Elliot Garbus

unread,
Nov 29, 2022, 8:19:41 PM11/29/22
to kivy-...@googlegroups.com

Your question: So am trying to get detail page of a post, where user click on a post and it will go to the post detail, it working fine when I click on any post at first, but clicking on another post, the first post i clicked still shows on the second one.”

 

I’m not sure I understand your problem – but I do see a problem in your code.

 

Id is a revered attribute in kv.  The id gets processed when kv is complied, and creates and ids dict that associates the id with the widget object the id is associated with.  You have created a numeric property called id.  I can not be certain of the behavior with the overloading of id.  See:  https://kivy.org/doc/stable/api-kivy.lang.html?highlight=lang#ids

 

I suggest you rename the numeric property in PostCard card_id (or anything else but id).

 

I did not run your code (it was too much copying pasting and renaming for me).  if you still have issues please try to create a minimal runnable example.

--

You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.

FlamzyFlames

unread,
Dec 2, 2022, 2:59:41 AM12/2/22
to kivy-...@googlegroups.com
Changed the id in the card to card_id but it still not working as expected. 

Elliot Garbus

unread,
Dec 2, 2022, 7:37:25 AM12/2/22
to kivy-...@googlegroups.com

Share a minimal runnable example.

I had tried running your code – but I think there were files missing – it would not run.  If you need to post multiple files please post a zip file.

Reply all
Reply to author
Forward
0 new messages