Skip to content

Commit

Permalink
> Precomputed text added for smooth UI for large data Calls.
Browse files Browse the repository at this point in the history
> Updated support Libraries
  • Loading branch information
Ashok-Varma committed Oct 27, 2018
1 parent c2f451e commit c7e2c27
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 79 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ Based on your IDE you can import library in one of the following ways
##### Gradle:
Add the dependency in your `build.gradle` file. Add it alongside the `no-op` variant to isolate Gander from release builds as follows:
```gradle
debugCompile 'com.ashokvarma.android:gander:1.2.0'
releaseCompile 'com.ashokvarma.android:gander-no-op:1.2.0'
debugCompile 'com.ashokvarma.android:gander:1.3.0'
releaseCompile 'com.ashokvarma.android:gander-no-op:1.3.0'
```
If you want this in library in both release and compile, then try this :
```gradle
compile 'com.ashokvarma.android:gander:1.2.0'
compile 'com.ashokvarma.android:gander:1.3.0'
```


Expand All @@ -44,7 +44,7 @@ compile 'com.ashokvarma.android:gander:1.2.0'
<dependency>
<groupId>com.ashokvarma.android</groupId>
<artifactId>gander</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<type>pom</type>
</dependency>
```
Expand Down Expand Up @@ -122,7 +122,7 @@ License
2. [SharedPrefManager](https://github.com/Ashok-Varma/SharedPrefManager) : SharedPref Manager is a Dev Debug tool that helps to manage(Edit, Add, Clear) your android Shared Preferences.
3. [BottomNavigation](https://github.com/Ashok-Varma/BottomNavigation) : This Library helps users to use Bottom Navigation Bar (A new pattern from google) with ease and allows ton of customizations.

[mavenAarDownload]: https://repo1.maven.org/maven2/com/ashokvarma/android/gander/1.2.0/gander-1.2.0.aar
[mavenAarDownload]: https://repo1.maven.org/maven2/com/ashokvarma/android/gander/1.3.0/gander-1.3.0.aar
[googlePlayStoreLink]: https://play.google.com/store/apps/details?id=com.ashokvarma.gander.sample
[chuckLink]: https://github.com/jgilfelt/chuck
[jgilfeltLink]: https://github.com/jgilfelt
Expand Down
18 changes: 9 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

Expand All @@ -29,16 +29,16 @@ task clean(type: Delete) {

ext {
minSdkVersion = 16
targetSdkVersion = 27
compileSdkVersion = 27
targetSdkVersion = 28
compileSdkVersion = 28

versionCode = 12
versionName = "1.2.0"
versionCode = 13
versionName = "1.3.0"

supportLibVersion = '27.1.1'
supportLibVersion = '28.0.0'
viewModelVersion = '1.1.1'
pagingVersion = "1.0.0"
roomVersion = '1.1.0'
okhttp3Version = '3.10.0'
pagingVersion = "1.0.1"
roomVersion = '1.1.1'
okhttp3Version = '3.11.0'
retrofitVersion = '2.4.0'
}
2 changes: 1 addition & 1 deletion gander-no-op/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ android {
}

dependencies {
compile "com.squareup.okhttp3:okhttp:$okhttp3Version"
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"
}

// Place it at the end of the file
Expand Down
14 changes: 6 additions & 8 deletions gander/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ android {
}

dependencies {
compile "com.squareup.okhttp3:okhttp:$okhttp3Version"
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"

compile "com.android.support:design:$supportLibVersion"
compile "com.android.support:appcompat-v7:$supportLibVersion"
implementation "com.android.support:design:$supportLibVersion"
implementation "com.android.support:appcompat-v7:$supportLibVersion"

compile "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.persistence.room:runtime:$roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomVersion"
compile "android.arch.paging:runtime:$pagingVersion"
compile "android.arch.lifecycle:extensions:$viewModelVersion"
compile "android.arch.lifecycle:viewmodel:$viewModelVersion"

implementation "android.arch.paging:runtime:$pagingVersion"
implementation "android.arch.lifecycle:extensions:$viewModelVersion"
}

// Place it at the end of the file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.ashokvarma.gander.internal.support;


import android.text.PrecomputedText;
import android.widget.TextView;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.concurrent.Executor;
Expand All @@ -20,23 +23,30 @@ public class TextUtil {
* because the callable which is formatting Json, Xml will now be done in background thread
*/
public static void asyncSetText(Executor bgExecutor, final AsyncTextProvider asyncTextProvider) {
// construct precompute related parameters using the TextView that we will set the text on.
// final PrecomputedText.Params params = textView.getTextMetricsParams();
final PrecomputedText.Params params = asyncTextProvider.getTextView().getTextMetricsParams();
final Reference<AsyncTextProvider> asyncTextProviderReference = new WeakReference<>(asyncTextProvider);

bgExecutor.execute(new Runnable() {
@Override
public void run() {
AsyncTextProvider asyncTextProvider = asyncTextProviderReference.get();
if (asyncTextProvider == null) return;
try {
AsyncTextProvider asyncTextProvider = asyncTextProviderReference.get();
if (asyncTextProvider == null) return;
// get text from background
CharSequence longString = asyncTextProvider.getText();
asyncTextProvider = null;//clear ref
// final PrecomputedText precomputedText = PrecomputedText.create(longString, params);
// pre-compute Text before setting on text view. so UI thread can be free from calculating text paint
final PrecomputedText precomputedText = PrecomputedText.create(longString, params);

asyncTextProvider = asyncTextProviderReference.get();
if (asyncTextProvider == null) return;
// asyncTextProvider.setText(precomputedText);
asyncTextProvider.setText(longString);
asyncTextProvider.getTextView().post(new Runnable() {
@Override
public void run() {
AsyncTextProvider asyncTextProviderInternal = asyncTextProviderReference.get();
if (asyncTextProviderInternal == null) return;
// set pre computed text
TextView textView = asyncTextProviderInternal.getTextView();
textView.setText(precomputedText, TextView.BufferType.SPANNABLE);
}
});
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -47,7 +57,7 @@ public void run() {
public interface AsyncTextProvider {
CharSequence getText();

void setText(final CharSequence charSequence);
TextView getTextView();
}

public static boolean isNullOrWhiteSpace(CharSequence text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public TransactionDetailViewModel(Application application) {
mTransactionDao = GanderDatabase.getInstance(application).httpTransactionDao();
}

public LiveData<HttpTransaction> getTransactionWithId(long id) {
LiveData<HttpTransaction> getTransactionWithId(long id) {
return mTransactionDao.getTransactionsWithId(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static void start(Context context, long transactionId, HttpTransaction.St
AppBarLayout mAppBarLayout;

private HttpTransaction mTransaction;
private String mSearchKey;
TransactionDetailViewModel mViewModel;
GanderColorUtil mColorUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ public class TransactionPayloadFragment extends Fragment implements TransactionF
private TextView mHeadersView;
private TextView mBodyView;
private NestedScrollView mScrollParentView;
private View mLinearParentView;
private FloatingActionButton mSearchFab;
private View mSearchBar;
private View mSearchBarPrev;
private View mSearchBarNext;
private View mSearchBarClose;
private EditText mSearchView;
private TextView mSearchCountView;

Expand Down Expand Up @@ -88,6 +84,7 @@ public static TransactionPayloadFragment newInstance(int type) {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
assert getArguments() != null;
mType = getArguments().getInt(ARG_TYPE);
mColorUtil = GanderColorUtil.getInstance(getContext());
setRetainInstance(true);
Expand All @@ -100,19 +97,18 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
mHeadersView = view.findViewById(R.id.gander_details_headers);
mBodyView = view.findViewById(R.id.gander_details_body);
mScrollParentView = view.findViewById(R.id.gander_details_scroll_parent);
mLinearParentView = view.findViewById(R.id.gander_details_linear_parent);
mSearchFab = view.findViewById(R.id.gander_details_search_fab);
mSearchBar = view.findViewById(R.id.gander_details_search_bar);
mSearchBarPrev = view.findViewById(R.id.gander_details_search_prev);
mSearchBarNext = view.findViewById(R.id.gander_details_search_next);
mSearchBarClose = view.findViewById(R.id.gander_details_search_close);
View searchBarPrev = view.findViewById(R.id.gander_details_search_prev);
View searchBarNext = view.findViewById(R.id.gander_details_search_next);
View searchBarClose = view.findViewById(R.id.gander_details_search_close);
mSearchView = view.findViewById(R.id.gander_details_search);
mSearchCountView = view.findViewById(R.id.gander_details_search_count);

mSearchFab.setOnClickListener(this);
mSearchBarPrev.setOnClickListener(this);
mSearchBarNext.setOnClickListener(this);
mSearchBarClose.setOnClickListener(this);
searchBarPrev.setOnClickListener(this);
searchBarNext.setOnClickListener(this);
searchBarClose.setOnClickListener(this);

mSearchView.addTextChangedListener(new TextWatcher() {
@Override
Expand Down Expand Up @@ -200,14 +196,8 @@ public CharSequence getText() {
}

@Override
public void setText(final CharSequence charSequence) {
mBodyView.post(new Runnable() {
@Override
public void run() {
mBodyView.setText(charSequence, TextView.BufferType.SPANNABLE);
// updateSearchCount(1);
}
});
public TextView getTextView() {
return mBodyView;
}
});
}
Expand Down Expand Up @@ -312,13 +302,13 @@ public void setUserVisibleHint(boolean isVisibleToUser) {
public void onClick(View v) {
int id = v.getId();
if (id == R.id.gander_details_search_fab) {
mSearchFab.setVisibility(View.GONE);
mSearchFab.hide();
mSearchBar.setVisibility(View.VISIBLE);
mScrollParentView.setPadding(0, getResources().getDimensionPixelSize(R.dimen.gander_search_bar_height), 0, mScrollParentView.getBottom());
showKeyboard();
} else if (id == R.id.gander_details_search_close) {
if (TextUtil.isNullOrWhiteSpace(mSearchKey)) {
mSearchFab.setVisibility(View.VISIBLE);
mSearchFab.show();
mSearchBar.setVisibility(View.GONE);
mScrollParentView.setPadding(0, 0, 0, mScrollParentView.getBottom());
hideKeyboard();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ashokvarma.gander.internal.ui.list;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;

import com.ashokvarma.gander.internal.data.HttpTransaction;
Expand All @@ -15,24 +17,19 @@ public class ListDiffUtil extends DiffUtil.ItemCallback<HttpTransaction> {

private String mSearchKey;

public void setSearchKey(String searchKey) {
void setSearchKey(String searchKey) {
this.mSearchKey = searchKey;
}

@Override
public boolean areItemsTheSame(HttpTransaction oldItem, HttpTransaction newItem) {
// even if both are null => items are not same (animation issues)
if (newItem != null) {
// might not work always due to async nature of Adapter.
// fails in very rare race conditions
// but increases pref.
newItem.searchKey = mSearchKey;
}
return oldItem != null && newItem != null && oldItem.getId() == newItem.getId();
public boolean areItemsTheSame(@NonNull HttpTransaction oldItem, @NonNull HttpTransaction newItem) {
// might not work always due to async nature of Adapter fails in very rare race conditions but increases pref.
newItem.searchKey = mSearchKey;
return oldItem.getId() == newItem.getId();
}

@Override
public boolean areContentsTheSame(HttpTransaction oldItem, HttpTransaction newItem) {
public boolean areContentsTheSame(@NonNull HttpTransaction oldItem, @NonNull HttpTransaction newItem) {
// both will non null. because of areItemsTheSame logic only non nulls come here
// comparing only items shown in the list
return areEqual(oldItem.getMethod(), newItem.getMethod()) &&
Expand All @@ -47,11 +44,14 @@ public boolean areContentsTheSame(HttpTransaction oldItem, HttpTransaction newIt
areEqual(oldItem.searchKey, newItem.searchKey);
}

private static boolean areEqual(Object oldItem, Object newItem) {
private static boolean areEqual(@Nullable Object oldItem, @Nullable Object newItem) {
if (oldItem == null && newItem == null) {
return true;// both are null
// both are null
return true;
} else if (oldItem == null || newItem == null) {
// only one is null => return false
return false;
}
// only one is null => return false
return oldItem != null && newItem != null && oldItem.equals(newItem);
return oldItem.equals(newItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public void onItemRangeInserted(int positionStart, int itemCount) {
});
}

public TransactionAdapter setListener(Listener listener) {
TransactionAdapter setListener(Listener listener) {
this.mListener = listener;
return this;
}

public TransactionAdapter setSearchKey(String searchKey) {
TransactionAdapter setSearchKey(String searchKey) {
this.mSearchKey = searchKey;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TransactionListViewModel(Application application) {
mTransactions = new LivePagedListBuilder<>(factory, config).build();
}

public LiveData<PagedList<HttpTransaction>> getTransactions(String key) {
LiveData<PagedList<HttpTransaction>> getTransactions(String key) {
if (key == null || key.trim().length() == 0) {
return mTransactions;
} else {
Expand All @@ -51,7 +51,7 @@ public void deleteItem(HttpTransaction transaction) {
new deleteAsyncTask(mTransactionDao).execute(transaction);
}

public void clearAll() {
void clearAll() {
new clearAsyncTask(mTransactionDao).execute();
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Jun 02 20:21:46 IST 2018
#Sat Oct 27 10:22:47 IST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,8 @@ public void onFailure(Call call, Throwable t) {
api.deny().enqueue(cb);
api.cache("Mon").enqueue(cb);
api.cache(30).enqueue(cb);

// large data 60MB Request and 60MB response (PrecomputedText magic test)
api.post(new SampleApiService.VeryLargeData()).enqueue(cb);
}
}
Loading

0 comments on commit c7e2c27

Please sign in to comment.