Class: QTimetrap::ViewModels::MainViewModel

Overview

Coordinates tracker data and exposes presentation-ready state for UI.

Constant Summary collapse

EPOCH_TIME =
Time.at(0)

Constants included from MainViewModelArchiveModeHelpers

QTimetrap::ViewModels::MainViewModelArchiveModeHelpers::EMPTY_ARRAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MainViewModelArchiveModeHelpers

#archive_entry, #archive_mode=, #archive_mode?, #project_names, #task_names_for_project, #task_names_for_selected_project, #unarchive_entry

Methods included from MainViewModelTimeRangeFilterHelpers

#update_time_range_filter

Methods included from MainViewModelTaskFilterHelpers

#filtered_entries, #select_tasks

Methods included from MainViewModelSheetHelpers

#current_project_name, #current_project_name=, #current_sheet_input, #current_sheet_label, #current_task_input, #current_task_input=, #sheet_for_task_input

Methods included from MainViewModelEntryTimeHelpers

#update_entry_time

Methods included from MainViewModelEntryNoteHelpers

#update_entry_note

Methods included from MainViewModelEntryTaskHelpers

#update_entry_task

Constructor Details

#initialize(gateway: Services::TimetrapGateway.new, archived_entries_store: Services::ArchivedEntriesStore.new) ⇒ MainViewModel

Returns a new instance of MainViewModel.



22
23
24
25
26
# File 'app/view_models/main_view_model.rb', line 22

def initialize(gateway: Services::TimetrapGateway.new, archived_entries_store: Services::ArchivedEntriesStore.new)
  @gateway = gateway
  @archived_entries_store = archived_entries_store
  initialize_state
end

Instance Attribute Details

#current_sheetObject (readonly)

Returns the value of attribute current_sheet.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def current_sheet
  @current_sheet
end

#current_started_atObject (readonly)

Returns the value of attribute current_started_at.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def current_started_at
  @current_started_at
end

#entriesObject (readonly)

Returns the value of attribute entries.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def entries
  @entries
end

#selected_projectObject (readonly)

Returns the value of attribute selected_project.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def selected_project
  @selected_project
end

#selected_projectsObject (readonly)

Returns the value of attribute selected_projects.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def selected_projects
  @selected_projects
end

#selected_tasksObject (readonly)

Returns the value of attribute selected_tasks.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def selected_tasks
  @selected_tasks
end

#time_filter_from_atObject (readonly)

Returns the value of attribute time_filter_from_at.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def time_filter_from_at
  @time_filter_from_at
end

#time_filter_to_atObject (readonly)

Returns the value of attribute time_filter_to_at.



19
20
21
# File 'app/view_models/main_view_model.rb', line 19

def time_filter_to_at
  @time_filter_to_at
end

Instance Method Details

#entry_nodesObject



92
93
94
# File 'app/view_models/main_view_model.rb', line 92

def entry_nodes
  EntryNodesBuilder.new(entries: filtered_entries, selected_project: selected_project).build
end

#refresh!Object



28
29
30
31
32
33
34
35
36
37
# File 'app/view_models/main_view_model.rb', line 28

def refresh!
  @current_started_at = gateway.active_started_at
  @entries = gateway.entries
  @current_sheet = detect_current_sheet
  reset_archive_mode_caches!
  normalize_selected_projects!
  seed_current_fields_from_sheet!
  normalize_selected_tasks!
  self
end

#running_current_sheet?Boolean

Returns:

  • (Boolean)


90
# File 'app/view_models/main_view_model.rb', line 90

def running_current_sheet? = !current_started_at.nil?

#running_timer_line(now: Time.now) ⇒ Object



84
85
86
87
88
# File 'app/view_models/main_view_model.rb', line 84

def running_timer_line(now: Time.now)
  return '00:00:00' unless current_started_at

  Services::Formatters.seconds_to_hms(now.to_i - current_started_at.to_i)
end

#select_project(project, sync_current_fields: true) ⇒ Object



39
40
41
# File 'app/view_models/main_view_model.rb', line 39

def select_project(project, sync_current_fields: true)
  select_projects([project], primary_project: project, sync_current_fields: sync_current_fields)
end

#select_projects(projects, primary_project:, sync_current_fields: true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'app/view_models/main_view_model.rb', line 43

def select_projects(projects, primary_project:, sync_current_fields: true)
  normalized = Array(projects).map(&:to_s).reject(&:empty?).uniq
  normalized = ['* ALL'] if normalized.empty? || normalized.include?('* ALL')
  @selected_projects = normalized
  @selected_project = normalized.include?(primary_project) ? primary_project : normalized.first
  @selected_tasks = []
  return unless sync_current_fields

  apply_selected_project_to_current_field!
  self.current_task_input = latest_task_for_project(@selected_project)
end

#start_tracking(sheet) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
# File 'app/view_models/main_view_model.rb', line 55

def start_tracking(sheet)
  value = normalize_text(sheet).strip
  raise ArgumentError, 'Task is required' if value.empty?

  gateway.start(value)
  @current_started_at = Time.now unless current_started_at
end

#stop_trackingObject



63
64
65
66
# File 'app/view_models/main_view_model.rb', line 63

def stop_tracking
  gateway.stop
  @current_started_at = nil
end

#summary_lineObject



79
80
81
82
# File 'app/view_models/main_view_model.rb', line 79

def summary_line
  "Week total: #{Services::Formatters.seconds_to_hms(week_total_seconds)} | " \
    "Total: #{Services::Formatters.seconds_to_hms(total_seconds)}"
end

#total_secondsObject



75
76
77
# File 'app/view_models/main_view_model.rb', line 75

def total_seconds
  filtered_entries.sum(&:duration_seconds)
end

#week_total_secondsObject



68
69
70
71
72
73
# File 'app/view_models/main_view_model.rb', line 68

def week_total_seconds
  start_of_week = Date.today - ((Date.today.wday + 6) % 7)
  filtered_entries.sum do |entry|
    entry.day >= start_of_week ? entry.duration_seconds : 0
  end
end