Skip to content

Commit

Permalink
all: smoother ui's (fixes #2030) (#2036)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Jun 12, 2024
1 parent 2e04672 commit 5fbd405
Show file tree
Hide file tree
Showing 27 changed files with 279 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ open class FragmentViewModel(application: Application) : AndroidViewModel(applic
/**
* @param toSend : String = A string to send to the Raspberry Pi
*/
fun sendMessage(toSend: String) {
lastCommand = toSend
fun sendMessage(toSend: String?) {
if (toSend != null) {
lastCommand = toSend
}
if (_connectionStatus.value != Constants.STATE_CONNECTED) {
Toast.makeText(getApplication(), "Not Connected to Bluetooth", Toast.LENGTH_LONG).show()
}
else mChatService.write(toSend.toByteArray())
else mChatService.write(toSend?.toByteArray())
}

/**
Expand Down
108 changes: 59 additions & 49 deletions app/src/main/kotlin/io/treehouses/remote/ui/home/BaseHomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,33 @@ open class BaseHomeFragment : BaseFragment() {
* - last time dialog was shows (only show after a week)
* @param preferences : SharedPreferences = Preferences to save the user preferences to
*/
protected fun showLogDialog(preferences: SharedPreferences) {
val connectionCount = preferences.getInt("connection_count", 0)
val lastDialogShown = preferences.getLong("last_dialog_shown", 0)
protected fun showLogDialog(preferences: SharedPreferences?) {
val connectionCount = preferences?.getInt("connection_count", 0)
val lastDialogShown = preferences?.getLong("last_dialog_shown", 0)
val date = Calendar.getInstance()
date.add(Calendar.DAY_OF_YEAR, -7)
val v = layoutInflater.inflate(R.layout.alert_log, null)
val emoji = String(Character.toChars(0x1F60A))
if (lastDialogShown < date.timeInMillis && !preferences.getBoolean("send_log", false)) {
if (connectionCount >= 3) {
preferences.edit().putLong("last_dialog_shown", Calendar.getInstance().timeInMillis).apply()
val builder = DialogUtils.createAlertDialog(activity,
"Sharing is Caring $emoji",
"Treehouses wants to collect your activities. Do you like to share it? It will help us to improve.", v)
.setCancelable(false)
DialogUtils.createAdvancedDialog(builder, Pair("Continue", "Cancel"), {
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION),
REQUEST_LOCATION_PERMISSION_FOR_ACTIVITY_COLLECTION
)
} else {
preferences.edit()?.putBoolean("send_log", true)?.apply()
if (lastDialogShown != null) {
if (lastDialogShown < date.timeInMillis && !preferences.getBoolean("send_log", false)) {
if (connectionCount != null) {
if (connectionCount >= 3) {
preferences.edit().putLong("last_dialog_shown", Calendar.getInstance().timeInMillis).apply()
val builder = DialogUtils.createAlertDialog(activity,
"Sharing is Caring $emoji",
"Treehouses wants to collect your activities. Do you like to share it? It will help us to improve.", v)
.setCancelable(false)
DialogUtils.createAdvancedDialog(builder, Pair("Continue", "Cancel"), {
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION),
REQUEST_LOCATION_PERMISSION_FOR_ACTIVITY_COLLECTION
)
} else {
preferences.edit()?.putBoolean("send_log", true)?.apply()
}
}, { MainApplication.showLogDialog = false })
}
}, { MainApplication.showLogDialog = false })
}
}
}
}
Expand All @@ -119,37 +123,41 @@ open class BaseHomeFragment : BaseFragment() {
* - Should show at the same time as the sharing data dialog
* @param preferences : SharedPreferences = preferences to save user preferences to
*/
protected fun rate(preferences: SharedPreferences) {
val connectionCount = preferences.getInt("connection_count", 0)
val ratingDialog = preferences.getBoolean("ratingDialog", true)
val lastDialogShown = preferences.getLong("last_dialog_shown", 0)
protected fun rate(preferences: SharedPreferences?) {
val connectionCount = preferences?.getInt("connection_count", 0)
val ratingDialog = preferences?.getBoolean("ratingDialog", true)
val lastDialogShown = preferences?.getLong("last_dialog_shown", 0)
val date = Calendar.getInstance()
if (lastDialogShown < date.timeInMillis) {
if (connectionCount >= 3 && ratingDialog) {
val a = DialogUtils.createAlertDialog(activity,"Thank You").setCancelable(false).setMessage("We're so happy to hear that you love the Treehouses app! " +
"It'd be really helpful if you rated us. Thanks so much for spending some time with us.")
.setPositiveButton("RATE IT NOW") { _: DialogInterface?, _: Int ->
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://play.google.com/store/apps/details?id=io.treehouses.remote")
startActivity(intent)
preferences.edit().putBoolean("ratingDialog", false).apply()
}.setNeutralButton("REMIND ME LATER") { _: DialogInterface?, _: Int -> MainApplication.ratingDialog = false }
.setNegativeButton("NO THANKS") { _: DialogInterface?, _: Int -> preferences.edit().putBoolean("ratingDialog", false).apply() }.create()
a.window!!.setBackgroundDrawableResource(android.R.color.transparent)
a.show()
if (lastDialogShown != null) {
if (lastDialogShown < date.timeInMillis) {
if (connectionCount != null) {
if (connectionCount >= 3 && ratingDialog == true) {
val a = DialogUtils.createAlertDialog(activity,"Thank You").setCancelable(false).setMessage("We're so happy to hear that you love the Treehouses app! " +
"It'd be really helpful if you rated us. Thanks so much for spending some time with us.")
.setPositiveButton("RATE IT NOW") { _: DialogInterface?, _: Int ->
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://play.google.com/store/apps/details?id=io.treehouses.remote")
startActivity(intent)
preferences.edit().putBoolean("ratingDialog", false).apply()
}.setNeutralButton("REMIND ME LATER") { _: DialogInterface?, _: Int -> MainApplication.ratingDialog = false }
.setNegativeButton("NO THANKS") { _: DialogInterface?, _: Int -> preferences.edit().putBoolean("ratingDialog", false).apply() }.create()
a.window?.setBackgroundDrawableResource(android.R.color.transparent)
a.show()
}
}
}
}
}

protected fun showDialogOnce(preferences: SharedPreferences) {
val firstTime = preferences.getBoolean(Screens.FIRST_TIME.name, true)
if (firstTime) {
protected fun showDialogOnce(preferences: SharedPreferences?) {
val firstTime = preferences?.getBoolean(Screens.FIRST_TIME.name, true)
if (firstTime == true) {
// showWelcomeDialog()
val i = Intent(activity, IntroActivity::class.java)
startActivity(i)
val editor = preferences.edit()
editor.putBoolean(Screens.FIRST_TIME.name, false)
editor.apply()
editor?.putBoolean(Screens.FIRST_TIME.name, false)
editor?.apply()
}
}

Expand All @@ -173,7 +181,7 @@ open class BaseHomeFragment : BaseFragment() {
animationDrawableGreen.start()
animationDrawableRed.start()
val a = createTestConnectionDialog(mView, dismissable, title, messageID)
a.window!!.setBackgroundDrawableResource(android.R.color.transparent)
a.window?.setBackgroundDrawableResource(android.R.color.transparent)
a.show()
return a
}
Expand All @@ -199,7 +207,7 @@ open class BaseHomeFragment : BaseFragment() {
}
.setNegativeButton("Upgrade Later") { dialog: DialogInterface, _: Int -> dialog.dismiss() }
.create()
alertDialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
alertDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
alertDialog.show()
}

Expand All @@ -212,7 +220,7 @@ open class BaseHomeFragment : BaseFragment() {
val inputStream = context?.assets?.open("bluetooth-server.txt")
val localString = inputStream?.bufferedReader().use { it?.readText() }
inputStream?.close()
val hashed = Utils.hashString(localString!!)
val hashed = localString?.let { Utils.hashString(it) }
//Bluetooth file is outdated, but RPI is connected to the internet
if (Matcher.isError(serverHash) && viewModel.internetStatus.value == true) {
askForBluetoothUpgradeOverInternet()
Expand All @@ -222,8 +230,10 @@ open class BaseHomeFragment : BaseFragment() {
noInternetForBluetoothUpgrade()
}
//If there is no error, compare the server hashes to determine whether an upgrade is needed
else if (hashed.trim() != serverHash.trim() && PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("bluetooth_file_local_upgrade", false)) {
askForBluetoothUpgradeStable(localString)
else if (hashed?.trim() != serverHash.trim() && PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("bluetooth_file_local_upgrade", false)) {
if (localString != null) {
askForBluetoothUpgradeStable(localString)
}
}
}

Expand All @@ -238,7 +248,7 @@ open class BaseHomeFragment : BaseFragment() {
.setPositiveButton("Ok") { d, _ ->
d.dismiss()
}.create()
dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.show()
}

Expand All @@ -252,7 +262,7 @@ open class BaseHomeFragment : BaseFragment() {
viewModel.sendMessage(getString(R.string.TREEHOUSES_UPGRADE_BLUETOOTH_MASTER))
}
.setNegativeButton("Cancel") {dialog, _ -> dialog.dismiss()}.create()
dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.show()
}

Expand All @@ -270,7 +280,7 @@ open class BaseHomeFragment : BaseFragment() {
}.setNegativeButton("Cancel") { dialog: DialogInterface, _: Int ->
dialog.dismiss()
}.create()
dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.show()
}

Expand All @@ -289,7 +299,7 @@ open class BaseHomeFragment : BaseFragment() {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")))
}
}.create()
alertDialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
alertDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
alertDialog.show()
}

Expand Down
28 changes: 14 additions & 14 deletions app/src/main/kotlin/io/treehouses/remote/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class HomeFragment : BaseHomeFragment() {
bind = ActivityHomeFragmentBinding.inflate(inflater, container, false)
preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
setupProfiles()
showDialogOnce(preferences!!)
showDialogOnce(preferences)
connectRpiListener()
testConnectionListener()
return bind.root
Expand All @@ -68,8 +68,8 @@ class HomeFragment : BaseHomeFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
observeConnectionState()
bind.btnGetStarted.setOnClickListener {
instance!!.checkStatusNow()
if (instance!!.hasValidConnection()) {
instance?.checkStatusNow()
if (instance?.hasValidConnection() == true) {
switchFragment(TerminalFragment(), "Terminal")
} else {
switchFragment(AboutFragment(), "About")
Expand Down Expand Up @@ -135,7 +135,7 @@ class HomeFragment : BaseHomeFragment() {
viewModel.networkProfileResult.observe(viewLifecycleOwner, Observer {
when(it.status) {
Status.SUCCESS, Status.ERROR, Status.NOTHING -> {
if (progressDialog != null) progressDialog!!.dismiss()
if (progressDialog != null) progressDialog?.dismiss()
if (it.message.isNotEmpty()) context.toast(it.message)
}
Status.LOADING -> {
Expand All @@ -154,7 +154,7 @@ class HomeFragment : BaseHomeFragment() {
* Switches fragment (To go to Terminal, or about for example)
*/
private fun switchFragment(fragment: Fragment, title: String) {
instance!!.openCallFragment(fragment)
instance?.openCallFragment(fragment)
activity?.let { it.title = title}
}

Expand All @@ -168,9 +168,9 @@ class HomeFragment : BaseHomeFragment() {
if (groupPosition == 3) {
viewModel.sendMessage(getString(R.string.TREEHOUSES_DEFAULT_NETWORK))
context.toast("Switched to Default Network", Toast.LENGTH_LONG)
} else if (SaveUtils.getProfiles(requireContext()).size > 0 && SaveUtils.getProfiles(requireContext())[listOf(*group_labels)[groupPosition]]!!.isNotEmpty()) {
if (SaveUtils.getProfiles(requireContext())[listOf(*group_labels)[groupPosition]]!!.size <= childPosition) return@setOnChildClickListener false
viewModel.networkProfile = SaveUtils.getProfiles(requireContext())[listOf(*group_labels)[groupPosition]]!![childPosition]
} else if (SaveUtils.getProfiles(requireContext()).size > 0 && SaveUtils.getProfiles(requireContext())[listOf(*group_labels)[groupPosition]]?.isNotEmpty() == true) {
if ((SaveUtils.getProfiles(requireContext())[listOf(*group_labels)[groupPosition]]?.size ?: 0) <= childPosition) return@setOnChildClickListener false
viewModel.networkProfile = SaveUtils.getProfiles(requireContext())[listOf(*group_labels)[groupPosition]]?.get(childPosition)
viewModel.sendMessage(getString(R.string.TREEHOUSES_DEFAULT_NETWORK))
requireContext().toast("Configuring...", Toast.LENGTH_LONG)
}
Expand All @@ -186,8 +186,8 @@ class HomeFragment : BaseHomeFragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (MainApplication.showLogDialog) {
rate(preferences!!)
showLogDialog(preferences!!)
rate(preferences)
showLogDialog(preferences)
}
activity?.invalidateOptionsMenu()
}
Expand All @@ -203,7 +203,7 @@ class HomeFragment : BaseHomeFragment() {
else vibe.vibrate(10)
}
if (viewModel.connectionStatus.value == Constants.STATE_CONNECTED) {
RPIDialogFragment.instance!!.bluetoothCheck("unregister")
RPIDialogFragment.instance?.bluetoothCheck("unregister")
viewModel.disconnectBT()
return@setOnClickListener
}
Expand All @@ -228,7 +228,7 @@ class HomeFragment : BaseHomeFragment() {
viewModel.selectedLed = options.indexOf(preference)
viewModel.sendMessage(optionsCode[viewModel.selectedLed])
testConnectionDialog = showTestConnectionDialog(false, "Testing Connection...", R.string.test_connection_message, viewModel.selectedLed)
testConnectionDialog?.window!!.setBackgroundDrawableResource(android.R.color.transparent)
testConnectionDialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
testConnectionDialog?.show()
viewModel.testConnectionResult.value = Resource.loading()
}
Expand All @@ -246,7 +246,7 @@ class HomeFragment : BaseHomeFragment() {
connectionDialog?.dismiss()
when (connected) {
Constants.STATE_CONNECTED -> {
showLogDialog(preferences!!)
showLogDialog(preferences)
viewModel.internetSent = true
viewModel.sendMessage(getString(R.string.TREEHOUSES_INTERNET))
Tutorials.homeTutorials(bind, requireActivity())
Expand Down Expand Up @@ -297,7 +297,7 @@ class HomeFragment : BaseHomeFragment() {
*/
private fun dismissTestConnection() {
if (testConnectionDialog != null) {
testConnectionDialog!!.cancel()
testConnectionDialog?.cancel()
showTestConnectionDialog(true, "Process Finished", R.string.test_finished, viewModel.selectedLed)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class HomeViewModel(application: Application) : FragmentViewModel(application) {
*/
private fun sendLog(deviceName: String, readMessage: List<String>) {
val preferences = PreferenceManager.getDefaultSharedPreferences(getApplication())
val connectionCount = preferences!!.getInt("connection_count", 0)
val connectionCount = preferences.getInt("connection_count", 0)
val sendLog = preferences.getBoolean("send_log", true)
preferences.edit().putInt("connection_count", connectionCount + 1).apply()
if (connectionCount >= 3 && sendLog) {
Expand Down
Loading

0 comments on commit 5fbd405

Please sign in to comment.