Skip to content

Commit

Permalink
Update version to 1.0.7, Support rtl
Browse files Browse the repository at this point in the history
  • Loading branch information
hearsilent committed Aug 22, 2018
1 parent fa3b6c0 commit 3b94eee
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 62 deletions.
13 changes: 7 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ android {
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "hearsilent.amazingavatar"
minSdkVersion 16
minSdkVersion 17
targetSdkVersion 27
versionCode 106
versionName "1.0.6"
versionCode 107
versionName "1.0.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -27,9 +27,10 @@ dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
testImplementation 'junit:junit:4.12'
}
35 changes: 25 additions & 10 deletions app/src/main/java/hearsilent/amazingavatar/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -12,14 +11,15 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Space;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.bumptech.glide.Glide;

import java.util.Locale;

Expand All @@ -37,6 +37,8 @@ public class MainActivity extends AppCompatActivity {
private final static float EXPAND_AVATAR_SIZE_DP = 80f;
private final static float COLLAPSED_AVATAR_SIZE_DP = 32f;

private View mContainerView;

private AppBarLayout mAppBarLayout;
private CircleImageView mAvatarImageView;
private TextView mToolbarTextView, mTitleTextView;
Expand All @@ -61,6 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void findViews() {
mContainerView = findViewById(R.id.view_container);
mAppBarLayout = findViewById(R.id.app_bar);
mAvatarImageView = findViewById(R.id.imageView_avatar);
mToolbarTextView = findViewById(R.id.toolbar_title);
Expand Down Expand Up @@ -136,7 +139,12 @@ private void translationView(float offset) {
float newTextWidth = Utils.getTextWidth(paint, mTitleTextView.getText().toString());
paint.setTextSize(mTitleTextSize);
float originTextWidth = Utils.getTextWidth(paint, mTitleTextView.getText().toString());
float xTitleOffset = (mToolbarTextPoint[0] - mTitleTextViewPoint[0] -
// If rtl should move title view to end of view.
boolean isRTL = TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) ==
View.LAYOUT_DIRECTION_RTL ||
mContainerView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
float xTitleOffset = ((mToolbarTextPoint[0] + (isRTL ? mToolbarTextView.getWidth() : 0)) -
(mTitleTextViewPoint[0] + (isRTL ? mTitleTextView.getWidth() : 0)) -
(mToolbarTextView.getWidth() > newTextWidth ?
(originTextWidth - newTextWidth) / 2f : 0)) * offset;
float yTitleOffset = (mToolbarTextPoint[1] - mTitleTextViewPoint[1]) * offset;
Expand All @@ -152,20 +160,27 @@ private void fetchAvatar() {
NetworkHelper.getAvatar(new AvatarCallback() {

@Override
public void onSuccess(AvatarModel avatarModel) {
public void onSuccess(final AvatarModel avatarModel) {
super.onSuccess(avatarModel);
if (isFinishing()) {
return;
}
ImageLoader.getInstance().displayImage(avatarModel.url, mAvatarImageView);
String name = String.format(Locale.getDefault(), "%s %s", avatarModel.firstName,
avatarModel.lastName);
mTitleTextView.setText(name);
new Handler(Looper.getMainLooper()).post(new Runnable() {
runOnUiThread(new Runnable() {

@Override
public void run() {
resetPoints(true);
Glide.with(MainActivity.this).load(avatarModel.url).into(mAvatarImageView);
String name =
String.format(Locale.getDefault(), "%s %s", avatarModel.firstName,
avatarModel.lastName);
mTitleTextView.setText(name);
mTitleTextView.post(new Runnable() {

@Override
public void run() {
resetPoints(true);
}
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package hearsilent.amazingavatar.base;

import android.app.Application;
import android.content.Context;
import android.os.StrictMode;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import hearsilent.amazingavatar.BuildConfig;

public class BaseApplication extends Application {
Expand All @@ -18,15 +14,6 @@ public void onCreate() {
if (BuildConfig.DEBUG) {
enableStrictMode();
}

initImageLoader(this);
}

private static void initImageLoader(Context context) {
ImageLoaderConfiguration config =
new ImageLoaderConfiguration.Builder(context).threadPoolSize(5).build();

ImageLoader.getInstance().init(config);
}

private void enableStrictMode() {
Expand Down
58 changes: 34 additions & 24 deletions app/src/main/java/hearsilent/amazingavatar/libs/NetworkHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,71 @@

import android.support.annotation.NonNull;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import hearsilent.amazingavatar.callbacks.AvatarCallback;
import hearsilent.amazingavatar.models.AvatarModel;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class NetworkHelper {

private static AsyncHttpClient mClient = null;

private static AsyncHttpClient init() {
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Connection", "Keep-Alive");
client.setEnableRedirects(true, true, true);
private static OkHttpClient mClient;

client.setTimeout(8 * 1000);
return client;
private static void init() {
mClient = new OkHttpClient().newBuilder().followRedirects(false).followSslRedirects(false)
.connectTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS).build();
}

private static AsyncHttpClient getClient() {
private static OkHttpClient getClient() {
if (mClient == null) {
mClient = init();
init();
}
return mClient;
}

private static final String AVATAR_URL = "https://tinyfac.es/api/users";

public static void getAvatar(@NonNull final AvatarCallback callback) {
AsyncHttpClient client = getClient();
OkHttpClient client = getClient();

client.get(AVATAR_URL, new JsonHttpResponseHandler() {
Request request = new Request.Builder().url(AVATAR_URL).get().build();

client.newCall(request).enqueue(new Callback() {

@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
if (statusCode != 200) {
callback.onFail();
} else {
public void onFailure(@NonNull Call call, @NonNull IOException e) {
callback.onFail();
}

@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
if (response.code() == 200) {
try {
JSONObject jsonObject = response.getJSONObject(0);
ResponseBody responseBodyCopy = response.peekBody(Long.MAX_VALUE);
String body = responseBodyCopy.string();
JSONArray jsonArray = new JSONArray(body);
JSONObject jsonObject = jsonArray.getJSONObject(0);
AvatarModel model = new AvatarModel();
model.url = jsonObject.getJSONArray("avatars").getJSONObject(0)
.getString("url");
model.firstName = jsonObject.getString("first_name");
model.lastName = jsonObject.getString("last_name");
callback.onSuccess(model);
} catch (JSONException ignore) {
} catch (Exception ignore) {
callback.onFail();
}
} else {
callback.onFail();
}
}
});
Expand Down
19 changes: 11 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/view_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
Expand All @@ -26,13 +27,13 @@
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_height="?android:attr/actionBarSize"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_collapseMode="parallax"
Expand All @@ -42,15 +43,16 @@
android:id="@+id/space"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="56dp"
android:layout_marginRight="8dp"/>
android:layout_marginEnd="8dp"
android:layout_marginStart="56dp"/>

<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center_vertical"/>
android:layout_height="match_parent"
android:gravity="center_vertical|start"
android:textAlignment="viewStart"/>
</LinearLayout>

<android.support.constraint.ConstraintLayout
Expand All @@ -77,11 +79,12 @@
<TextView
android:id="@+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_height="?android:attr/actionBarSize"
android:ellipsize="end"
android:gravity="center_vertical"
android:gravity="center_vertical|start"
android:maxLines="1"
android:text="HearSilent"
android:textAlignment="viewStart"
android:textColor="#FFF"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/textView_title_shadow"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/item_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textAlignment="viewStart"
android:textColor="#FFF"
android:textSize="16sp"
tools:background="#303030"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:3.1.4'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 3b94eee

Please sign in to comment.