1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-10 04:52:38 +01:00

Create activity refresh observable

This commit is contained in:
Allan Wang 2017-06-04 10:45:23 -07:00
parent 6fa685433e
commit 4b78e433e1
6 changed files with 69 additions and 27 deletions

View File

@ -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<CookieModel> by lazy { cookies() }
var titleDisposable: Disposable? = null
var refreshObservable = PublishSubject.create<Unit>().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();
}
}

View File

@ -0,0 +1,8 @@
package com.pitchedapps.frost
/**
* Created by Allan Wang on 2017-06-04.
*/
class SelectorActivity : BaseLeakActivity() {
}

View File

@ -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
}

View File

@ -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()
}

View File

@ -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<out Activity>, cookieList: ArrayList<CookieModel> = arrayListOf()) {
fun Context.launchNewTask(clazz: Class<out Activity>, cookieList: ArrayList<CookieModel> = 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()

View File

@ -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)