chat-sdk / chat-sdk-android

Chat SDK Android - Open Source Mobile Messenger

Home Page:https://chatsdk.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Threads aren't appearing immediately after authentication

anonym24 opened this issue · comments

  1. Is the bug present in the demo Chat SDK project?
    no, but demo opens another activity (MainAppBarActivity) after authentication (SplashScreenActivity or LoginActivity), and mb because of this delay between switching activities the bug isn't reproduced, and all it asks for permission before displaying threads, my sample uses the same activity, everything happens fast, after authentication it immediately calls safeReloadData method on PrivateThreadsFragment

  2. What modifications have you made to the Chat SDK?
    Used one activity for authentication and adding PrivateThreadsFragment in that activity

  3. Android Version:
    Any, e.g. API 30

  4. Steps taken to reproduce the problem:
    after authentication ChatSDK.auth().authenticate(details) in rx callback subscribe method try to call fragment.safeReloadData() method immediately

  5. Expected result:
    chat private threads should have been appeared

  6. Actual result:
    nothing appeared

  7. Comments:
    only appears if you call fragment.safeReloadData() method with some delay, e.g. 1 second after subscribe method was called

Full Code:

class MainActivityTest : AppCompatActivity() {

    private var authenticationDisposable: Disposable? = null

    private lateinit var fragment: PrivateThreadsFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        fragment = openPrivateThreadsFragment()

        if (ChatSDK.auth().isAuthenticatedThisSession) {
            // do nothing
        } else if (ChatSDK.auth().isAuthenticated || ChatSDK.auth().isAuthenticating) {
            authenticationDisposable = ChatSDK.auth().authenticate()
                .observeOn(RX.main())
                .subscribe(
                    { fragment.safeReloadData() },
                    { Log.i(TAG, "authenticate error $it") }
                )
        } else {
            val details = AccountDetails.username("test@test.com", "testtest")
            authenticationDisposable = ChatSDK.auth().authenticate(details)
                .observeOn(RX.main())
                .subscribe(
                    {
                        fragment.safeReloadData() // - TODO: WON'T WORK, WHY?
                        
                        // TODO: some bug on first authentication, we need to put delay otherwise nothing will be updated
                       //  Handler(Looper.getMainLooper()).postDelayed( {
                       //      fragment.safeReloadData()
                        // }, 2000L)
                        
                        Log.i(TAG, "authenticate ok ${ChatSDK.auth().isAuthenticated}")
                    },
                    { Log.i(TAG, "authenticate error $it") }
                )
        }
    }

    override fun onResume() {
        super.onResume()
        fragment.safeReloadData()
    }

    override fun onDestroy() {
        authenticationDisposable?.dispose()
        super.onDestroy()
    }

    private fun openPrivateThreadsFragment(): PrivateThreadsFragment {
        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        var fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG) as PrivateThreadsFragment?
        if (fragment == null) {
            fragment = PrivateThreadsFragment()
            transaction.replace(R.id.frame_layout, fragment,FRAGMENT_TAG)
        } else {
            transaction.show(fragment)
        }
        transaction.commit()

        return fragment
    }

    companion object {
        private const val TAG: String = "TEST_AUTHENTICATION"
        private val FRAGMENT_TAG: String = PrivateThreadsFragment::class.java.name
    }
}

SDK init:

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        ChatSDKFirebase.quickStart(
            this,
            "pre_1",
            "",
            true
        )
    }
}

So threads are appearing only after calling safeReloadData method with delay or after this app with that activity launched the next time (when a user is authenticated already)

Screenshot_1

Sometimes it displays part of information only (only 1 item of 2, and no name of user):

image

Is there some synchronization happens after authentication (to local datebase for example)?

but everything works ok after launching the app again when user is already authenticated

also Log.i(TAG, "authenticate ok ${ChatSDK.auth().isAuthenticated}") isn't being called until you post it to main handler

I have that same issue