Skip to content

Commit

Permalink
Fix #138: Implemented Forem app popup on start (#143)
Browse files Browse the repository at this point in the history
* Implemented Forem app popup on start

* Added bottom bar options

* Improved UX

* Deep-linking finished

* Icon updates
  • Loading branch information
rt4914 authored Jan 21, 2022
1 parent 3f32520 commit e07b9c2
Show file tree
Hide file tree
Showing 15 changed files with 647 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ captures/
.idea/**/misc.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/assetWizardSettings.xml
.idea/libraries
.idea/caches
.idea/caches/build_file_checksums.ser
Expand Down Expand Up @@ -120,3 +121,7 @@ google-service-account-user.json
fastlane/README.md
fastlane/metadata/
fastlane/*.xml

# Extra
*.DS_Store

131 changes: 131 additions & 0 deletions app/src/main/java/to/dev/dev_android/activities/ForemAppDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package to.dev.dev_android.activities

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.DialogFragment
import android.content.pm.PackageManager
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import to.dev.dev_android.R

class ForemAppDialog : DialogFragment() {

companion object {
const val PACKAGE_NAME = "com.forem.android"
private const val FOREM_URL = "ForemAppDialog.url"

fun newInstance(url: String): ForemAppDialog {
val foremAppDialog = ForemAppDialog()
val args = Bundle()
args.putString(FOREM_URL, url)
foremAppDialog.arguments = args
return foremAppDialog
}

fun isForemAppAlreadyInstalled(activity: Activity?): Boolean {
return try {
activity?.packageManager?.getPackageInfo(PACKAGE_NAME, 0)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}

fun openForemApp(activity: Activity?, url: String?) {
val packageManager: PackageManager? = activity?.packageManager
val app = packageManager?.getLaunchIntentForPackage(PACKAGE_NAME)
if (!url.isNullOrEmpty()) {
app?.putExtra(Intent.EXTRA_TEXT, url)
}
activity?.startActivity(app)
}
}

lateinit var url: String

override fun onStart() {
super.onStart()
val width = resources.displayMetrics.widthPixels
dialog!!.window?.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT)
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val args = arguments
url = args?.getString(FOREM_URL) ?: ""

val view = inflater.inflate(R.layout.forem_app_dialog, container, false)
if (dialog != null && dialog!!.window != null) {
dialog!!.window?.setBackgroundDrawableResource(R.drawable.forem_dialog_fragment_background)
}
val downloadInstallForemAppTextView =
view.findViewById<TextView>(R.id.download_install_forem_app_text_view)
val downloadOpenForemAppImageView =
view.findViewById<ImageView>(R.id.download_open_forem_image_view)
val descriptionTextView =
view.findViewById<TextView>(R.id.forem_app_dialog_description_text_view)

if (isForemAppAlreadyInstalled(activity)) {
downloadInstallForemAppTextView.text = getString(R.string.open_forem_app)
downloadOpenForemAppImageView.setImageDrawable(
ContextCompat.getDrawable(
this.requireContext(),
R.drawable.ic_compass
)
)
descriptionTextView.text = getString(R.string.forem_app_dialog_description_if_installed)
} else {
downloadInstallForemAppTextView.text = getString(R.string.download_forem_app)
downloadOpenForemAppImageView.setImageDrawable(
ContextCompat.getDrawable(
this.requireContext(),
R.drawable.ic_baseline_arrow_downward_24
)
)
descriptionTextView.text = getString(R.string.forem_app_dialog_description)
}

val downloadLayout = view.findViewById<ConstraintLayout>(R.id.download_forem_app_layout)

downloadLayout.setOnClickListener {
openForemAppLink()
}
return view
}

private fun openForemAppLink() {
if (isForemAppAlreadyInstalled(activity)) {
openForemApp(activity, url)
} else {
try {
// Opens Forem app in Play Store, if Play Store app is available.
activity?.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=$PACKAGE_NAME")
)
)
} catch (e: ActivityNotFoundException) {
// Opens Forem app on Play Store in browser.
activity?.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$PACKAGE_NAME")
)
)
}
}
this.dismiss()
}
}
28 changes: 27 additions & 1 deletion app/src/main/java/to/dev/dev_android/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
handleIntent(intent)
PushNotifications.start(applicationContext, BuildConfig.pusherInstanceId)
PushNotifications.addDeviceInterest("broadcast")

binding.showPopupImageView.setOnClickListener {
showForemAppAlert()
}

binding.openForemImageView.setOnClickListener {
val url = binding.webView.url
ForemAppDialog.openForemApp(this, url)
}
}

override fun onResume() {
Expand All @@ -50,10 +59,16 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
binding.webView.loadUrl(targetUrl)
}
} catch (e: Exception) {
Log.e(LOG_TAG, e.message)
Log.e(LOG_TAG, "${e.message}")
}
}

if (ForemAppDialog.isForemAppAlreadyInstalled(this)) {
binding.openForemImageView.visibility = View.VISIBLE
} else {
binding.openForemImageView.visibility = View.GONE
}

super.onResume()
webViewClient.observeNetwork()
}
Expand Down Expand Up @@ -107,12 +122,23 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
mainActivityScope
) {
binding.splash.visibility = View.GONE
binding.webView.visibility = View.VISIBLE
binding.bottomLayout.visibility = View.VISIBLE
showForemAppAlert()
}
binding.webView.webViewClient = webViewClient
webViewBridge.webViewClient = webViewClient
binding.webView.webChromeClient = CustomWebChromeClient(BuildConfig.baseUrl, this)
}

private fun showForemAppAlert() {
val url: String = binding.webView.url ?: ""
ForemAppDialog.newInstance(url).show(
supportFragmentManager,
"ForemAppDialogFragment"
)
}

private fun restoreState(savedInstanceState: Bundle) {
binding.webView.restoreState(savedInstanceState)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/rounded_dialog"
android:insetLeft="16dp"
android:insetRight="16dp" />
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_arrow_downward_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"/>
</vector>
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/ic_compass.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="28"
android:viewportHeight="28">
<path
android:pathData="M13.987,28C6.274,28 0,21.714 0,13.987 0,6.275 6.274,0 13.987,0 21.714,0 28,6.275 28,13.987 28,21.714 21.714,28 13.987,28zM13.987,1C6.826,1 1,6.826 1,13.987 1,21.163 6.826,27 13.987,27 21.162,27 27,21.163 27,13.987 27,6.826 21.162,1 13.987,1z"
android:fillColor="#0c0c0c"/>
<path
android:pathData="M6.821,21.179l4.922,-9.436 9.436,-4.923 -4.922,9.436 -9.436,4.923zM12.484,12.485l-3.306,6.336 6.337,-3.306 3.306,-6.336 -6.337,3.306z"
android:fillColor="#0c0c0c"/>
<path
android:pathData="M14,15.499c-0.384,0 -0.769,-0.146 -1.061,-0.438a1.501,1.501 0,0 1,0 -2.121,1.501 1.501,0 0,1 2.121,0 1.501,1.501 0,0 1,0 2.121,1.494 1.494,0 0,1 -1.06,0.438zM14,13.5a0.498,0.498 0,0 0,-0.354 0.853A0.5,0.5 0,1 0,14 13.5z"
android:fillColor="#0c0c0c"/>
</vector>
Loading

0 comments on commit e07b9c2

Please sign in to comment.