Created
March 25, 2021 01:44
-
-
Save tshirtman/9cce16df53f643118cf72cb080e50398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
''' | |
import random | |
from uuid import uuid4 | |
from string import ascii_lowercase | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.properties import ListProperty, StringProperty | |
KV = ''' | |
#:import dumps json.dumps | |
<ImageButton@ButtonBehavior+Image>: | |
<EditableLabel@ButtonBehavior+Label>: | |
edit: False | |
tmp: '' | |
next: None | |
font_size: '20sp' | |
uuid: None | |
key: '' | |
on_release: app.edit_people(root.uuid, root.key, True) | |
canvas.before: | |
Color: | |
rgba: rgba('#222222') | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
BoxLayout: | |
pos: root.pos | |
size: root.width, root.height if root.edit else 0 | |
opacity: 1 if root.edit else 0 | |
disabled: not root.edit | |
TextInput: | |
text: root.tmp | |
multiline: False | |
font_size: root.font_size | |
focus: root.edit and app.current_focus == '{uuid}-{key}'.format(uuid=root.uuid, key=root.key) | |
on_text: app.update_people(root.uuid, **{f'tmp_{root.key}': self.text}) | |
on_text_validate: | |
app.update_people(root.uuid, **{root.key: self.text}) | |
app.edit_people(root.uuid, root.key, False) | |
if root.next: app.edit_people(root.uuid, root.next, True) | |
ImageButton: | |
source: 'atlas://data/images/defaulttheme/close' | |
pos_hint: {'center_y': .5} | |
size_hint: None, None | |
size: self.texture_size | |
on_press: | |
root.edit = False | |
root.tmp = root.text | |
<EditableRow@BoxLayout>: | |
uuid: None | |
name: '' | |
surname: '' | |
year: '' | |
edit_name: False | |
edit_surname: False | |
edit_year: False | |
tmp_name: '' | |
tmp_surname: '' | |
tmp_year: '' | |
spacing: '10dp' | |
EditableLabel: | |
id: name | |
text: root.name | |
tmp: root.tmp_name | |
edit: root.edit_name | |
uuid: root.uuid | |
key: 'name' | |
next: 'surname' | |
EditableLabel: | |
id: surname | |
text: root.surname | |
tmp: root.tmp_surname | |
edit: root.edit_surname | |
uuid: root.uuid | |
key: 'surname' | |
next: 'year' | |
EditableLabel: | |
id: year | |
text: root.year | |
tmp: root.tmp_year | |
uuid: root.uuid | |
edit: root.edit_year | |
key: 'year' | |
FloatLayout: | |
BoxLayout: | |
ScrollView: | |
size_hint_x: .5 | |
Label: | |
text: dumps(app.people, indent=2) | |
size_hint_y: None | |
height: self.texture_size[1] | |
BoxLayout: | |
orientation: 'vertical' | |
BoxLayout: | |
size_hint_y: None | |
height: '30dp' | |
Label: | |
text: 'name' | |
Label: | |
text: 'surname' | |
Label: | |
text: 'year of birth' | |
RecycleView: | |
data: app.people | |
viewclass: 'EditableRow' | |
RecycleBoxLayout: | |
size_hint_y: None | |
height: self.minimum_height | |
orientation: 'vertical' | |
default_size_hint: 1, None | |
default_size: 0, '40dp' | |
spacing: '10dp' | |
Button: | |
text: 'add' | |
size_hint_y: None | |
height: '40dp' | |
on_press: | |
app.add_people() | |
''' | |
people_model = { | |
'uuid': '', | |
'name': '', | |
'surname': '', | |
'year': '', | |
'tmp_name': '', | |
'tmp_surname': '', | |
'tmp_year': '', | |
'edit_name': False, | |
'edit_surname': False, | |
'edit_year': False, | |
} | |
class Application(App): | |
people = ListProperty() | |
current_focus = StringProperty('') | |
def build(self): | |
self.people = [{ | |
**people_model, | |
'uuid': uuid4().int, | |
'name': ''.join(random.sample(ascii_lowercase, 5)).capitalize(), | |
'surname': ''.join(random.sample(ascii_lowercase, 8)).capitalize(), | |
'year': str(random.randint(1900, 2020)), | |
} for i in range(50)] | |
return Builder.load_string(KV) | |
def add_people(self): | |
uuid = uuid4().int | |
self.people.append({ | |
**people_model, | |
'uuid': uuid, | |
}) | |
self.edit_people(uuid, 'name', True) | |
def edit_people(self, uuid, key, active): | |
self.people = [ | |
{ | |
**item, | |
f'tmp_{key}': item[key], | |
f'edit_{key}': active, | |
} | |
if item['uuid'] == uuid | |
else item | |
for item in self.people | |
] | |
if active: | |
print(f"setting current_focus to {uuid, key}") | |
self.current_focus = f'{uuid}-{key}' | |
def update_people(self, uuid, **kwargs): | |
# recreate the list with the updated item | |
data = [ | |
{ | |
**item, | |
**kwargs, | |
} | |
if item["uuid"] == uuid | |
else item | |
for item in self.people | |
] | |
self.people = data | |
if __name__ == "__main__": | |
Application().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment