Skip to content

Commit 41cbf35

Browse files
committed
refactored executeNavigationCommand
1 parent 08e49fe commit 41cbf35

5 files changed

Lines changed: 51 additions & 143 deletions

File tree

app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ plugins {
33
id 'org.jetbrains.kotlin.android'
44
id 'kotlin-parcelize'
55
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
6-
id 'org.jetbrains.dokka'
76

87
}
98

app/src/main/java/com/nlinterface/activities/GroceryListActivity.kt

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.nlinterface.dataclasses.GroceryItem
2424
import com.nlinterface.interfaces.GroceryListCallback
2525
import com.nlinterface.utility.ActivityType
2626
import com.nlinterface.utility.STTInputType
27+
import com.nlinterface.utility.navToActivity
2728
import com.nlinterface.utility.setViewRelativeSize
2829
import com.nlinterface.viewmodels.GroceryListViewModel
2930
import kotlinx.coroutines.CoroutineScope
@@ -458,54 +459,24 @@ class GroceryListActivity : AppCompatActivity(), GroceryListCallback {
458459
*/
459460
private fun executeNavigationCommand(command: String) {
460461

461-
if ((command == resources.getString(R.string.navigate_to_grocery_list))) {
462-
navToActivity(ActivityType.GROCERYLIST)
463-
} else if ((command == resources.getString(R.string.navigate_to_place_details))) {
464-
navToActivity(ActivityType.PLACEDETAILS)
465-
} else if ((command == resources.getString(R.string.navigate_to_settings))) {
466-
navToActivity(ActivityType.SETTINGS)
467-
} else if ((command == resources.getString(R.string.navigate_to_main_menu))) {
468-
navToActivity(ActivityType.MAIN)
469-
} else {
470-
viewModel.say(resources.getString(R.string.invalid_command))
462+
when (command) {
463+
resources.getString(R.string.navigate_to_grocery_list) ->
464+
navToActivity(this, ActivityType.GROCERYLIST)
465+
466+
resources.getString(R.string.navigate_to_place_details) ->
467+
navToActivity(this, ActivityType.PLACEDETAILS)
468+
469+
resources.getString(R.string.navigate_to_settings) ->
470+
navToActivity(this, ActivityType.SETTINGS)
471+
472+
resources.getString(R.string.navigate_to_main_menu) ->
473+
navToActivity(this, ActivityType.MAIN)
474+
475+
else -> viewModel.say(resources.getString(R.string.invalid_command))
471476
}
472477

473478
}
474479

475-
/**
476-
* Handles navigation to next activity. Called either by button click or by execution of the
477-
* voice command. If the called for activity is the current one, read out the activity name.
478-
*
479-
* @param activity: ActivityType, Enum specifying the activity
480-
*/
481-
private fun navToActivity(activity: ActivityType) {
482-
483-
Log.println(Log.DEBUG, "navToActivity", activity.toString())
484-
485-
when (activity) {
486-
487-
ActivityType.GROCERYLIST -> {
488-
viewModel.say(resources.getString(R.string.grocery_list))
489-
}
490-
491-
ActivityType.MAIN -> {
492-
val intent = Intent(this, MainActivity::class.java)
493-
this.startActivity(intent)
494-
}
495-
496-
ActivityType.PLACEDETAILS -> {
497-
val intent = Intent(this, PlaceDetailsActivity::class.java)
498-
this.startActivity(intent)
499-
}
500-
501-
ActivityType.SETTINGS -> {
502-
val intent = Intent(this, SettingsActivity::class.java)
503-
this.startActivity(intent)
504-
}
505-
506-
}
507-
}
508-
509480
/**
510481
* Called when voiceActivationButton is clicked and handles the result. If clicked while the
511482
* STT system is listening, call to viewModel to cancel listening. Else, call viewModel to begin

app/src/main/java/com/nlinterface/activities/MainActivity.kt

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -174,55 +174,20 @@ class MainActivity : AppCompatActivity() {
174174
*/
175175
private fun executeNavigationCommand(command: String) {
176176

177-
if ((command == resources.getString(R.string.navigate_to_grocery_list))) {
178-
navToActivity(ActivityType.GROCERYLIST)
179-
180-
} else if ((command == resources.getString(R.string.navigate_to_place_details))) {
181-
navToActivity(ActivityType.PLACEDETAILS)
182-
183-
} else if ((command == resources.getString(R.string.navigate_to_settings))) {
184-
navToActivity(ActivityType.SETTINGS)
185-
186-
} else if ((command == resources.getString(R.string.navigate_to_main_menu))) {
187-
navToActivity(ActivityType.MAIN)
188-
189-
} else {
190-
viewModel.say(resources.getString(R.string.invalid_command))
191-
}
177+
when (command) {
178+
resources.getString(R.string.navigate_to_grocery_list) ->
179+
navToActivity(this, ActivityType.GROCERYLIST)
192180

193-
}
194-
195-
/**
196-
* Handles navigation to next activity. Called either by button click or by execution of the
197-
* voice command. If the called for activity is the current one, read out the activity name.
198-
*
199-
* @param activity: ActivityType, Enum specifying the activity
200-
*/
201-
private fun navToActivity(activity: ActivityType) {
181+
resources.getString(R.string.navigate_to_place_details) ->
182+
navToActivity(this, ActivityType.PLACEDETAILS)
202183

203-
Log.println(Log.DEBUG, "navToActivity", activity.toString())
184+
resources.getString(R.string.navigate_to_settings) ->
185+
navToActivity(this, ActivityType.SETTINGS)
204186

205-
when (activity) {
206-
207-
ActivityType.MAIN -> {
208-
viewModel.say(resources.getString(R.string.main_menu))
209-
}
210-
211-
ActivityType.GROCERYLIST -> {
212-
val intent = Intent(this, GroceryListActivity::class.java)
213-
this.startActivity(intent)
214-
}
215-
216-
ActivityType.PLACEDETAILS -> {
217-
val intent = Intent(this, PlaceDetailsActivity::class.java)
218-
this.startActivity(intent)
219-
}
220-
221-
ActivityType.SETTINGS -> {
222-
val intent = Intent(this, SettingsActivity::class.java)
223-
this.startActivity(intent)
224-
}
225-
187+
resources.getString(R.string.navigate_to_main_menu) ->
188+
navToActivity(this, ActivityType.MAIN)
189+
190+
else -> viewModel.say(resources.getString(R.string.invalid_command))
226191
}
227192

228193
}
@@ -236,19 +201,19 @@ class MainActivity : AppCompatActivity() {
236201
// set up button to navigate to GroceryListActivity
237202
val groceryListButton: Button = findViewById<View>(R.id.grocery_list_bt) as Button
238203
groceryListButton.setOnClickListener { _ ->
239-
navToActivity(ActivityType.GROCERYLIST)
204+
navToActivity(this, ActivityType.GROCERYLIST)
240205
}
241206

242207
// set up button to navigate to PlaceDetailsActivity
243208
val placeDetailsButton: Button = findViewById<View>(R.id.place_details_bt) as Button
244209
placeDetailsButton.setOnClickListener { _ ->
245-
navToActivity(ActivityType.PLACEDETAILS)
210+
navToActivity(this, ActivityType.PLACEDETAILS)
246211
}
247212

248213
// set up button to navigate to SettingsActivity
249214
val settingsActivityButton: Button = findViewById<View>(R.id.settings_bt) as Button
250215
settingsActivityButton.setOnClickListener { _ ->
251-
navToActivity(ActivityType.SETTINGS)
216+
navToActivity(this, ActivityType.SETTINGS)
252217
}
253218

254219
// set up voice Activation Button listener

app/src/main/java/com/nlinterface/activities/PlaceDetailsActivity.kt

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.nlinterface.dataclasses.PlaceDetailsItem
2525
import com.nlinterface.interfaces.PlaceDetailsItemCallback
2626
import com.nlinterface.utility.ActivityType
2727
import com.nlinterface.utility.STTInputType
28+
import com.nlinterface.utility.navToActivity
2829
import com.nlinterface.utility.setViewRelativeSize
2930
import com.nlinterface.viewmodels.PlaceDetailsViewModel
3031
import kotlinx.coroutines.CoroutineScope
@@ -397,51 +398,23 @@ class PlaceDetailsActivity : AppCompatActivity(), PlaceDetailsItemCallback {
397398
* @param command: String, the command to be executed
398399
*/
399400
private fun executeNavigationCommand(command: String) {
401+
402+
when (command) {
403+
resources.getString(R.string.navigate_to_grocery_list) ->
404+
navToActivity(this, ActivityType.GROCERYLIST)
400405

401-
if ((command == resources.getString(R.string.navigate_to_grocery_list))) {
402-
navToActivity(ActivityType.GROCERYLIST)
403-
} else if ((command == resources.getString(R.string.navigate_to_place_details))) {
404-
navToActivity(ActivityType.PLACEDETAILS)
405-
} else if ((command == resources.getString(R.string.navigate_to_settings))) {
406-
navToActivity(ActivityType.SETTINGS)
407-
} else if ((command == resources.getString(R.string.navigate_to_main_menu))) {
408-
navToActivity(ActivityType.MAIN)
409-
} else {
410-
viewModel.say(resources.getString(R.string.invalid_command))
411-
}
406+
resources.getString(R.string.navigate_to_place_details) ->
407+
navToActivity(this, ActivityType.PLACEDETAILS)
412408

413-
}
414-
415-
/**
416-
* Handles navigation to next activity. Called either by button click or by execution of the
417-
* voice command. If the called for activity is the current one, read out the activity name.
418-
*
419-
* @param activity: ActivityType, Enum specifying the activity
420-
*/
421-
private fun navToActivity(activity: ActivityType) {
409+
resources.getString(R.string.navigate_to_settings) ->
410+
navToActivity(this, ActivityType.SETTINGS)
422411

423-
when (activity) {
424-
425-
ActivityType.PLACEDETAILS -> {
426-
viewModel.say(resources.getString(R.string.place_details))
427-
}
428-
429-
ActivityType.MAIN -> {
430-
val intent = Intent(this, MainActivity::class.java)
431-
this.startActivity(intent)
432-
}
433-
434-
ActivityType.GROCERYLIST -> {
435-
val intent = Intent(this, GroceryListActivity::class.java)
436-
this.startActivity(intent)
437-
}
438-
439-
ActivityType.SETTINGS -> {
440-
val intent = Intent(this, SettingsActivity::class.java)
441-
this.startActivity(intent)
442-
}
443-
412+
resources.getString(R.string.navigate_to_main_menu) ->
413+
navToActivity(this, ActivityType.MAIN)
414+
415+
else -> viewModel.say(resources.getString(R.string.invalid_command))
444416
}
417+
445418
}
446419

447420
/**

app/src/main/java/com/nlinterface/activities/SettingsActivity.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,20 +401,20 @@ class SettingsActivity : AppCompatActivity() {
401401
* @param command: String, the command to be executed
402402
*/
403403
private fun executeNavigationCommand(command: String) {
404-
404+
405405
when (command) {
406406
resources.getString(R.string.navigate_to_grocery_list) ->
407407
navToActivity(this, ActivityType.GROCERYLIST)
408-
408+
409409
resources.getString(R.string.navigate_to_place_details) ->
410-
navToActivity(this, ActivityType.GROCERYLIST)
411-
410+
navToActivity(this, ActivityType.PLACEDETAILS)
411+
412412
resources.getString(R.string.navigate_to_settings) ->
413-
navToActivity(this, ActivityType.GROCERYLIST)
414-
413+
navToActivity(this, ActivityType.SETTINGS)
414+
415415
resources.getString(R.string.navigate_to_main_menu) ->
416-
navToActivity(this, ActivityType.GROCERYLIST)
417-
416+
navToActivity(this, ActivityType.MAIN)
417+
418418
else -> viewModel.say(resources.getString(R.string.invalid_command))
419419
}
420420

0 commit comments

Comments
 (0)