|
package net.broachcutter.vendorapp.util |
|
|
|
import android.content.Context |
|
import android.content.Intent |
|
import android.os.Bundle |
|
import androidx.appcompat.app.AppCompatActivity |
|
import android.widget.Toast |
|
import ru.terrakok.cicerone.Navigator |
|
import ru.terrakok.cicerone.commands.* |
|
|
|
@Suppress("UNUSED_PARAMETER") |
|
abstract class SupportActivityNavigator(val activity: AppCompatActivity) : Navigator { |
|
|
|
override fun applyCommands(commands: Array<out Command>) { |
|
for (command in commands) { |
|
applyCommand(command) |
|
} |
|
} |
|
|
|
/** |
|
* Perform transition described by the navigation command |
|
* |
|
* @param command the navigation command to apply |
|
*/ |
|
private fun applyCommand(command: Command) { |
|
when (command) { |
|
is Forward -> forward(command) |
|
is Back -> back() |
|
is Replace -> replace(command) |
|
is BackTo -> backTo(command) |
|
is SystemMessage -> showSystemMessage(command.message) |
|
} |
|
} |
|
|
|
private fun backTo(command: BackTo) { |
|
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. |
|
} |
|
|
|
/** |
|
* Override this method to create option for start activity |
|
* |
|
* @param command current navigation command. Will be only [Forward] or [Replace] |
|
* @param activityIntent activity intent |
|
* @return transition options |
|
*/ |
|
private fun createStartActivityOptions(command: Command, activityIntent: Intent): Bundle? { |
|
return null |
|
} |
|
|
|
private fun back() { |
|
exit() |
|
} |
|
|
|
private fun forward(command: Forward) { |
|
val activityIntent = |
|
createActivityIntent(activity, command.screenKey, command.transitionData) |
|
|
|
// Start activity |
|
if (activityIntent != null) { |
|
val options = createStartActivityOptions(command, activityIntent) |
|
checkAndStartActivity(command.screenKey, activityIntent, options) |
|
} |
|
} |
|
|
|
private fun replace(command: Replace) { |
|
val activityIntent = |
|
createActivityIntent(activity, command.screenKey, command.transitionData) |
|
|
|
// Replace activity |
|
if (activityIntent != null) { |
|
val options = createStartActivityOptions(command, activityIntent) |
|
checkAndStartActivity(command.screenKey, activityIntent, options) |
|
activity.finish() |
|
} |
|
} |
|
|
|
private fun checkAndStartActivity(screenKey: String, activityIntent: Intent, options: Bundle?) { |
|
// Check if we can start activity |
|
if (activityIntent.resolveActivity(activity.packageManager) != null) { |
|
activity.startActivity(activityIntent, options) |
|
} else { |
|
unexistingActivity(screenKey, activityIntent) |
|
} |
|
} |
|
|
|
/** |
|
* Called when there is no activity to open `screenKey`. |
|
* |
|
* @param screenKey screen key |
|
* @param activityIntent intent passed to start Activity for the `screenKey` |
|
*/ |
|
private fun unexistingActivity(screenKey: String, activityIntent: Intent) { |
|
// Do nothing by default |
|
} |
|
|
|
/** |
|
* Creates Intent to start Activity for `screenKey`. |
|
* |
|
* |
|
* **Warning:** This method does not work with [BackTo] command. |
|
* |
|
* |
|
* @param screenKey screen key |
|
* @param data initialization data, can be null |
|
* @return intent to start Activity for the passed screen key |
|
*/ |
|
protected abstract fun createActivityIntent( |
|
context: Context, |
|
screenKey: String, |
|
data: Any |
|
): Intent? |
|
|
|
private fun showSystemMessage(message: String) { |
|
// Toast by default |
|
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show() |
|
} |
|
|
|
private fun exit() { |
|
// Finish by default |
|
activity.finish() |
|
} |
|
} |