feat: 优化联系人列表刷新冲突,联系人列表先缓存后网络
This commit is contained in:
@@ -3,8 +3,10 @@ package com.ttstd.dialer.activity.alarm.list;
|
||||
import android.content.Intent;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
|
||||
import com.kongzue.dialogx.dialogs.MessageDialog;
|
||||
@@ -100,9 +102,9 @@ public class AlarmListActivity extends BaseMvvmActivity<AlarmListViewModel, Acti
|
||||
mViewDataBinding.recyclerView.setAdapter(mAlarmInfoAdapter);
|
||||
mViewDataBinding.recyclerView.addItemDecoration(new ListDividerItemDecoration(this, 16));
|
||||
|
||||
mViewDataBinding.recyclerView.addOnScrollListener(new androidx.recyclerview.widget.RecyclerView.OnScrollListener() {
|
||||
mViewDataBinding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrolled(androidx.recyclerview.widget.RecyclerView recyclerView, int dx, int dy) {
|
||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
super.onScrolled(recyclerView, dx, dy);
|
||||
mViewDataBinding.swipeRefreshLayout.setEnabled(recyclerView.getChildCount() == 0 || recyclerView.getChildAt(0).getTop() >= 0);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.recyclerview.widget.ItemTouchHelper;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
|
||||
import com.kongzue.dialogx.dialogs.TipDialog;
|
||||
@@ -146,6 +147,13 @@ public class ContactListActivity extends BaseMvvmActivity<ContactListViewModel,
|
||||
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
||||
mViewDataBinding.recyclerView.setLayoutManager(linearLayoutManager);
|
||||
mViewDataBinding.recyclerView.setAdapter(mContactInfoAdapter);
|
||||
mViewDataBinding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
super.onScrolled(recyclerView, dx, dy);
|
||||
mViewDataBinding.swipeRefreshLayout.setEnabled(recyclerView.getChildCount() == 0 || recyclerView.getChildAt(0).getTop() >= 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -42,44 +42,7 @@ public class ContactListViewModel extends BaseViewModel<ActivityContactListBindi
|
||||
public MutableLiveData<List<ContactInfo>> mContactListData = new MutableLiveData<>();
|
||||
|
||||
public void getAllContacts() {
|
||||
mContactManager.getContacts(getLifecycle(), new ContactManager.ContactCallback() {
|
||||
@Override
|
||||
public void onSuccess(List<ContactInfo> contacts) {
|
||||
Logger.e(TAG, "获取联系人成功: " + contacts.size());
|
||||
mContactListData.setValue(contacts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable e) {
|
||||
Logger.e(TAG, "获取联系人失败: " + e.getMessage());
|
||||
mContactListData.setValue(null);
|
||||
}
|
||||
});
|
||||
|
||||
// OkHttpManager.getInstance().getContactListObservable(getLifecycle())
|
||||
// .compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
// .subscribe(new BaseObserver<BaseResponse<List<ContactInfo>>>() {
|
||||
// @Override
|
||||
// public void onSuccess(BaseResponse<List<ContactInfo>> listBaseResponse) {
|
||||
// Log.e("getAllContacts", "onSuccess: " + listBaseResponse);
|
||||
// if (listBaseResponse.isSuccess()) {
|
||||
// List<ContactInfo> contactInfos = listBaseResponse.getData();
|
||||
// List<ContactInfo> sorted = contactInfos.stream().sorted(new Comparator<ContactInfo>() {
|
||||
// @Override
|
||||
// public int compare(ContactInfo t0, ContactInfo t1) {
|
||||
// return Integer.compare(t0.getPosition(), t1.getPosition());
|
||||
// }
|
||||
// }).collect(Collectors.toList());
|
||||
// mContactListData.setValue(sorted);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onFailure(Throwable e) {
|
||||
// Log.e("getAllContacts", "onFailure: " + e.getMessage());
|
||||
// mContactListData.setValue(null);
|
||||
// }
|
||||
// });
|
||||
getAllContactsFromDB();
|
||||
}
|
||||
|
||||
private void getAllContactsFromDB() {
|
||||
@@ -102,15 +65,7 @@ public class ContactListViewModel extends BaseViewModel<ActivityContactListBindi
|
||||
@Override
|
||||
public void onNext(@NonNull List<ContactInfo> contactInfos) {
|
||||
Logger.e("getAllContacts", "onNext: ");
|
||||
// mContactListData.setValue(contactInfos);
|
||||
|
||||
// List<Contact> sorted = contacts.stream().sorted(new Comparator<Contact>() {
|
||||
// @Override
|
||||
// public int compare(Contact o1, Contact o2) {
|
||||
// return Integer.compare(o1.getSort(), o2.getSort());
|
||||
// }
|
||||
// }).collect(Collectors.toList());
|
||||
// mContactListData.setValue(sorted);
|
||||
mContactListData.setValue(contactInfos);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -121,10 +76,26 @@ public class ContactListViewModel extends BaseViewModel<ActivityContactListBindi
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Logger.e("getAllContacts", "onComplete: ");
|
||||
requestNetworkContacts();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void requestNetworkContacts() {
|
||||
mContactManager.getContacts(getLifecycle(), new ContactManager.ContactCallback() {
|
||||
@Override
|
||||
public void onSuccess(List<ContactInfo> contacts) {
|
||||
Logger.e(TAG, "获取网络联系人成功: " + contacts.size());
|
||||
mContactListData.setValue(contacts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable e) {
|
||||
Logger.e(TAG, "获取网络联系人失败: " + e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void updateItemPosition(ContactInfo contactInfo) {
|
||||
Logger.e(TAG, "updateItemPosition: " + contactInfo);
|
||||
Observable.create(new ObservableOnSubscribe<Integer>() {
|
||||
|
||||
@@ -37,8 +37,8 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:maxLines="1"
|
||||
android:singleLine="true"
|
||||
android:onClick="@{click::testContact}"
|
||||
android:singleLine="true"
|
||||
android:text="联系人"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="@dimen/sp_22"
|
||||
@@ -72,8 +72,7 @@
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user