Skip to content

Instantly share code, notes, and snippets.

@PyDann
Created November 3, 2014 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PyDann/a21eb12551014c79c225 to your computer and use it in GitHub Desktop.
Save PyDann/a21eb12551014c79c225 to your computer and use it in GitHub Desktop.
Countdown
# coding: utf-8
import ui
import os
import json
import console
from datetime import datetime as dt
if not os.path.exists('Countdown_Data'):
filedata = [{'title': 'GTA V', 'icon': None, 'datetime': '2014-11-14'}]
with open('Countdown_Data', 'w+') as f:
json.dump(filedata, f)
class MyView (ui.View):
def __init__(self):
with open('Countdown_Data', 'r') as f:
self.filedata = json.load(f)
def did_load(self):
self['button'].alpha = 0.0
self['button'].action = self.confirm_new_date
self['datepicker'].action = self.datepicker_did_change
filedate = dt.strptime(self.filedata[0]['datetime'], "%Y-%m-%d")
self['datepicker'].date = filedate
self['datelabel'].text = self.date_math(self['datepicker'].date)
self['tableview'].data_source = self
def date_math(self, thedate):
newtime = (thedate-dt.utcnow())
return str(newtime.days+1)
def datepicker_did_change(self, sender):
newdate = self.date_math(sender.date)
def inanimation():
self['button'].alpha = 1.0
def outanimation():
self['button'].alpha = 0.0
if int(newdate) > 0:
ui.animate(inanimation, duration=0.5)
else:
ui.animate(outanimation, duration=0.5)
self['datelabel'].text = str(newdate)
@ui.in_background
def confirm_new_date(self, sender):
title = console.input_alert('Title')
dateo = str(self['datepicker'].date.strftime("%Y-%m-%d"))
self.filedata.append({'title': title, 'datetime': dateo, 'icon': None})
with open('Countdown_Data', 'w+') as f:
json.dump(self.filedata, f)
self['tableview'].reload()
def animation():
self['button'].alpha = 0.0
ui.animate(animation, duration=0.5)
def tableview_number_of_rows(self, tableview, section):
return len(self.filedata)
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell('value1')
cell.text_label.number_of_lines = 0
cell.text_label.text = self.filedata[row]['title']
dateobject = dt.strptime(self.filedata[row]['datetime'], "%Y-%m-%d")
cell.detail_text_label.text = self.date_math(dateobject) + ' days'
cell.detail_text_label.font = ('<system>', 12.0)
try:
cell.image_view.image = ui.Image.named(self.filedata[row]['icon'])
except TypeError:
pass
return cell
the_view = ui.load_view('Countdown')
the_view.present('popover', hide_title_bar=True, orientations=['portrait'])
[{"class":"View","attributes":{"custom_class":"MyView","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","tint_color":"RGBA(0.000000,0.478000,1.000000,1.000000)","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":""},"frame":"{{0, 0}, {332, 495}}","nodes":[{"class":"Label","attributes":{"font_size":58,"enabled":true,"text":"25","font_name":"HelveticaNeue-Bold","name":"datelabel","flex":"LRB","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"center","tint_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","uuid":"69E9A1A9-104F-4E33-990F-CB27EDF377D7"},"frame":"{{70, 48}, {191, 64.5}}","nodes":[]},{"class":"Label","attributes":{"font_size":14,"enabled":true,"text":"Days","font_name":"AppleSDGothicNeo-Light","name":"label1","flex":"LRB","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"center","uuid":"30BE22F7-349D-4A29-A03E-36E425C2EFA7"},"frame":"{{70, 96.5}, {191, 32}}","nodes":[]},{"class":"DatePicker","attributes":{"tint_color":"RGBA(1.000000,0.071429,0.071429,1.000000)","enabled":true,"flex":"LRT","name":"datepicker","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","mode":1,"uuid":"FD2D58EB-01A0-4579-A7A6-B35E5018B5A2"},"frame":"{{6, 273}, {320, 216}}","nodes":[]},{"class":"TableView","attributes":{"enabled":true,"data_source_font_size":18,"data_source_number_of_lines":1,"flex":"WHB","name":"tableview","data_source_delete_enabled":true,"row_height":44,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","data_source_items":"Row 1\nRow 2 >\nRow 3 (i)","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","uuid":"EBE3031E-799E-4285-B253-1D83DE2489DC"},"frame":"{{7, 147}, {320, 118}}","nodes":[]},{"class":"ImageView","attributes":{"enabled":true,"flex":"LRB","name":"imageview1","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alpha":1,"image_name":"ionicons-ios7-calendar-outline-256","uuid":"9B0C65CE-E1D1-4C94-BC98-488AF6F1304D"},"frame":"{{93, 0}, {144, 144}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"LB","font_bold":false,"name":"button","uuid":"753D8333-94C6-412A-BA88-D328C4992198","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alpha":0,"image_name":"ionicons-ios7-alarm-outline-32","title":""},"frame":"{{245, 16}, {50, 50}}","nodes":[]}]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment