From f1dcecc6cf376a25ab02154eb6133abdbf00910d Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sat, 5 Oct 2019 22:04:03 +0200 Subject: [PATCH] Added link opening on homepage --- .../magisk/model/events/OpenInappLinkEvent.kt | 32 +++++++++++++++++++ .../magisk/redesign/home/HomeViewModel.kt | 3 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/events/OpenInappLinkEvent.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/model/events/OpenInappLinkEvent.kt b/app/src/main/java/com/topjohnwu/magisk/model/events/OpenInappLinkEvent.kt new file mode 100644 index 000000000..522b1c88f --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/events/OpenInappLinkEvent.kt @@ -0,0 +1,32 @@ +package com.topjohnwu.magisk.model.events + +import android.content.Context +import android.content.res.Resources +import android.util.TypedValue +import androidx.annotation.AttrRes +import androidx.browser.customtabs.CustomTabsIntent +import androidx.core.net.toUri +import com.skoumal.teanity.viewevents.ViewEvent +import com.topjohnwu.magisk.R + +data class OpenInappLinkEvent( + private val link: String +) : ViewEvent(), ContextExecutor { + + // todo find app that can open the link and as a fallback open custom tabs! it shouldn't be the default + override fun invoke(context: Context) = CustomTabsIntent.Builder() + .setShowTitle(true) + .setToolbarColor(context.themedColor(R.attr.colorSurface)) + .enableUrlBarHiding() + .build() + .launchUrl(context, link.toUri()) + + private fun Context.themedColor(@AttrRes attribute: Int) = theme + .resolveAttribute(attribute).data + + private fun Resources.Theme.resolveAttribute( + @AttrRes attribute: Int, + resolveRefs: Boolean = true + ) = TypedValue().also { resolveAttribute(attribute, it, resolveRefs) } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt index 6bd373ea9..3a1da8894 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt @@ -12,6 +12,7 @@ import com.topjohnwu.magisk.model.entity.MagiskJson import com.topjohnwu.magisk.model.entity.ManagerJson import com.topjohnwu.magisk.model.entity.UpdateInfo import com.topjohnwu.magisk.model.entity.recycler.HomeItem +import com.topjohnwu.magisk.model.events.OpenInappLinkEvent import com.topjohnwu.magisk.model.observer.Observer import com.topjohnwu.magisk.redesign.compat.CompatViewModel import com.topjohnwu.magisk.ui.home.MagiskState @@ -70,7 +71,7 @@ class HomeViewModel( } fun onDeletePressed() {} - fun onLinkPressed(link: String) {} + fun onLinkPressed(link: String) = OpenInappLinkEvent(link).publish() }