mirror of
https://github.com/android10/Android-CleanArchitecture.git
synced 2026-03-13 10:13:41 +08:00
Fix orientation changes making execute uses cases. Minor refactor and leaks solved.
This commit is contained in:
@@ -44,7 +44,8 @@ public class UserListPresenter implements Presenter {
|
||||
private final UserModelDataMapper userModelDataMapper;
|
||||
|
||||
@Inject
|
||||
public UserListPresenter(@Named("userList") UseCase getUserListUserCase, UserModelDataMapper userModelDataMapper) {
|
||||
public UserListPresenter(@Named("userList") UseCase getUserListUserCase,
|
||||
UserModelDataMapper userModelDataMapper) {
|
||||
this.getUserListUseCase = getUserListUserCase;
|
||||
this.userModelDataMapper = userModelDataMapper;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* Copyright (C) 2014 android10.org. All rights reserved.
|
||||
*
|
||||
* @author Fernando Cejas (the android10 coder)
|
||||
*/
|
||||
package com.fernandocejas.android10.sample.presentation.view.activity;
|
||||
@@ -23,20 +24,19 @@ public class UserDetailsActivity extends BaseActivity implements HasComponent<Us
|
||||
private static final String INTENT_EXTRA_PARAM_USER_ID = "org.android10.INTENT_PARAM_USER_ID";
|
||||
private static final String INSTANCE_STATE_PARAM_USER_ID = "org.android10.STATE_PARAM_USER_ID";
|
||||
|
||||
private int userId;
|
||||
private UserComponent userComponent;
|
||||
|
||||
public static Intent getCallingIntent(Context context, int userId) {
|
||||
Intent callingIntent = new Intent(context, UserDetailsActivity.class);
|
||||
callingIntent.putExtra(INTENT_EXTRA_PARAM_USER_ID, userId);
|
||||
|
||||
return callingIntent;
|
||||
}
|
||||
|
||||
private int userId;
|
||||
private UserComponent userComponent;
|
||||
|
||||
@Override protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
||||
setContentView(R.layout.activity_user_details);
|
||||
setContentView(R.layout.activity_layout);
|
||||
|
||||
this.initializeActivity(savedInstanceState);
|
||||
this.initializeInjector();
|
||||
@@ -55,7 +55,7 @@ public class UserDetailsActivity extends BaseActivity implements HasComponent<Us
|
||||
private void initializeActivity(Bundle savedInstanceState) {
|
||||
if (savedInstanceState == null) {
|
||||
this.userId = getIntent().getIntExtra(INTENT_EXTRA_PARAM_USER_ID, -1);
|
||||
addFragment(R.id.fl_fragment, UserDetailsFragment.create(this.userId));
|
||||
addFragment(R.id.fragmentContainer, new UserDetailsFragment());
|
||||
} else {
|
||||
this.userId = savedInstanceState.getInt(INSTANCE_STATE_PARAM_USER_ID);
|
||||
}
|
||||
|
||||
@@ -31,10 +31,9 @@ public class UserListActivity extends BaseActivity implements HasComponent<UserC
|
||||
@Override protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
||||
setContentView(R.layout.activity_user_list);
|
||||
setContentView(R.layout.activity_layout);
|
||||
|
||||
this.initializeInjector();
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
addFragment(R.id.fragmentContainer, new UserListFragment());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* Copyright (C) 2014 android10.org. All rights reserved.
|
||||
*
|
||||
* @author Fernando Cejas (the android10 coder)
|
||||
*/
|
||||
package com.fernandocejas.android10.sample.presentation.view.component;
|
||||
@@ -10,6 +11,8 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
@@ -29,7 +32,8 @@ public class AutoLoadImageView extends ImageView {
|
||||
|
||||
private static final String BASE_IMAGE_NAME_CACHED = "image_";
|
||||
|
||||
private int imagePlaceHolderResourceId = -1;
|
||||
private String imageUrl = null;
|
||||
private int imagePlaceHolderResId = -1;
|
||||
private DiskCache cache = new DiskCache(getContext().getCacheDir());
|
||||
|
||||
public AutoLoadImageView(Context context) {
|
||||
@@ -44,15 +48,36 @@ public class AutoLoadImageView extends ImageView {
|
||||
super(context, attrs, defStyle);
|
||||
}
|
||||
|
||||
@Override protected Parcelable onSaveInstanceState() {
|
||||
Parcelable superState = super.onSaveInstanceState();
|
||||
SavedState savedState = new SavedState(superState);
|
||||
savedState.imagePlaceHolderResId = this.imagePlaceHolderResId;
|
||||
savedState.imageUrl = this.imageUrl;
|
||||
return savedState;
|
||||
}
|
||||
|
||||
@Override protected void onRestoreInstanceState(Parcelable state) {
|
||||
if(!(state instanceof SavedState)) {
|
||||
super.onRestoreInstanceState(state);
|
||||
return;
|
||||
}
|
||||
SavedState savedState = (SavedState)state;
|
||||
super.onRestoreInstanceState(savedState.getSuperState());
|
||||
this.imagePlaceHolderResId = savedState.imagePlaceHolderResId;
|
||||
this.imageUrl = savedState.imageUrl;
|
||||
this.setImageUrl(this.imageUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an image from a remote url.
|
||||
*
|
||||
* @param imageUrl The url of the resource to load.
|
||||
*/
|
||||
public void setImageUrl(final String imageUrl) {
|
||||
this.imageUrl = imageUrl;
|
||||
AutoLoadImageView.this.loadImagePlaceHolder();
|
||||
if (imageUrl != null) {
|
||||
this.loadImageFromUrl(imageUrl);
|
||||
if (this.imageUrl != null) {
|
||||
this.loadImageFromUrl(this.imageUrl);
|
||||
} else {
|
||||
this.loadImagePlaceHolder();
|
||||
}
|
||||
@@ -64,7 +89,7 @@ public class AutoLoadImageView extends ImageView {
|
||||
* @param resourceId The resource id to use as a place holder.
|
||||
*/
|
||||
public void setImagePlaceHolder(int resourceId) {
|
||||
this.imagePlaceHolderResourceId = resourceId;
|
||||
this.imagePlaceHolderResId = resourceId;
|
||||
this.loadImagePlaceHolder();
|
||||
}
|
||||
|
||||
@@ -126,11 +151,11 @@ public class AutoLoadImageView extends ImageView {
|
||||
* Loads the image place holder if any has been assigned.
|
||||
*/
|
||||
private void loadImagePlaceHolder() {
|
||||
if (this.imagePlaceHolderResourceId != -1) {
|
||||
if (this.imagePlaceHolderResId != -1) {
|
||||
((Activity) getContext()).runOnUiThread(new Runnable() {
|
||||
@Override public void run() {
|
||||
AutoLoadImageView.this.setImageResource(
|
||||
AutoLoadImageView.this.imagePlaceHolderResourceId);
|
||||
AutoLoadImageView.this.imagePlaceHolderResId);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -310,4 +335,37 @@ public class AutoLoadImageView extends ImageView {
|
||||
return new File(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SavedState extends BaseSavedState {
|
||||
int imagePlaceHolderResId;
|
||||
String imageUrl;
|
||||
|
||||
SavedState(Parcelable superState) {
|
||||
super(superState);
|
||||
}
|
||||
|
||||
private SavedState(Parcel in) {
|
||||
super(in);
|
||||
this.imagePlaceHolderResId = in.readInt();
|
||||
this.imageUrl = in.readString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel out, int flags) {
|
||||
super.writeToParcel(out, flags);
|
||||
out.writeInt(this.imagePlaceHolderResId);
|
||||
out.writeString(this.imageUrl);
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<SavedState> CREATOR =
|
||||
new Parcelable.Creator<SavedState>() {
|
||||
public SavedState createFromParcel(Parcel in) {
|
||||
return new SavedState(in);
|
||||
}
|
||||
|
||||
public SavedState[] newArray(int size) {
|
||||
return new SavedState[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,6 @@ import javax.inject.Inject;
|
||||
*/
|
||||
public class UserDetailsFragment extends BaseFragment implements UserDetailsView {
|
||||
|
||||
private static final String ARGUMENT_KEY_USER_ID = "org.android10.ARGUMENT_USER_ID";
|
||||
|
||||
@Inject UserDetailsPresenter userDetailsPresenter;
|
||||
|
||||
@Bind(R.id.iv_cover) AutoLoadImageView iv_cover;
|
||||
@@ -45,14 +43,6 @@ public class UserDetailsFragment extends BaseFragment implements UserDetailsView
|
||||
setRetainInstance(true);
|
||||
}
|
||||
|
||||
public static UserDetailsFragment create(int userId) {
|
||||
UserDetailsFragment userDetailsFragment = new UserDetailsFragment();
|
||||
Bundle argumentsBundle = new Bundle();
|
||||
argumentsBundle.putInt(ARGUMENT_KEY_USER_ID, userId);
|
||||
userDetailsFragment.setArguments(argumentsBundle);
|
||||
return userDetailsFragment;
|
||||
}
|
||||
|
||||
@Override public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
this.getComponent(UserComponent.class).inject(this);
|
||||
@@ -68,7 +58,9 @@ public class UserDetailsFragment extends BaseFragment implements UserDetailsView
|
||||
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
this.userDetailsPresenter.setView(this);
|
||||
this.userDetailsPresenter.initialize();
|
||||
if (savedInstanceState == null) {
|
||||
this.loadUserDetails();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onResume() {
|
||||
|
||||
@@ -76,7 +76,9 @@ public class UserListFragment extends BaseFragment implements UserListView {
|
||||
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
this.userListPresenter.setView(this);
|
||||
this.loadUserList();
|
||||
if (savedInstanceState == null) {
|
||||
this.loadUserList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onResume() {
|
||||
@@ -100,6 +102,11 @@ public class UserListFragment extends BaseFragment implements UserListView {
|
||||
this.userListPresenter.destroy();
|
||||
}
|
||||
|
||||
@Override public void onDetach() {
|
||||
super.onDetach();
|
||||
this.userListListener = null;
|
||||
}
|
||||
|
||||
@Override public void showLoading() {
|
||||
this.rl_progress.setVisibility(View.VISIBLE);
|
||||
this.getActivity().setProgressBarIndeterminateVisibility(true);
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
>
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/fragmentContainer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -1,12 +0,0 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/fl_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
>
|
||||
|
||||
<com.fernandocejas.android10.sample.presentation.view.component.AutoLoadImageView
|
||||
@@ -15,15 +15,16 @@
|
||||
<TextView
|
||||
android:id="@+id/tv_fullname"
|
||||
style="@style/AppTheme.TextViewHeader"
|
||||
android:freezesText="true"
|
||||
/>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="@dimen/activity_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/activity_horizontal_margin">
|
||||
|
||||
android:layout_marginRight="@dimen/activity_horizontal_margin"
|
||||
android:orientation="vertical"
|
||||
>
|
||||
<TextView
|
||||
style="@style/AppTheme.TextViewTitle"
|
||||
android:text="@string/view_text_email"
|
||||
@@ -32,8 +33,8 @@
|
||||
android:id="@+id/tv_email"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:freezesText="true"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
style="@style/AppTheme.TextViewTitle"
|
||||
android:text="@string/view_text_followers"
|
||||
@@ -42,6 +43,7 @@
|
||||
android:id="@+id/tv_followers"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:freezesText="true"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
@@ -52,7 +54,8 @@
|
||||
android:id="@+id/tv_description"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:freezesText="true"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user