@@ -8,6 +8,9 @@ function Controller.new(options)
88 push = assert (options .push , " navigation push callback is required" ),
99 back = assert (options .back , " navigation back callback is required" ),
1010 ns = assert (options .ns , " native platform module is required" ),
11+ speech = nil ,
12+ dictationActive = false ,
13+ dictationPrefix = " " ,
1114 }, Controller )
1215end
1316
@@ -21,14 +24,24 @@ function Controller:show(id)
2124 }, game .title )
2225 return false
2326 end
27+ self :cancelDictation ()
28+ local speechAvailable = type (self .ns .SpeechRecognizer ) == " function"
29+ if speechAvailable then
30+ self .speech = self .ns .SpeechRecognizer (function (state , text , message )
31+ self :onSpeechEvent (state , text , message )
32+ end )
33+ end
2434 local actions = {
2535 submit = function () self :submitCommand (self .refs .input .text ) end ,
2636 look = function () self :submitCommand (" look" ) end ,
2737 inventory = function () self :submitCommand (" inventory" ) end ,
28- close = self .back ,
38+ dictate = function () self :toggleDictation () end ,
39+ close = function () self :close () end ,
2940 }
3041 self .view , self .refs = self .push (" Session" , {
31- transcript = self .model :transcript (), actions = actions ,
42+ transcript = self .model :transcript (),
43+ speechAvailable = speechAvailable ,
44+ actions = actions ,
3245 }, game .title )
3346 self .refs .input .accessibilityLabel = " Command"
3447 self .ns ._textFieldCallbacks (self .refs .input , nil , function (command )
@@ -39,10 +52,69 @@ function Controller:show(id)
3952 return true
4053end
4154
55+ function Controller :onSpeechEvent (state , text , message )
56+ if not self .refs then return end
57+ if state == " listening" then
58+ self .dictationActive = true
59+ self .refs .dictationStatus .text = " Listening… Tap the microphone to finish."
60+ elseif state == " partial" or state == " finished" then
61+ local separator = self .dictationPrefix ~= " "
62+ and not self .dictationPrefix :match (" %s$" ) and " " or " "
63+ self .refs .input .text = self .dictationPrefix .. separator .. (text or " " )
64+ if state == " finished" then
65+ self .dictationActive = false
66+ self .refs .dictationStatus .text = " "
67+ end
68+ elseif state == " starting" then
69+ self .refs .dictationStatus .text = " Waiting for microphone access…"
70+ elseif state == " processing" then
71+ self .refs .dictationStatus .text = " Transcribing…"
72+ elseif state == " error" then
73+ self .dictationActive = false
74+ self .refs .dictationStatus .text = message or " Dictation is unavailable. Check microphone and speech access in Settings."
75+ elseif state == " idle" then
76+ self .dictationActive = false
77+ self .refs .dictationStatus .text = " "
78+ end
79+ self .view :layout ()
80+ end
81+
82+ function Controller :toggleDictation ()
83+ if not self .speech then return end
84+ if self .dictationActive then
85+ self .speech :stop ()
86+ self .refs .dictationStatus .text = " Transcribing…"
87+ else
88+ self .dictationPrefix = self .refs .input .text or " "
89+ self .dictationActive = true
90+ self .speech :start ()
91+ end
92+ self .view :layout ()
93+ end
94+
95+ function Controller :cancelDictation ()
96+ if self .speech and self .dictationActive then self .speech :cancel () end
97+ self .dictationActive = false
98+ end
99+
100+ function Controller :close ()
101+ self :onDisappear ()
102+ self .back ()
103+ end
104+
105+ function Controller :onDisappear ()
106+ self :cancelDictation ()
107+ self .speech = nil
108+ self .refs = nil
109+ self .view = nil
110+ end
111+
42112function Controller :submitCommand (command )
113+ self :cancelDictation ()
43114 local ok , err = self .model :submit (command )
44115 self .refs .output .text = self .model :transcript ()
45116 self .refs .input .text = " "
117+ self .refs .dictationStatus .text = " "
46118 self .view :layout ()
47119 return ok , err
48120end
0 commit comments