Skip to content

Offline gym log for Android. No account, no ads, no internet.

Add workout voice in beta

InlitXe118da18e1be
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index e57a822..8cbd9eb 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -200,5 +200,8 @@
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
+ <intent>
+ <action android:name="android.intent.action.TTS_SERVICE"/>
+ </intent>
</queries>
</manifest>
diff --git a/android/app/src/main/kotlin/com/gymmane/app/VoiceBridge.kt b/android/app/src/main/kotlin/com/gymmane/app/VoiceBridge.kt
new file mode 100644
index 0000000..34e0caf
--- /dev/null
+++ b/android/app/src/main/kotlin/com/gymmane/app/VoiceBridge.kt
@@ -0,0 +1,147 @@
+package com.gymmane.app
+
+import android.content.Context
+import android.content.Intent
+import android.media.AudioAttributes
+import android.os.Handler
+import android.os.Looper
+import android.speech.tts.TextToSpeech
+import android.speech.tts.UtteranceProgressListener
+import io.flutter.plugin.common.MethodChannel
+import java.util.Locale
+
+object VoiceBridge {
+ const val CHANNEL = "gymmane/voice"
+ private var tts: TextToSpeech? = null
+ private var ready = false
+ private var starting = false
+ private val waiting = mutableListOf<() -> Unit>()
+ private var listening: MethodChannel.Result? = null
+ private var testId = ""
+ private var tests = 0
+ private val main = Handler(Looper.getMainLooper())
+
+ fun attach(context: Context, channel: MethodChannel) {
+ val app = context.applicationContext
+ channel.setMethodCallHandler { call, result ->
+ when (call.method) {
+ "say" -> {
+ val text = call.argument<String>("text") ?: ""
+ val lang = call.argument<String>("lang") ?: ""
+ if (text.isNotEmpty()) ensure(app) { if (ready) speak(text, lang) }
+ finish()
+ result.success(true)
+ }
+ "sayWait" -> {
+ val text = call.argument<String>("text") ?: ""
+ val lang = call.argument<String>("lang") ?: ""
+ finish()
+ if (text.isEmpty()) {
+ result.success(false)
+ } else {
+ listening = result
+ tests++
+ val id = "gymmane-test-$tests"
+ testId = id
+ ensure(app) { if (ready) speak(text, lang, id) else finish() }
+ }
+ }
+ "stop" -> {
+ finish()
+ try {
+ tts?.stop()
+ } catch (_: Exception) {
+ }
+ result.success(true)
+ }
+ "check" -> {
+ val lang = call.argument<String>("lang") ?: ""
+ ensure(app) { result.success(status(lang)) }
+ }
+ "settings" -> {
+ result.success(
+ try {
+ app.startActivity(Intent("com.android.settings.TTS_SETTINGS").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
+ true
+ } catch (_: Exception) {
+ false
+ }
+ )
+ }
+ else -> result.notImplemented()
+ }
+ }
+ }
+
+ private fun ensure(context: Context, then: () -> Unit) {
+ if (ready && tts != null) {
+ then()
+ return
+ }
+ waiting.add(then)
+ if (starting) return
+ starting = true
+ tts = TextToSpeech(context) { st ->
+ ready = st == TextToSpeech.SUCCESS
+ starting = false
+ if (ready) {
+ tts?.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
+ override fun onStart(id: String?) {}
+ override fun onDone(id: String?) {
+ main.post { if (id == testId) finish() }
+ }
+ @Deprecated("Deprecated in Java")
+ override fun onError(id: String?) {
+ main.post { if (id == testId) finish() }
+ }
+ override fun onStop(id: String?, interrupted: Boolean) {
+ main.post { if (id == testId) finish() }
+ }
+ })
+ }
+ if (!ready) {
+ try {
+ tts?.shutdown()
+ } catch (_: Exception) {
+ }
+ tts = null
+ }
+ val run = waiting.toList()
+ waiting.clear()
+ run.forEach { it() }
+ }
+ }
+
+ private fun status(lang: String): String {
+ val engine = tts
+ if (!ready || engine == null) return "noEngine"
+ if (lang.isEmpty()) return "ok"
+ return try {
+ if (engine.isLanguageAvailable(Locale.forLanguageTag(lang)) >= TextToSpeech.LANG_AVAILABLE) "ok" else "noLang"
+ } catch (_: Exception) {
+ "noLang"
+ }
+ }
+
+ private fun finish() {
+ val r = listening ?: return
+ listening = null
+ r.success(true)
+ }
+
+ private fun speak(text: String, lang: String, id: String = "gymmane") {
+ val engine = tts ?: return
+ try {
+ if (lang.isNotEmpty()) engine.setLanguage(Locale.forLanguageTag(lang))
+ engine.setAudioAttributes(
+ AudioAttributes.Builder()
+ .setUsage(AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
+ .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
+ .build()
+ )
+ engine.speak(text, TextToSpeech.QUEUE_FLUSH, null, id)
+ } catch (_: Exception) {
+ if (id == testId) finish()
+ }
+ }
+}
diff --git a/lib/services/voice_coach.dart b/lib/services/voice_coach.dart
new file mode 100644
index 0000000..7460729
--- /dev/null
+++ b/lib/services/voice_coach.dart
@@ -0,0 +1,48 @@
+import 'dart:io' show Platform;
+
+import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+
+class VoiceCoach {
+ VoiceCoach._();
+
+ static const _channel = MethodChannel('gymmane/voice');
+ static bool enabled = true;
+
+ static Future<void> say(String text, String lang) async {
+ if (!enabled || kIsWeb || !Platform.isAndroid || text.trim().isEmpty) return;
+ try {
+ await _channel.invokeMethod('say', {'text': text, 'lang': lang});
+ } catch (_) {}
+ }
+
+ static Future<void> sayAndWait(String text, String lang) async {
+ if (kIsWeb || !Platform.isAndroid || text.trim().isEmpty) return;
+ try {
+ await _channel.invokeMethod('sayWait', {'text': text, 'lang': lang});
+ } catch (_) {}
+ }
+
+ static Future<void> stop() async {
+ if (!enabled || kIsWeb || !Platform.isAndroid) return;
+ try {
+ await _channel.invokeMethod('stop');
+ } catch (_) {}
+ }
+
+ static Future<String> check(String lang) async {
+ if (kIsWeb || !Platform.isAndroid) return 'noEngine';
+ try {
+ return await _channel.invokeMethod<String>('check', {'lang': lang}) ?? 'noEngine';
+ } catch (_) {
+ return 'noEngine';
+ }
+ }
+
+ static Future<void> openSettings() async {
+ if (kIsWeb || !Platform.isAndroid) return;
+ try {
+ await _channel.invokeMethod('settings');
+ } catch (_) {}
+ }
+}
diff --git a/lib/widgets/voice_sheet.dart b/lib/widgets/voice_sheet.dart
new file mode 100644
index 0000000..f6f1b31
--- /dev/null
+++ b/lib/widgets/voice_sheet.dart
@@ -0,0 +1,125 @@
+import 'package:flutter/material.dart';
+import 'package:phosphoricons_flutter/phosphoricons_flutter.dart';
+
+import '../l10n/l10n.dart';
+import '../services/voice_coach.dart';
+import '../state/fit_state.dart';
+import '../theme/app_colors.dart';
+import '../theme/app_theme.dart';
+import 'glass.dart';
+import 'ui_kit.dart';
+
+String _voiceSample() =>
+ '${t.voiceRest(fit.voiceDuration(90))} ${t.voiceNext(t.catalogName('EIeI8Vf', 'Barbell Bench Press'), t.voiceLoad(8, fit.weightLabel(60)))}';
+
+void showVoiceSheet(BuildContext context) {
+ final gc = context.gc;
+ final status = VoiceCoach.check(intlLocale);
+ final points = [
+ (PhosphorIconsRegular.timer, t.voicePointRest),
+ (PhosphorIconsRegular.hourglassMedium, t.voicePointCount),
+ (PhosphorIconsRegular.speakerHigh, t.voicePointPhone),
+ ];
+ showAppSheet<void>(
+ context: context,
+ isScrollControlled: true,
+ backgroundColor: Colors.transparent,
+ builder: (sheet) => Container(
+ padding: sheetPad(sheet),
+ decoration: sheetDecoration(gc),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ const SheetHandle(),
+ const SizedBox(height: 20),
+ Text(t.voiceSheetTitle, textAlign: TextAlign.center, style: AppTheme.f(19, color: gc.text)),
+ const SizedBox(height: 22),
+ for (final (icon, text) in points)
+ Padding(
+ padding: const EdgeInsets.only(bottom: 10),
+ child: Row(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Icon(icon, size: 16, color: gc.accent),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Text(text,
+ style: AppTheme.f(12.5, weight: FontWeight.w500, color: gc.textSecondary, height: 1.4)),
+ ),
+ ],
+ ),
+ ),
+ FutureBuilder<String>(
+ future: status,
+ builder: (_, snap) {
+ final s = snap.data;
+ if (s == null || s == 'ok') return const SizedBox(height: 12);
+ return Padding(
+ padding: const EdgeInsets.fromLTRB(0, 6, 0, 12),
+ child: Row(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Icon(PhosphorIconsRegular.warningCircle, size: 16, color: gc.warn),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Text(s == 'noLang' ? t.voiceNoLang : t.voiceNoEngine,
+ style: AppTheme.f(12.5, weight: FontWeight.w600, color: gc.text, height: 1.4)),
+ ),
+ ],
+ ),
+ );
+ },
+ ),
+ Center(
+ child: Wrap(alignment: WrapAlignment.center, spacing: 10, runSpacing: 10, children: [
+ const _TryButton(),
+ GhostButton(
+ label: t.voiceOpenSettings,
+ icon: PhosphorIconsRegular.gear,
+ onTap: VoiceCoach.openSettings,
+ ),
+ ]),
+ ),
+ const SizedBox(height: 16),
+ PrimaryButton(label: t.done, onTap: () => Navigator.of(sheet).pop()),
+ ],
+ ),
+ ),
+ );
+}
+
+class _TryButton extends StatefulWidget {
+ const _TryButton();
+
+ @override
+ State<_TryButton> createState() => _TryButtonState();
+}
+
+class _TryButtonState extends State<_TryButton> {
+ bool _playing = false;
+
+ Future<void> _tap() async {
+ if (_playing) {
+ setState(() => _playing = false);
+ await VoiceCoach.stop();
+ return;
+ }
+ setState(() => _playing = true);
+ await VoiceCoach.sayAndWait(_voiceSample(), intlLocale);
+ if (mounted) setState(() => _playing = false);
+ }
+
+ @override
+ void dispose() {
+ if (_playing) VoiceCoach.stop();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) => GhostButton(
+ label: t.voiceTry,
+ icon: _playing ? PhosphorIconsRegular.pause : PhosphorIconsRegular.play,
+ onTap: _tap,
+ );
+}