From 4b78e433e1f55b278623c84d1223e42cab875be6 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 4 Jun 2017 10:45:23 -0700 Subject: Create activity refresh observable --- .../kotlin/com/pitchedapps/frost/MainActivity.kt | 27 +++++++------------- .../com/pitchedapps/frost/SelectorActivity.kt | 8 ++++++ .../com/pitchedapps/frost/facebook/FbCookie.kt | 6 ++--- .../com/pitchedapps/frost/fragments/WebFragment.kt | 22 +++++++++++++++- .../com/pitchedapps/frost/utils/ContextUtils.kt | 4 +-- .../com/pitchedapps/frost/web/FrostWebViewCore.kt | 29 ++++++++++++++++++++-- 6 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt (limited to 'app/src/main/kotlin/com') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index 7d46ef50..24497d1c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -7,7 +7,6 @@ import android.support.design.widget.TabLayout import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentPagerAdapter import android.support.v4.view.ViewPager -import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.Menu import android.view.MenuItem @@ -23,16 +22,14 @@ import com.mikepenz.materialdrawer.Drawer import com.pitchedapps.frost.dbflow.CookieModel import com.pitchedapps.frost.dbflow.loadFbTabs import com.pitchedapps.frost.dbflow.saveAsync -import com.pitchedapps.frost.events.FbAccountEvent import com.pitchedapps.frost.facebook.FbCookie.switchUser import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL import com.pitchedapps.frost.fragments.WebFragment import com.pitchedapps.frost.utils.* +import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable -import org.greenrobot.eventbus.EventBus -import org.greenrobot.eventbus.Subscribe -import org.greenrobot.eventbus.ThreadMode +import io.reactivex.subjects.PublishSubject class MainActivity : BaseLeakActivity() { @@ -45,6 +42,7 @@ class MainActivity : BaseLeakActivity() { lateinit var drawerHeader: AccountHeader val cookies: ArrayList by lazy { cookies() } var titleDisposable: Disposable? = null + var refreshObservable = PublishSubject.create().observeOn(AndroidSchedulers.mainThread()) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -94,7 +92,12 @@ class MainActivity : BaseLeakActivity() { fun setupTabs() { viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs)) - tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(viewPager)) + tabs.addOnTabSelectedListener(object : TabLayout.ViewPagerOnTabSelectedListener(viewPager) { + override fun onTabReselected(tab: TabLayout.Tab) { + super.onTabReselected(tab) + currentFragment.web.scrollOrRefresh() + } + }) adapter.pages.forEach { tabs.addTab(tabs.newTab().setIcon(it.icon.toDrawable(this))) } } @@ -171,16 +174,4 @@ class MainActivity : BaseLeakActivity() { override fun getPageTitle(position: Int): CharSequence = getString(pages[position].titleId) } - @Subscribe(threadMode = ThreadMode.MAIN) - fun accountEvent(event: FbAccountEvent) = event.execute(drawerHeader) - - override fun onStart() { - super.onStart(); - EventBus.getDefault().register(this); - } - - override fun onStop() { - EventBus.getDefault().unregister(this); - super.onStop(); - } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt new file mode 100644 index 00000000..85eea240 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt @@ -0,0 +1,8 @@ +package com.pitchedapps.frost + +/** + * Created by Allan Wang on 2017-06-04. + */ +class SelectorActivity : BaseLeakActivity() { + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt index 038eb1a6..af7e086a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -15,7 +15,6 @@ import org.greenrobot.eventbus.EventBus */ object FbCookie { - var dbCookie: String? = null var webCookie: String? get() = CookieManager.getInstance().getCookie(FB_URL_BASE) set(value) { @@ -25,7 +24,7 @@ object FbCookie { operator fun invoke() { L.d("User ${Prefs.userId}") - dbCookie = loadFbCookie(Prefs.userId)?.cookie + val dbCookie = loadFbCookie(Prefs.userId)?.cookie if (dbCookie != null && webCookie == null) { L.d("DbCookie found & WebCookie is null; setting webcookie") webCookie = dbCookie @@ -55,8 +54,7 @@ object FbCookie { fun switchUser(cookie: CookieModel?) { if (cookie == null) return Prefs.userId = cookie.id - dbCookie = cookie.cookie - webCookie = dbCookie + webCookie = cookie.cookie //TODO add webview refresh event } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt index 39c93b01..3c8094aa 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt @@ -1,14 +1,17 @@ package com.pitchedapps.frost.fragments +import android.content.Context import android.os.Bundle import android.support.v4.widget.SwipeRefreshLayout import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import com.pitchedapps.frost.MainActivity import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.putString import com.pitchedapps.frost.web.FrostWebView import com.pitchedapps.frost.web.FrostWebViewCore +import io.reactivex.disposables.Disposable /** * Created by Allan Wang on 2017-05-29. @@ -22,11 +25,12 @@ class WebFragment : BaseFragment() { fun newInstance(position: Int, url: String) = BaseFragment.newInstance(WebFragment(), position).putString(ARG_URL, url) } - val refresh: SwipeRefreshLayout by lazy { frostWebView.refresh } +// val refresh: SwipeRefreshLayout by lazy { frostWebView.refresh } val web: FrostWebViewCore by lazy { frostWebView.web } lateinit var url: String lateinit private var frostWebView: FrostWebView private var firstLoad = true + private var refreshDisposable: Disposable? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -58,5 +62,21 @@ class WebFragment : BaseFragment() { } } + override fun onAttach(context: Context) { + super.onAttach(context) + refreshDisposable?.dispose() + if (context is MainActivity) + refreshDisposable = context.refreshObservable.subscribe { + web.clearHistory() + web.loadBaseUrl() + } + } + + override fun onDetach() { + refreshDisposable?.dispose() + L.d("F Detatch") + super.onDetach() + } + override fun onBackPressed() = frostWebView.onBackPressed() } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt index 13a8a836..702196bb 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt @@ -18,9 +18,9 @@ import com.pitchedapps.frost.facebook.FbTab private const val EXTRA_COOKIES = "extra_cookies" private const val ARG_URL = "arg_url" -fun Context.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf()) { +fun Context.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf(), clearStack: Boolean = true) { val intent = (Intent(this, clazz)) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) intent.putParcelableArrayListExtra(EXTRA_COOKIES, cookieList) startActivity(intent) if (this is Activity) finish() diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt index 34fd4509..ab9a6209 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt @@ -1,5 +1,6 @@ package com.pitchedapps.frost.web +import android.animation.ValueAnimator import android.annotation.SuppressLint import android.content.Context import android.support.v4.view.MotionEventCompat @@ -9,13 +10,14 @@ import android.support.v4.view.ViewCompat import android.util.AttributeSet import android.view.MotionEvent import android.view.View +import android.view.animation.DecelerateInterpolator import android.webkit.WebView import com.pitchedapps.frost.events.FbAccountEvent +import com.pitchedapps.frost.utils.L import io.reactivex.Scheduler import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.subjects.BehaviorSubject -import io.reactivex.subjects.Subject import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode @@ -54,7 +56,7 @@ class FrostWebViewCore @JvmOverloads constructor( @SuppressLint("SetJavaScriptEnabled") fun setupWebview() { settings.javaScriptEnabled = true - settings.domStorageEnabled = true +// settings.domStorageEnabled = true setLayerType(View.LAYER_TYPE_HARDWARE, null) setWebViewClient(FrostWebViewClient(refreshObservable)) setWebChromeClient(FrostChromeClient(progressObservable, titleObservable)) @@ -120,6 +122,29 @@ class FrostWebViewCore @JvmOverloads constructor( super.onDetachedFromWindow() } + /** + * If webview is already at the top, refresh + * Otherwise scroll to top + */ + fun scrollOrRefresh() { + L.d("Scroll or Refresh") + if (scrollY < 5) reload() + else scrollToTop() + } + + fun scrollToTop() { + if (scrollY > 1000) scrollTo(0, 0) + else { + val animator = ValueAnimator.ofInt(scrollY, 0) + animator.duration = scrollY.toLong() + animator.interpolator = DecelerateInterpolator() + animator.addUpdateListener { + scrollY = it.animatedValue as Int + invalidate() + } + } + } + @Subscribe(threadMode = ThreadMode.MAIN) fun webEvent(event: FbAccountEvent) = event.execute(this) -- cgit v1.2.3