Skip to content

Commit

Permalink
Make toggles global
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkrieger committed Oct 3, 2022
1 parent d884581 commit 4252c01
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 61 deletions.
1 change: 1 addition & 0 deletions lib/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const shouldTranscribeKey = "should_transcribe";
const shouldLocateKey = "should_locate";
const showCompletedKey = "show_completed";
const showArchivedKey = "show_archived";
const allowRetranscriptionKey = "allow_retranscription";
const lastRouteKey = "last_route";
const lastOutlineKey = "last_outline";
const modelDirKey = "model_dir";
Expand Down
47 changes: 22 additions & 25 deletions lib/state/notes_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:voice_outliner/repositories/vosk_speech_recognizer.dart';
import 'package:voice_outliner/state/player_state.dart';

import '../consts.dart';
import '../globals.dart';

final defaultNote = Note(
id: "",
Expand All @@ -27,7 +28,6 @@ final defaultNote = Note(
class NotesModel extends ChangeNotifier {
bool shouldTranscribe = false;
bool shouldLocate = false;
bool showCompleted = true;
bool isReady = false;
String locale = "en-US";
Completer<bool> _readyCompleter = Completer();
Expand All @@ -50,12 +50,6 @@ class NotesModel extends ChangeNotifier {

NotesModel(this._outlineId);

Future<void> toggleShowCompleted() async {
showCompleted = !showCompleted;
await prefs.setBool(showCompletedKey, showCompleted);
notifyListeners();
}

@override
void dispose() {
super.dispose();
Expand All @@ -75,27 +69,29 @@ class NotesModel extends ChangeNotifier {
return (shouldTranscribe && !note.transcribed);
}

Future<void> transcribe(Note note) async {
if (shouldTranscribe &&
(!note.transcribed || note.transcript == null) &&
note.filePath != null) {
final path = _playerModel.getPathFromFilename(note.filePath!);
final res = Platform.isIOS
? await recognizeNoteIOS(path, locale)
: await voskSpeechRecognize(path);
// Guard against writing after user went back
if (isReady) {
note.transcribed = true;
note.transcript = res;
await rebuildNote(note);
}
Future<void> _transcribeNote(Note note) async {
final path = _playerModel.getPathFromFilename(note.filePath!);
final res = Platform.isIOS
? await recognizeNoteIOS(path, locale)
: await voskSpeechRecognize(path);
// Guard against writing after user went back
if (isReady) {
note.transcribed = true;
note.transcript = res;
await rebuildNote(note);
}
}

Future<void> _transcribe(Note note) async {
if (shouldTranscribe && (!note.transcribed) && note.filePath != null) {
_transcribeNote(note);
}
}

Future<void> runJobs() async {
Sentry.addBreadcrumb(
Breadcrumb(message: "Running jobs", timestamp: DateTime.now()));
notes.forEach(transcribe);
notes.forEach(_transcribe);
}

Future<void> createTextNote(String text) async {
Expand Down Expand Up @@ -229,13 +225,15 @@ class NotesModel extends ChangeNotifier {
message: "Couldn't locate note", timestamp: DateTime.now()));
}
}
await transcribe(note);
await _transcribe(note);
}

Future<void> retranscribeNote(Note note) async {
snackbarKey.currentState
?.showSnackBar(SnackBar(content: Text("Re-transcribing...")));
Sentry.addBreadcrumb(
Breadcrumb(message: "Retranscribe note", timestamp: DateTime.now()));
await transcribe(note);
await _transcribeNote(note);
notifyListeners();
}

Expand Down Expand Up @@ -505,7 +503,6 @@ class NotesModel extends ChangeNotifier {
prefs = await SharedPreferences.getInstance();
shouldTranscribe = prefs.getBool(shouldTranscribeKey) ?? false;
shouldLocate = prefs.getBool(shouldLocateKey) ?? false;
showCompleted = prefs.getBool(showCompletedKey) ?? true;
if (Platform.isIOS) {
locale = prefs.getString(localeKey) ?? locale;
}
Expand Down
17 changes: 17 additions & 0 deletions lib/state/outline_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class OutlinesModel extends ChangeNotifier {
late SharedPreferences prefs;
bool isReady = false;
bool showArchived = false;
bool showCompleted = true;
// Not sure where else to put this state
bool allowRetranscription = false;

Future<Outline> createOutline(String name, String emoji) async {
final now = DateTime.now();
Expand Down Expand Up @@ -89,6 +92,18 @@ class OutlinesModel extends ChangeNotifier {
notifyListeners();
}

void setShowCompleted(bool show) {
showCompleted = show;
prefs.setBool(showCompletedKey, showCompleted);
notifyListeners();
}

void setAllowRetranscription(bool allow) {
allowRetranscription = allow;
prefs.setBool(allowRetranscriptionKey, allowRetranscription);
notifyListeners();
}

Future<void> load(PlayerModel playerModel, DBRepository db) async {
if (db.ready && !isReady) {
Sentry.addBreadcrumb(
Expand All @@ -97,6 +112,8 @@ class OutlinesModel extends ChangeNotifier {
_playerModel = playerModel;
prefs = await SharedPreferences.getInstance();
showArchived = prefs.getBool(showArchivedKey) ?? false;
showCompleted = prefs.getBool(showCompletedKey) ?? true;
allowRetranscription = prefs.getBool(allowRetranscriptionKey) ?? false;
await loadOutlines();
await ifShouldBackup();
}
Expand Down
26 changes: 13 additions & 13 deletions lib/views/notes_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class _NotesView extends StatefulWidget {
class _NotesViewState extends State<_NotesView> {
Offset? tapXY;
final _textController = TextEditingController();

@override
void dispose() {
super.dispose();
Expand Down Expand Up @@ -162,15 +161,16 @@ class _NotesViewState extends State<_NotesView> {
value: "create_text",
child: ListTile(
leading: Icon(Icons.text_fields), title: Text("add text note"))),
PopupMenuItem(
value: "show_completed",
child: context.read<NotesModel>().showCompleted
? const ListTile(
leading: Icon(Icons.unpublished),
title: Text("hide completed"))
: const ListTile(
leading: Icon(Icons.check_circle),
title: Text("show completed"))),
// TODO: outline-specific completion toggle - keep here for this future
// PopupMenuItem(
// value: "show_completed",
// child: context.read<NotesModel>().showCompleted
// ? const ListTile(
// leading: Icon(Icons.unpublished),
// title: Text("hide completed"))
// : const ListTile(
// leading: Icon(Icons.check_circle),
// title: Text("show completed"))),
const PopupMenuDivider(),
const PopupMenuItem(
value: "rename",
Expand Down Expand Up @@ -267,8 +267,8 @@ class _NotesViewState extends State<_NotesView> {
} else if (item == "time") {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(outline.dateCreated.toLocal().toString())));
} else if (item == "show_completed") {
context.read<NotesModel>().toggleShowCompleted();
// } else if (item == "show_completed") {
// context.read<NotesModel>().toggleShowCompleted();
} else if (item == "archive" || item == "unarchive") {
_toggleArchive();
} else if (item == "create_text") {
Expand Down Expand Up @@ -332,7 +332,7 @@ class _NotesViewState extends State<_NotesView> {
final playerState =
context.select<PlayerModel, PlayerState>((value) => value.playerState);
final showCompleted =
context.select<NotesModel, bool>((value) => value.showCompleted);
context.select<OutlinesModel, bool>((value) => value.showCompleted);
return Scaffold(
backgroundColor:
MediaQuery.of(context).platformBrightness == Brightness.light
Expand Down
66 changes: 46 additions & 20 deletions lib/views/settings_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class _SettingsViewState extends State<SettingsView> {
Widget build(BuildContext context) {
final showArchived =
context.select<OutlinesModel, bool>((value) => value.showArchived);
final showCompleted =
context.select<OutlinesModel, bool>((value) => value.showCompleted);
final allowRetranscription = context
.select<OutlinesModel, bool>((value) => value.allowRetranscription);
final shouldTranscribe = (isInited
? (sharedPreferences.getBool(shouldTranscribeKey) ?? true)
: false);
return Scaffold(
appBar: AppBar(
title: const Text("Settings"),
Expand All @@ -119,8 +126,7 @@ class _SettingsViewState extends State<SettingsView> {
secondary: const Icon(Icons.voicemail),
title: const Text("Transcribe Recordings"),
subtitle: const Text("uses local transcription"),
value: sharedPreferences.getBool(shouldTranscribeKey) ??
true,
value: shouldTranscribe,
onChanged: (v) async {
if (Platform.isIOS && v) {
final res = await tryTxPermissionIOS();
Expand All @@ -136,7 +142,7 @@ class _SettingsViewState extends State<SettingsView> {
sharedPreferences.setBool(shouldTranscribeKey, v);
});
}),
if (sharedPreferences.getBool(shouldTranscribeKey) ?? true)
if (shouldTranscribe)
ListTile(
leading: const Icon(Icons.language),
trailing: const Icon(Icons.arrow_forward_ios),
Expand All @@ -159,46 +165,66 @@ class _SettingsViewState extends State<SettingsView> {
builder: (_) =>
const VoskTranscriptionSetupView())),
),
ListTile(
leading: const Icon(Icons.backup),
title: const Text("Backup"),
trailing: const Icon(Icons.arrow_forward_ios),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const DriveSettingsView())),
),
SwitchListTile(
secondary: const Icon(Icons.location_pin),
title: const Text("Attach Location"),
subtitle: const Text("remember where you took a note"),
value: sharedPreferences.getBool(shouldLocateKey) ?? false,
onChanged: toggleLocation,
),
SwitchListTile(
secondary: const Icon(Icons.check_circle),
title: const Text("Show Completed Notes"),
value: showCompleted,
onChanged: (v) {
context.read<OutlinesModel>().setShowCompleted(v);
},
),
SwitchListTile(
secondary: const Icon(Icons.archive),
title: const Text("Show Archived Outlines"),
value: showArchived,
onChanged: (_) =>
context.read<OutlinesModel>().toggleShowArchived(),
),
ListTile(
leading: const Icon(Icons.backup),
title: const Text("Backup"),
trailing: const Icon(Icons.arrow_forward_ios),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const DriveSettingsView())),
),
if (shouldTranscribe)
SwitchListTile(
secondary: const Icon(Icons.sms),
title: const Text("Show Re-transcription Option"),
subtitle: const Text("for language change, other issues"),
value: allowRetranscription,
onChanged: (v) {
context
.read<OutlinesModel>()
.setAllowRetranscription(v);
},
),
ListTile(
leading: const Icon(Icons.favorite),
title: const Text("Send Tip"),
onTap: () =>
launch("https://github.com/sponsors/maxkrieger"),
onTap: () => launchUrl(
Uri.parse("https://github.com/sponsors/maxkrieger")),
),
ListTile(
leading: const Icon(Icons.bug_report),
title: const Text("Report Issue"),
onTap: () => launch(
"https://github.com/maxkrieger/voiceliner/issues"),
onTap: () => launchUrl(Uri.parse(
"https://github.com/maxkrieger/voiceliner/issues")),
),
ListTile(
leading: const Icon(Icons.privacy_tip),
title: const Text("Privacy"),
onTap: () => launch(
"https://gist.github.com/maxkrieger/301352ae9b7a9e51f49d843fb851d823"),
onTap: () => launchUrl(Uri.parse(
"https://gist.github.com/maxkrieger/301352ae9b7a9e51f49d843fb851d823")),
),
AboutListTile(
icon: const Icon(Icons.info),
Expand All @@ -208,8 +234,8 @@ class _SettingsViewState extends State<SettingsView> {
textAlign: TextAlign.center,
),
TextButton(
onPressed: () => launch(
"https://github.com/maxkrieger/voiceliner"),
onPressed: () => launchUrl(Uri.parse(
("https://github.com/maxkrieger/voiceliner"))),
child: Text(
"fork on GitHub",
style: TextStyle(
Expand Down
10 changes: 7 additions & 3 deletions lib/widgets/note_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:share_plus/share_plus.dart';
import 'package:timeago_flutter/timeago_flutter.dart';
import 'package:voice_outliner/data/note.dart';
import 'package:voice_outliner/state/notes_state.dart';
import 'package:voice_outliner/state/outline_state.dart';
import 'package:voice_outliner/state/player_state.dart';
import 'package:voice_outliner/widgets/note_wizard.dart';

Expand Down Expand Up @@ -100,6 +101,9 @@ class _NoteItemState extends State<NoteItem> {

List<PopupMenuEntry<String>> _menuBuilder(BuildContext context) {
final note = context.read<NotesModel>().notes.elementAt(widget.num);
final shouldTranscribe = context.read<NotesModel>().shouldTranscribe;
final allowRetranscription =
context.read<OutlinesModel>().allowRetranscription;
final isTranscribing =
context.read<NotesModel?>()?.isNoteTranscribing(note) ?? false;
return [
Expand All @@ -118,11 +122,11 @@ class _NoteItemState extends State<NoteItem> {
value: "locate",
child: ListTile(
leading: Icon(Icons.location_pin), title: Text("location"))),
if (note.transcript == null)
if (allowRetranscription)
const PopupMenuItem(
value: "transcribe",
child:
ListTile(leading: Icon(Icons.sms), title: Text("transcribe"))),
child: ListTile(
leading: Icon(Icons.sms), title: Text("re-transcribe"))),
const PopupMenuItem(
value: "delete",
child: ListTile(leading: Icon(Icons.delete), title: Text("delete"))),
Expand Down

0 comments on commit 4252c01

Please sign in to comment.