Skip to content

Commit

Permalink
feat: handle manual send
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-espie committed Nov 18, 2024
1 parent 622320f commit e70a31a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
36 changes: 31 additions & 5 deletions app/src/main/java/com/geeksville/mesh/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ class MainActivity : AppCompatActivity(), Logging {
val mainTab = tab?.position ?: 0
model.setCurrentTab(mainTab)
}
override fun onTabUnselected(tab: TabLayout.Tab?) { }
override fun onTabReselected(tab: TabLayout.Tab?) { }

override fun onTabUnselected(tab: TabLayout.Tab?) {}
override fun onTabReselected(tab: TabLayout.Tab?) {}
})

// Handle any intent
Expand Down Expand Up @@ -295,10 +296,18 @@ class MainActivity : AppCompatActivity(), Logging {
val json = Json
try {
val intentMessage: IntentMessage = json.decodeFromString(text)
model.sendMessage(intentMessage.message, intentMessage.contactKey)
showMessages(intentMessage.contactKey, intentMessage.contactName)
if (intentMessage.autoSend) {
model.sendMessage(intentMessage.message, intentMessage.contactKey)
showMessages(intentMessage.contactKey, intentMessage.contactName)
} else {
showMessagesPreInit(
intentMessage.contactKey,
intentMessage.contactName,
intentMessage.message
)
}
} catch (e: SerializationException) {
errormsg("Failed to decode JSON: ${e.message}")
debug("Failed to decode JSON: ${e.message}; falling back to default message")
}
}
}
Expand Down Expand Up @@ -632,6 +641,13 @@ class MainActivity : AppCompatActivity(), Logging {
}
}

private fun showMessagesPreInit(contactKey: String?, contactName: String?, message: String?) {
model.setCurrentTab(0)
if (contactKey != null && contactName != null && message != null) {
supportFragmentManager.navigateToPreInitMessages(contactKey, contactName, message)
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
Expand Down Expand Up @@ -662,10 +678,12 @@ class MainActivity : AppCompatActivity(), Logging {
getVersionInfo()
return true
}

R.id.connectStatusImage -> {
Toast.makeText(applicationContext, item.title, Toast.LENGTH_SHORT).show()
return true
}

R.id.debug -> {
val fragmentManager: FragmentManager = supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
Expand All @@ -675,6 +693,7 @@ class MainActivity : AppCompatActivity(), Logging {
fragmentTransaction.commit()
return true
}

R.id.stress_test -> {
fun postPing() {
// Send ping message and arrange delayed recursion.
Expand All @@ -692,10 +711,12 @@ class MainActivity : AppCompatActivity(), Logging {
}
return true
}

R.id.radio_config -> {
supportFragmentManager.navigateToNavGraph()
return true
}

R.id.save_messages_csv -> {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
Expand All @@ -705,18 +726,22 @@ class MainActivity : AppCompatActivity(), Logging {
createDocumentLauncher.launch(intent)
return true
}

R.id.theme -> {
chooseThemeDialog()
return true
}

R.id.preferences_language -> {
chooseLangDialog()
return true
}

R.id.show_intro -> {
startActivity(Intent(this, AppIntroduction::class.java))
return true
}

R.id.preferences_quick_chat -> {
val fragmentManager: FragmentManager = supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
Expand All @@ -726,6 +751,7 @@ class MainActivity : AppCompatActivity(), Logging {
fragmentTransaction.commit()
return true
}

else -> super.onOptionsItemSelected(item)
}
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/geeksville/mesh/ui/MessagesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ internal fun FragmentManager.navigateToMessages(contactKey: String, contactName:
.addToBackStack(null)
.commit()
}
internal fun FragmentManager.navigateToPreInitMessages(contactKey: String, contactName: String, message: String) {
val messagesFragment = MessagesFragment().apply {
arguments = bundleOf("contactKey" to contactKey, "contactName" to contactName, "message" to message)
}
beginTransaction()
.add(R.id.mainActivityLayout, messagesFragment)
.addToBackStack(null)
.commit()
}

@AndroidEntryPoint
class MessagesFragment : Fragment(), Logging {
Expand Down Expand Up @@ -109,6 +118,7 @@ class MessagesFragment : Fragment(), Logging {

contactKey = arguments?.getString("contactKey").toString()
val contactName = arguments?.getString("contactName").toString()
if (arguments?.getString("message") != null) binding.messageInputText.setText(arguments?.getString("message").toString())
binding.toolbar.title = contactName
val channelNumber = contactKey[0].digitToIntOrNull()
if (channelNumber == DataPacket.PKC_CHANNEL_INDEX) {
Expand Down

0 comments on commit e70a31a

Please sign in to comment.