mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-26 09:44:43 +01:00
Submitting posts to user account is available.
This commit is contained in:
parent
d8669c94e8
commit
2b9b8fddfa
@ -18,4 +18,5 @@ interface AppComponent {
|
|||||||
void inject(CommentActivity commentActivity);
|
void inject(CommentActivity commentActivity);
|
||||||
void inject(SubscribedThingListingActivity subscribedThingListingActivity);
|
void inject(SubscribedThingListingActivity subscribedThingListingActivity);
|
||||||
void inject(PostTextActivity postTextActivity);
|
void inject(PostTextActivity postTextActivity);
|
||||||
|
void inject(SubscribedSubredditsListingFragment subscribedSubredditsListingFragment);
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,12 @@ import retrofit2.Retrofit;
|
|||||||
public class PostTextActivity extends AppCompatActivity {
|
public class PostTextActivity extends AppCompatActivity {
|
||||||
|
|
||||||
static final String EXTRA_SUBREDDIT_NAME = "ESN";
|
static final String EXTRA_SUBREDDIT_NAME = "ESN";
|
||||||
|
static final String EXTRA_SUBREDDIT_ICON = "ESI";
|
||||||
|
|
||||||
|
private static final String SUBREDDIT_NAME_STATE = "SNS";
|
||||||
|
private static final String SUBREDDIT_ICON_STATE = "SIS";
|
||||||
|
private static final String SUBREDDIT_SELECTED_STATE = "SSS";
|
||||||
|
private static final String SUBREDDIT_IS_USER_STATE = "SIUS";
|
||||||
|
|
||||||
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
|
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
|
||||||
|
|
||||||
@ -45,6 +51,11 @@ public class PostTextActivity extends AppCompatActivity {
|
|||||||
@BindView(R.id.post_title_edit_text_post_text_activity) EditText titleEditText;
|
@BindView(R.id.post_title_edit_text_post_text_activity) EditText titleEditText;
|
||||||
@BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText;
|
@BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText;
|
||||||
|
|
||||||
|
private String iconUrl;
|
||||||
|
private String subredditName;
|
||||||
|
private boolean subredditSelected = false;
|
||||||
|
private boolean subredditIsUser;
|
||||||
|
|
||||||
private RequestManager mGlide;
|
private RequestManager mGlide;
|
||||||
private Locale mLocale;
|
private Locale mLocale;
|
||||||
|
|
||||||
@ -72,13 +83,42 @@ public class PostTextActivity extends AppCompatActivity {
|
|||||||
mGlide = Glide.with(this);
|
mGlide = Glide.with(this);
|
||||||
mLocale = getResources().getConfiguration().locale;
|
mLocale = getResources().getConfiguration().locale;
|
||||||
|
|
||||||
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
|
if(savedInstanceState != null) {
|
||||||
subreditNameTextView.setText(getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME));
|
subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE);
|
||||||
|
iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE);
|
||||||
|
subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE);
|
||||||
|
subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE);
|
||||||
|
|
||||||
|
if(subredditName != null) {
|
||||||
|
subreditNameTextView.setText(subredditName);
|
||||||
|
}
|
||||||
|
if(iconUrl != null && !iconUrl.equals("")) {
|
||||||
|
mGlide.load(iconUrl)
|
||||||
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
||||||
|
.error(mGlide.load(R.drawable.subreddit_default_icon)
|
||||||
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
|
||||||
|
.into(iconGifImageView);
|
||||||
} else {
|
} else {
|
||||||
mGlide.load(R.drawable.subreddit_default_icon)
|
mGlide.load(R.drawable.subreddit_default_icon)
|
||||||
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
||||||
.into(iconGifImageView);
|
.into(iconGifImageView);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
|
||||||
|
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME);
|
||||||
|
iconUrl = getIntent().getExtras().getString(EXTRA_SUBREDDIT_ICON);
|
||||||
|
subreditNameTextView.setText(subredditName);
|
||||||
|
} else {
|
||||||
|
mGlide.load(R.drawable.subreddit_default_icon)
|
||||||
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
||||||
|
.into(iconGifImageView);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iconGifImageView.setOnClickListener(view -> {
|
||||||
|
Intent intent = new Intent(this, SubredditSelectionActivity.class);
|
||||||
|
startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE);
|
||||||
|
});
|
||||||
|
|
||||||
subreditNameTextView.setOnClickListener(view -> {
|
subreditNameTextView.setOnClickListener(view -> {
|
||||||
Intent intent = new Intent(this, SubredditSelectionActivity.class);
|
Intent intent = new Intent(this, SubredditSelectionActivity.class);
|
||||||
@ -99,12 +139,24 @@ public class PostTextActivity extends AppCompatActivity {
|
|||||||
finish();
|
finish();
|
||||||
return true;
|
return true;
|
||||||
case R.id.action_send_post_text_activity:
|
case R.id.action_send_post_text_activity:
|
||||||
|
if(!subredditSelected) {
|
||||||
|
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
item.setEnabled(false);
|
item.setEnabled(false);
|
||||||
item.getIcon().setAlpha(130);
|
item.getIcon().setAlpha(130);
|
||||||
Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
|
Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
|
||||||
postingSnackbar.show();
|
postingSnackbar.show();
|
||||||
|
|
||||||
SubmitPost.submitPostText(mOauthRetrofit, sharedPreferences, mLocale, subreditNameTextView.getText().toString(),
|
String subredditName;
|
||||||
|
if(subredditIsUser) {
|
||||||
|
subredditName = "u_" + subreditNameTextView.getText().toString();
|
||||||
|
} else {
|
||||||
|
subredditName = subreditNameTextView.getText().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
SubmitPost.submitPostText(mOauthRetrofit, sharedPreferences, mLocale, subredditName,
|
||||||
titleEditText.getText().toString(), contentEditText.getText().toString(),
|
titleEditText.getText().toString(), contentEditText.getText().toString(),
|
||||||
false, new SubmitPost.SubmitPostListener() {
|
false, new SubmitPost.SubmitPostListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -116,11 +168,15 @@ public class PostTextActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void submitFailed() {
|
public void submitFailed(@Nullable String errorMessage) {
|
||||||
postingSnackbar.dismiss();
|
postingSnackbar.dismiss();
|
||||||
item.setEnabled(true);
|
item.setEnabled(true);
|
||||||
item.getIcon().setAlpha(255);
|
item.getIcon().setAlpha(255);
|
||||||
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT);
|
if(errorMessage == null) {
|
||||||
|
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
|
||||||
|
} else {
|
||||||
|
Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
@ -129,15 +185,27 @@ public class PostTextActivity extends AppCompatActivity {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
outState.putString(SUBREDDIT_NAME_STATE, subredditName);
|
||||||
|
outState.putString(SUBREDDIT_ICON_STATE, iconUrl);
|
||||||
|
outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected);
|
||||||
|
outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) {
|
if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) {
|
||||||
if(resultCode == RESULT_OK) {
|
if(resultCode == RESULT_OK) {
|
||||||
subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor));
|
subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY);
|
||||||
subreditNameTextView.setText(data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY));
|
iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY);
|
||||||
|
subredditSelected = true;
|
||||||
|
subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER_KEY);
|
||||||
|
|
||||||
String iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY);
|
subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor));
|
||||||
|
subreditNameTextView.setText(subredditName);
|
||||||
if(!iconUrl.equals("")) {
|
if(!iconUrl.equals("")) {
|
||||||
mGlide.load(iconUrl)
|
mGlide.load(iconUrl)
|
||||||
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
||||||
|
@ -4,7 +4,9 @@ import android.content.SharedPreferences;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -19,7 +21,7 @@ import retrofit2.Retrofit;
|
|||||||
class SubmitPost {
|
class SubmitPost {
|
||||||
interface SubmitPostListener {
|
interface SubmitPostListener {
|
||||||
void submitSuccessful(Post post);
|
void submitSuccessful(Post post);
|
||||||
void submitFailed();
|
void submitFailed(@Nullable String errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submitPostText(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences,
|
static void submitPostText(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences,
|
||||||
@ -32,7 +34,7 @@ class SubmitPost {
|
|||||||
params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON);
|
params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON);
|
||||||
params.put(RedditUtils.SR_KEY, subredditName);
|
params.put(RedditUtils.SR_KEY, subredditName);
|
||||||
params.put(RedditUtils.TITLE_KEY, title);
|
params.put(RedditUtils.TITLE_KEY, title);
|
||||||
params.put(RedditUtils.KIND_KEY, RedditUtils.KIND_TEXT);
|
params.put(RedditUtils.KIND_KEY, RedditUtils.KIND_SELF);
|
||||||
params.put(RedditUtils.TEXT_KEY, text);
|
params.put(RedditUtils.TEXT_KEY, text);
|
||||||
params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW));
|
params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW));
|
||||||
|
|
||||||
@ -47,18 +49,18 @@ class SubmitPost {
|
|||||||
submitPostListener);
|
submitPostListener);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
submitPostListener.submitFailed();
|
submitPostListener.submitFailed(null);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.i("call_failed", response.message());
|
Log.i("call_failed", response.message());
|
||||||
submitPostListener.submitFailed();
|
submitPostListener.submitFailed(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
Log.i("call_failed", call.request().url().toString());
|
Log.i("call_failed", call.request().url().toString());
|
||||||
submitPostListener.submitFailed();
|
submitPostListener.submitFailed(null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -66,13 +68,29 @@ class SubmitPost {
|
|||||||
private static void getSubmittedPost(String response, Retrofit oauthRetrofit,
|
private static void getSubmittedPost(String response, Retrofit oauthRetrofit,
|
||||||
SharedPreferences authInfoSharedPreferences, Locale locale,
|
SharedPreferences authInfoSharedPreferences, Locale locale,
|
||||||
SubmitPostListener submitPostListener) throws JSONException {
|
SubmitPostListener submitPostListener) throws JSONException {
|
||||||
JSONObject responseObject = new JSONObject(response);
|
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
|
||||||
if(responseObject.getJSONObject(JSONUtils.JSON_KEY).getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
|
if(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
|
||||||
submitPostListener.submitFailed();
|
JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY)
|
||||||
|
.getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1);
|
||||||
|
if(error.length() != 0) {
|
||||||
|
String errorString;
|
||||||
|
if(error.length() >= 2) {
|
||||||
|
errorString = error.getString(1);
|
||||||
|
errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
|
||||||
|
submitPostListener.submitFailed(errorString);
|
||||||
|
} else {
|
||||||
|
errorString = error.getString(0);
|
||||||
|
errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
|
||||||
|
submitPostListener.submitFailed(errorString);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
submitPostListener.submitFailed(null);
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String postId = responseObject.getJSONObject(JSONUtils.JSON_KEY).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY);
|
String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY);
|
||||||
|
|
||||||
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
|
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
|
||||||
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
||||||
@ -90,19 +108,19 @@ class SubmitPost {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onParsePostFail() {
|
public void onParsePostFail() {
|
||||||
submitPostListener.submitFailed();
|
submitPostListener.submitFailed(null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Log.i("call_failed", response.message());
|
Log.i("call_failed", response.message());
|
||||||
submitPostListener.submitFailed();
|
submitPostListener.submitFailed(response.message());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
Log.i("call_failed", call.request().url().toString());
|
Log.i("call_failed", call.request().url().toString());
|
||||||
submitPostListener.submitFailed();
|
submitPostListener.submitFailed(t.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,8 @@ import butterknife.ButterKnife;
|
|||||||
public class SubredditSelectionActivity extends AppCompatActivity {
|
public class SubredditSelectionActivity extends AppCompatActivity {
|
||||||
|
|
||||||
static final String EXTRA_RETURN_SUBREDDIT_NAME_KEY = "ERSNK";
|
static final String EXTRA_RETURN_SUBREDDIT_NAME_KEY = "ERSNK";
|
||||||
static final String EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY = "ERSIUK";
|
static final String EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY = "ERSIURLK";
|
||||||
|
static final String EXTRA_RETURN_SUBREDDIT_IS_USER_KEY = "ERSIUK";
|
||||||
|
|
||||||
private SubscribedSubredditViewModel mSubscribedSubredditViewModel;
|
private SubscribedSubredditViewModel mSubscribedSubredditViewModel;
|
||||||
|
|
||||||
@ -49,10 +50,11 @@ public class SubredditSelectionActivity extends AppCompatActivity {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void getSelectedSubreddit(String name, String iconUrl) {
|
void getSelectedSubreddit(String name, String iconUrl, boolean subredditIsUser) {
|
||||||
Intent returnIntent = new Intent();
|
Intent returnIntent = new Intent();
|
||||||
returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME_KEY, name);
|
returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME_KEY, name);
|
||||||
returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY, iconUrl);
|
returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY, iconUrl);
|
||||||
|
returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_IS_USER_KEY, subredditIsUser);
|
||||||
setResult(Activity.RESULT_OK, returnIntent);
|
setResult(Activity.RESULT_OK, returnIntent);
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
|
|||||||
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -17,6 +18,9 @@ import androidx.recyclerview.widget.RecyclerView;
|
|||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.RequestManager;
|
import com.bumptech.glide.RequestManager;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Named;
|
||||||
|
|
||||||
import SubscribedSubredditDatabase.SubscribedSubredditViewModel;
|
import SubscribedSubredditDatabase.SubscribedSubredditViewModel;
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
@ -41,6 +45,10 @@ public class SubscribedSubredditsListingFragment extends Fragment {
|
|||||||
|
|
||||||
private boolean mInsertSuccess = false;
|
private boolean mInsertSuccess = false;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Named("user_info")
|
||||||
|
SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
public SubscribedSubredditsListingFragment() {
|
public SubscribedSubredditsListingFragment() {
|
||||||
// Required empty public constructor
|
// Required empty public constructor
|
||||||
}
|
}
|
||||||
@ -54,18 +62,23 @@ public class SubscribedSubredditsListingFragment extends Fragment {
|
|||||||
|
|
||||||
mActivity = getActivity();
|
mActivity = getActivity();
|
||||||
|
|
||||||
|
((Infinity) mActivity.getApplication()).getmAppComponent().inject(this);
|
||||||
|
|
||||||
|
String username = sharedPreferences.getString(SharedPreferencesUtils.USER_KEY, "");
|
||||||
|
String userIconUrl = sharedPreferences.getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
|
||||||
|
|
||||||
mGlide = Glide.with(this);
|
mGlide = Glide.with(this);
|
||||||
|
|
||||||
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
|
||||||
|
|
||||||
SubscribedSubredditsRecyclerViewAdapter adapter;
|
SubscribedSubredditsRecyclerViewAdapter adapter;
|
||||||
if(getArguments().getBoolean(EXTRA_IS_SUBREDDIT_SELECTION)) {
|
if(getArguments().getBoolean(EXTRA_IS_SUBREDDIT_SELECTION)) {
|
||||||
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, (name, iconUrl) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl));
|
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity,
|
||||||
|
(name, iconUrl, subredditIsUser) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl, subredditIsUser));
|
||||||
} else {
|
} else {
|
||||||
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity);
|
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mRecyclerView.setAdapter(adapter);
|
mRecyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class);
|
mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class);
|
||||||
@ -79,6 +92,7 @@ public class SubscribedSubredditsListingFragment extends Fragment {
|
|||||||
mRecyclerView.setVisibility(View.VISIBLE);
|
mRecyclerView.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
adapter.addUser(username, userIconUrl);
|
||||||
adapter.setSubscribedSubreddits(subscribedSubredditData);
|
adapter.setSubscribedSubreddits(subscribedSubredditData);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -26,8 +26,11 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
|
|||||||
private RequestManager glide;
|
private RequestManager glide;
|
||||||
private ItemClickListener itemClickListener;
|
private ItemClickListener itemClickListener;
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
private String userIconUrl;
|
||||||
|
|
||||||
interface ItemClickListener {
|
interface ItemClickListener {
|
||||||
void onClick(String name, String iconUrl);
|
void onClick(String name, String iconUrl, boolean subredditIsUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
SubscribedSubredditsRecyclerViewAdapter(Context context) {
|
SubscribedSubredditsRecyclerViewAdapter(Context context) {
|
||||||
@ -49,19 +52,38 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
|
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
|
||||||
viewHolder.itemView.setOnClickListener(view -> {
|
String name;
|
||||||
if(viewHolder.getAdapterPosition() >= 0) {
|
String iconUrl;
|
||||||
|
|
||||||
if(itemClickListener != null) {
|
if(itemClickListener != null) {
|
||||||
itemClickListener.onClick(mSubscribedSubredditData.get(i).getName(), mSubscribedSubredditData.get(i).getIconUrl());
|
if(viewHolder.getAdapterPosition() == 0) {
|
||||||
|
name = username;
|
||||||
|
iconUrl = userIconUrl;
|
||||||
} else {
|
} else {
|
||||||
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
|
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getName();
|
||||||
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName());
|
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getIconUrl();
|
||||||
mContext.startActivity(intent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
viewHolder.itemView.setOnClickListener(view -> {
|
||||||
|
if(viewHolder.getAdapterPosition() == 0) {
|
||||||
|
itemClickListener.onClick(name, iconUrl, true);
|
||||||
|
} else {
|
||||||
|
itemClickListener.onClick(name, iconUrl, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if(!mSubscribedSubredditData.get(i).getIconUrl().equals("")) {
|
} else {
|
||||||
glide.load(mSubscribedSubredditData.get(i).getIconUrl())
|
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName();
|
||||||
|
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getIconUrl();
|
||||||
|
|
||||||
|
viewHolder.itemView.setOnClickListener(view -> {
|
||||||
|
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
|
||||||
|
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, name);
|
||||||
|
mContext.startActivity(intent);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!iconUrl.equals("")) {
|
||||||
|
glide.load(iconUrl)
|
||||||
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
||||||
.error(glide.load(R.drawable.subreddit_default_icon)
|
.error(glide.load(R.drawable.subreddit_default_icon)
|
||||||
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
|
||||||
@ -71,12 +93,16 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
|
|||||||
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
|
||||||
.into(((SubredditViewHolder) viewHolder).iconGifImageView);
|
.into(((SubredditViewHolder) viewHolder).iconGifImageView);
|
||||||
}
|
}
|
||||||
((SubredditViewHolder) viewHolder).subredditNameTextView.setText(mSubscribedSubredditData.get(i).getName());
|
((SubredditViewHolder) viewHolder).subredditNameTextView.setText(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
if(mSubscribedSubredditData != null) {
|
if(mSubscribedSubredditData != null) {
|
||||||
|
if(itemClickListener != null) {
|
||||||
|
return mSubscribedSubredditData.size() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
return mSubscribedSubredditData.size();
|
return mSubscribedSubredditData.size();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -92,6 +118,10 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
|
|||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addUser(String username, String userIconUrl) {
|
||||||
|
this.username = username;
|
||||||
|
this.userIconUrl = userIconUrl;
|
||||||
|
}
|
||||||
|
|
||||||
private class SubredditViewHolder extends RecyclerView.ViewHolder {
|
private class SubredditViewHolder extends RecyclerView.ViewHolder {
|
||||||
private final GifImageView iconGifImageView;
|
private final GifImageView iconGifImageView;
|
||||||
|
@ -80,6 +80,7 @@
|
|||||||
<string name="send_comment_failed">Could not send this comment</string>
|
<string name="send_comment_failed">Could not send this comment</string>
|
||||||
<string name="parse_sent_comment_failed">The comment is sent but unable to get the sent comment</string>
|
<string name="parse_sent_comment_failed">The comment is sent but unable to get the sent comment</string>
|
||||||
|
|
||||||
|
<string name="select_a_subreddit">Please select a subreddit first</string>
|
||||||
<string name="posting">Posting</string>
|
<string name="posting">Posting</string>
|
||||||
<string name="post_failed">Could not post it</string>
|
<string name="post_failed">Could not post it</string>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user