Phuket2
Just to give a better idea, the below is where I am at so far. I just show the TinyDB class and the datasource class that I am mapping to the TinyDB class. It's not complete, but its getting there.
The Move callback maybe quite tricky to implement. I am sure its possible, maybe just slow. But on small datasets will not see it I think.
Also, the cell creation in the callback 'tableview_cell_for_row', is not correct yet. I will pull that out and make cell externally. It can be generic for simple things, but for what I have in mind it will be handled separately.
Just posted, to give a bit more context to what I am trying to achieve.
class TinyDBBackEnd(object):
'''
For whatever reason, I decided not to leave the database open.
'''
def __init__(self, db_fn, *args, **kwargs):
self.db_fn = db_fn
def add_record(self, rec_dict, key, section_str='_default'):
with TinyDB(self.db_fn) as db:
tbl = db.table(section_str)
tbl.upsert(rec_dict, where(key) == rec_dict[key])
def get_section_tbl(self, db, section):
return db.table(list(db.tables())[section])
def num_records(self, section):
with TinyDB(self.db_fn) as db:
return len(self.get_section_tbl(db, section))
def get_indexed_rec(self, section, idx):
with TinyDB(self.db_fn) as db:
tbl = self.get_section_tbl(db, section)
doc_ids = list(tbl._read().keys())
return db.get(doc_id=doc_ids[idx])
def del_rec(self, section, idx):
with TinyDB(self.db_fn) as db:
tbl = self.get_section_tbl(db, section)
doc_ids = list(tbl._read().keys())
tbl.remove(doc_ids=[doc_ids[idx]])
def num_sections(self):
with TinyDB(self.db_fn) as db:
return len(db.tables())
def get_section_name(self, section):
with TinyDB(self.db_fn) as db:
return list(db.tables())[section]
class MyDataSource(object):
def __init__(self, db_fn, *args, **kwargs):
self.db = TinyDBBackEnd(db_fn)
def tableview_number_of_sections(self, tableview):
# Return the number of sections (defaults to 1)
return self.db.num_sections()
def tableview_number_of_rows(self, tableview, section):
# Return the number of rows in the section
return self.db.num_records(section)
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
rec = self.db.get_indexed_rec(section, row)
cell = ui.TableViewCell()
cell.text_label.text = rec['timer_name']
return cell
def tableview_title_for_header(self, tableview, section):
# Return a title for the given section.
# If this is not implemented, no section headers will be shown.
return self.db.get_section_name(section)
def tableview_can_delete(self, tableview, section, row):
# Return True if the user should be able to delete the given row.
return True
def tableview_can_move(self, tableview, section, row):
# Return True if a reordering control should be shown for the given row (in editing mode).
return True
def tableview_delete(self, tableview, section, row):
# Called when the user confirms deletion of the given row.
self.db.del_rec(section, row)
tableview.reload_data()
def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
# Called when the user moves a row with the reordering control (in editing mode).
pass