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 loaded correctly on fresh app (with no data) after quick login (with pre-allowed/defined permissions)

anonym24 opened this issue · comments

  1. Is the bug present in the demo Chat SDK project? yes, 5.0.16

  2. What modifications have you made to the Chat SDK? none

  3. Android Version: 28,29,30

  4. Steps taken to reproduce the problem:
    install demo app, login, create some threads with other user, close app, clear data, give all needed permissions in the settings so app would NOT ask for them,
    open app and login to the same user, after login there is no threads or they were partly loaded, some cache error in the project,
    press back, and again open app, now all threads may be will be loaded

  5. Expected result:
    after login on fresh app (with no previous data) it should load threads correctly, but empty list was shown

  6. Actual result:
    no threads were loaded or partly loaded

  7. Comments:
    probably issue with cache (local DB) and missing notification for updating threads (reloading list data) when threads were fully loaded from firebase and were cached to local DB

login on fresh app install (with pre-allowed permissions in the settings, so there was no time for app to load cache fully before threads fragment is opened)

Screenshot_1

threads appeared only after relaunching the app:

Screenshot_2

I can confirm the issue still occurs in latest version

class ChatThreadsFragment : PrivateThreadsFragment() {

    override fun filter(threads: MutableList<Thread>?): MutableList<Thread> {
        Log.i(TAG, "threads count ${threads?.size}")
        return super.filter(threads)
    }

so after fresh reinstall and login it returns 0 size of threads but after restarting the app it's more than 0

and I see that in your ChatSDK code you get threads from db, which still can be empty:

image

so PrivateThreadsFragment & PublicThreadsFragment can easily show empty thread list and it will be like this until you restart the app or reopen the fragment

I noticed this only happens when a user registers. It does not happen when a user logs in to an existing account.
So i did a bandage fix with:

Create the class CoreUtils in the below package

package sdk.chat.ui.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;

import static android.content.ContentValues.TAG;

public class CoreUtils {

public static void doRestart(Context c) {
    try {
        //check if the context is given
        if (c != null) {
            //fetch the packagemanager so we can get the default launch activity
            // (you can replace this intent with any other activity if you want
            PackageManager pm = c.getPackageManager();
            //check if we got the PackageManager
            if (pm != null) {
                //create the intent with the default start activity for your application
                Intent mStartActivity = pm.getLaunchIntentForPackage(
                        c.getPackageName()
                );
                if (mStartActivity != null) {
                    mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    //create a pending intent so the application is restarted after System.exit(0) was called.
                    // We use an AlarmManager to call this intent in 100ms
                    int mPendingIntentId = 223344;
                    PendingIntent mPendingIntent = PendingIntent
                            .getActivity(c, mPendingIntentId, mStartActivity,
                                    PendingIntent.FLAG_CANCEL_CURRENT);
                    AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
                    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                    //kill the application
                    System.exit(0);
                } else {
                    Log.e(TAG, "Was not able to restart application, mStartActivity null");
                }
            } else {
                Log.e(TAG, "Was not able to restart application, PM null");
            }
        } else {
            Log.e(TAG, "Was not able to restart application, Context null");
        }
    } catch (Exception ex) {
        Log.e(TAG, "Was not able to restart application");
    }
}

}

and in PostRegistrationActivity replace void onCreate with

@OverRide
protected void onCreate(@nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initViews();

    if (!StringChecker.isNullOrEmpty(ChatSDK.currentUser().getName())) {
        ChatSDK.ui().startMainActivity(this);
    }
}

and in void next() at the bottom replace

dm.add(ChatSDK.core().pushUser()
.observeOn(RX.main())
.subscribe(() -> {
ChatSDK.ui().startMainActivity(this);
}));

with

dm.add(ChatSDK.core().pushUser()
.observeOn(RX.main())
.subscribe(() -> {
CoreUtils.doRestart(this);
}));

This is going to restart the app when registration is finished which loads the threads

I also suggest commenting out doneFab.setEnabled(false);
in

doneFab.setOnClickListener(v -> {
//doneFab.setEnabled(false);
next();
});

it seems to be blocking the done button

@ChatWave I always used existing user to login, so no, it's not the case if it's existing user or a new one (after registration)