1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 13:26:59 +00:00

Complete almost all Profile actions using service

This commit is contained in:
Ammar Githam 2020-08-30 17:55:09 +09:00
parent 40e810e88c
commit 6ee476b590
14 changed files with 390 additions and 90 deletions

View File

@ -114,7 +114,7 @@ public final class SavedViewer extends BaseLanguageActivity implements SwipeRefr
savedBinding.mainPosts.setVisibility(View.VISIBLE);
});
final PostModel model = result[result.length - 1];
final PostModel model = result.length > 0 ? result[result.length - 1] : null;
if (model != null) {
endCursor = model.getEndCursor();
@ -130,13 +130,12 @@ public final class SavedViewer extends BaseLanguageActivity implements SwipeRefr
}
model.setPageCursor(false, null);
}
} else {
savedBinding.swipeRefreshLayout.setRefreshing(false);
// if (oldSize == 0) {
// Toast.makeText(getApplicationContext(), R.string.empty_list, Toast.LENGTH_SHORT).show();
// finish();
// }
}
savedBinding.swipeRefreshLayout.setRefreshing(false);
// if (oldSize == 0) {
// Toast.makeText(getApplicationContext(), R.string.empty_list, Toast.LENGTH_SHORT).show();
// finish();
// }
}
};

View File

@ -103,7 +103,7 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh
NavHostFragment.findNavController(this).navigate(action);
});
inboxList.setAdapter(inboxAdapter);
listViewModel = new ViewModelProvider(fragmentActivity).get(InboxThreadModelListViewModel.class);
listViewModel = new ViewModelProvider(this).get(InboxThreadModelListViewModel.class);
listViewModel.getList().observe(fragmentActivity, inboxAdapter::submitList);
initData();
return root;
@ -127,6 +127,12 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh
}
}
@Override
public void onDestroy() {
super.onDestroy();
listViewModel.getList().postValue(Collections.emptyList());
}
private void initData() {
lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> {
if (!Utils.isEmpty(endCursor))

View File

@ -59,7 +59,8 @@ import awais.instagrabber.models.ProfileModel;
import awais.instagrabber.models.StoryModel;
import awais.instagrabber.models.enums.DownloadMethod;
import awais.instagrabber.models.enums.ItemGetType;
import awais.instagrabber.repositories.responses.FriendshipRepositoryChangeResponseRootObject;
import awais.instagrabber.repositories.responses.FriendshipRepoChangeRootResponse;
import awais.instagrabber.repositories.responses.FriendshipRepoRestrictRootResponse;
import awais.instagrabber.services.FriendshipService;
import awais.instagrabber.services.ServiceCallback;
import awais.instagrabber.utils.Constants;
@ -222,10 +223,10 @@ public class ProfileFragment extends Fragment {
}
setupPosts();
setupCommonListeners();
fetchProfile();
fetchUsername();
}
private void fetchProfile() {
private void fetchUsername() {
final String uid = Utils.getUserIdFromCookie(cookie);
if (username == null && uid != null) {
final FetchListener<String> fetchListener = username -> {
@ -339,36 +340,18 @@ public class ProfileFragment extends Fragment {
binding.btnRestrict,
ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_orange_background)));
}
if (profileModel.isReallyPrivate()) {
binding.btnBlock.setVisibility(View.VISIBLE);
binding.btnTagged.setVisibility(View.GONE);
if (profileModel.getBlocked()) {
binding.btnBlock.setText(R.string.unblock);
ViewCompat.setBackgroundTintList(
binding.btnBlock,
ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_green_background)));
} else {
binding.btnBlock.setText(R.string.block);
ViewCompat.setBackgroundTintList(
binding.btnBlock,
ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_red_background)));
}
binding.btnBlock.setVisibility(View.VISIBLE);
binding.btnTagged.setVisibility(View.VISIBLE);
if (profileModel.getBlocked()) {
binding.btnBlock.setText(R.string.unblock);
ViewCompat.setBackgroundTintList(
binding.btnBlock,
ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_green_background)));
} else {
binding.btnBlock.setVisibility(View.GONE);
binding.btnSaved.setVisibility(View.VISIBLE);
binding.btnTagged.setVisibility(View.VISIBLE);
if (profileModel.getBlocked()) {
binding.btnSaved.setText(R.string.unblock);
ViewCompat.setBackgroundTintList(
binding.btnSaved,
ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_green_background)));
} else {
binding.btnSaved.setText(R.string.block);
ViewCompat.setBackgroundTintList(
binding.btnSaved,
ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_red_background))
);
}
binding.btnBlock.setText(R.string.block);
ViewCompat.setBackgroundTintList(
binding.btnBlock,
ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_red_background)));
}
}
} else {
@ -511,10 +494,11 @@ public class ProfileFragment extends Fragment {
userIdFromCookie,
profileModel.getId(),
Utils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipRepositoryChangeResponseRootObject>() {
new ServiceCallback<FriendshipRepoChangeRootResponse>() {
@Override
public void onSuccess(final FriendshipRepositoryChangeResponseRootObject result) {
public void onSuccess(final FriendshipRepoChangeRootResponse result) {
Log.d(TAG, "Unfollow success: " + result);
fetchProfileDetails();
}
@Override
@ -527,10 +511,11 @@ public class ProfileFragment extends Fragment {
userIdFromCookie,
profileModel.getId(),
Utils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipRepositoryChangeResponseRootObject>() {
new ServiceCallback<FriendshipRepoChangeRootResponse>() {
@Override
public void onSuccess(final FriendshipRepositoryChangeResponseRootObject result) {
public void onSuccess(final FriendshipRepoChangeRootResponse result) {
Log.d(TAG, "Follow success: " + result);
fetchProfileDetails();
}
@Override
@ -543,12 +528,61 @@ public class ProfileFragment extends Fragment {
binding.btnRestrict.setOnClickListener(v -> {
if (!isLoggedIn) return;
// restrict
// new ProfileAction().execute("restrict");
final String action = profileModel.getRestricted() ? "Unrestrict" : "Restrict";
friendshipService.toggleRestrict(
profileModel.getId(),
!profileModel.getRestricted(),
Utils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipRepoRestrictRootResponse>() {
@Override
public void onSuccess(final FriendshipRepoRestrictRootResponse result) {
Log.d(TAG, action + " success: " + result);
fetchProfileDetails();
}
@Override
public void onFailure(final Throwable t) {
Log.e(TAG, "Error while performing " + action, t);
}
});
});
binding.btnBlock.setOnClickListener(v -> {
if (!isLoggedIn) return;
// new MainHelper.ProfileAction().execute("block");
if (profileModel.getBlocked()) {
friendshipService.unblock(
userIdFromCookie,
profileModel.getId(),
Utils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipRepoChangeRootResponse>() {
@Override
public void onSuccess(final FriendshipRepoChangeRootResponse result) {
Log.d(TAG, "Unblock success: " + result);
fetchProfileDetails();
}
@Override
public void onFailure(final Throwable t) {
Log.e(TAG, "Error unblocking", t);
}
});
return;
}
friendshipService.block(
userIdFromCookie,
profileModel.getId(),
Utils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipRepoChangeRootResponse>() {
@Override
public void onSuccess(final FriendshipRepoChangeRootResponse result) {
Log.d(TAG, "Block success: " + result);
fetchProfileDetails();
}
@Override
public void onFailure(final Throwable t) {
Log.e(TAG, "Error blocking", t);
}
});
});
binding.btnSaved.setOnClickListener(v -> startActivity(new Intent(requireContext(), SavedViewer.class)
.putExtra(Constants.EXTRAS_INDEX, "$" + profileModel.getId())

View File

@ -1,4 +0,0 @@
package awais.instagrabber.fragments.settings.helpers;
public class AutoSummaryDropDownPreference {
}

View File

@ -2,10 +2,12 @@ package awais.instagrabber.repositories;
import java.util.Map;
import awais.instagrabber.repositories.responses.FriendshipRepositoryChangeResponseRootObject;
import awais.instagrabber.repositories.responses.FriendshipRepoChangeRootResponse;
import awais.instagrabber.repositories.responses.FriendshipRepoRestrictRootResponse;
import retrofit2.Call;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Path;
@ -13,7 +15,14 @@ public interface FriendshipRepository {
@FormUrlEncoded
@POST("/api/v1/friendships/{action}/{id}/")
Call<FriendshipRepositoryChangeResponseRootObject> change(@Path("action") String action,
@Path("id") String id,
@FieldMap Map<String, String> form);
Call<FriendshipRepoChangeRootResponse> change(@Header("User-Agent") String userAgent,
@Path("action") String action,
@Path("id") String id,
@FieldMap Map<String, String> form);
@FormUrlEncoded
@POST("/api/v1/restrict_action/{action}/")
Call<FriendshipRepoRestrictRootResponse> toggleRestrict(@Header("User-Agent") String userAgent,
@Path("action") String action,
@FieldMap Map<String, String> form);
}

View File

@ -1,6 +1,8 @@
package awais.instagrabber.repositories.responses;
public class FriendshipRepositoryChangeResponseFriendshipStatus {
import androidx.annotation.NonNull;
public class FriendshipRepoChangeResponseFriendshipStatus {
private boolean following;
private boolean followedBy;
private boolean blocking;
@ -10,14 +12,14 @@ public class FriendshipRepositoryChangeResponseFriendshipStatus {
private boolean outgoingRequest;
private boolean isBestie;
public FriendshipRepositoryChangeResponseFriendshipStatus(final boolean following,
final boolean followedBy,
final boolean blocking,
final boolean muting,
final boolean isPrivate,
final boolean incomingRequest,
final boolean outgoingRequest,
final boolean isBestie) {
public FriendshipRepoChangeResponseFriendshipStatus(final boolean following,
final boolean followedBy,
final boolean blocking,
final boolean muting,
final boolean isPrivate,
final boolean incomingRequest,
final boolean outgoingRequest,
final boolean isBestie) {
this.following = following;
this.followedBy = followedBy;
this.blocking = blocking;
@ -60,6 +62,7 @@ public class FriendshipRepositoryChangeResponseFriendshipStatus {
return isBestie;
}
@NonNull
@Override
public String toString() {
return "FriendshipRepositoryChangeResponseFriendshipStatus{" +

View File

@ -1,15 +1,16 @@
package awais.instagrabber.repositories.responses;
public class FriendshipRepositoryChangeResponseRootObject {
private FriendshipRepositoryChangeResponseFriendshipStatus friendshipStatus;
public class FriendshipRepoChangeRootResponse {
private FriendshipRepoChangeResponseFriendshipStatus friendshipStatus;
private String status;
public FriendshipRepositoryChangeResponseRootObject(final FriendshipRepositoryChangeResponseFriendshipStatus friendshipStatus, final String status) {
public FriendshipRepoChangeRootResponse(final FriendshipRepoChangeResponseFriendshipStatus friendshipStatus,
final String status) {
this.friendshipStatus = friendshipStatus;
this.status = status;
}
public FriendshipRepositoryChangeResponseFriendshipStatus getFriendshipStatus() {
public FriendshipRepoChangeResponseFriendshipStatus getFriendshipStatus() {
return friendshipStatus;
}

View File

@ -0,0 +1,40 @@
package awais.instagrabber.repositories.responses;
import androidx.annotation.NonNull;
public class FriendshipRepoRestrictResponseFriendshipStatus extends FriendshipRepoChangeResponseFriendshipStatus {
private boolean isRestricted;
public FriendshipRepoRestrictResponseFriendshipStatus(final boolean following,
final boolean followedBy,
final boolean blocking,
final boolean muting,
final boolean isPrivate,
final boolean incomingRequest,
final boolean outgoingRequest,
final boolean isBestie,
final boolean isRestricted) {
super(following, followedBy, blocking, muting, isPrivate, incomingRequest, outgoingRequest, isBestie);
this.isRestricted = isRestricted;
}
public boolean isRestricted() {
return isRestricted;
}
@NonNull
@Override
public String toString() {
return "FriendshipRepoRestrictResponseFriendshipStatus{" +
"following=" + isFollowing() +
", followedBy=" + isFollowedBy() +
", blocking=" + isBlocking() +
", muting=" + isMuting() +
", isPrivate=" + isPrivate() +
", incomingRequest=" + isIncomingRequest() +
", outgoingRequest=" + isOutgoingRequest() +
", isBestie=" + isBestie() +
", isRestricted=" + isRestricted() +
'}';
}
}

View File

@ -0,0 +1,65 @@
package awais.instagrabber.repositories.responses;
import androidx.annotation.NonNull;
public class FriendshipRepoRestrictResponseUsersItem {
private long pk;
private String username;
private String fullName;
private boolean isPrivate;
private String profilePicUrl;
private FriendshipRepoRestrictResponseFriendshipStatus friendshipStatus;
private boolean isVerified;
public FriendshipRepoRestrictResponseUsersItem(final long pk, final String username, final String fullName, final boolean isPrivate, final String profilePicUrl, final FriendshipRepoRestrictResponseFriendshipStatus friendshipStatus, final boolean isVerified) {
this.pk = pk;
this.username = username;
this.fullName = fullName;
this.isPrivate = isPrivate;
this.profilePicUrl = profilePicUrl;
this.friendshipStatus = friendshipStatus;
this.isVerified = isVerified;
}
public long getPk() {
return pk;
}
public String getUsername() {
return username;
}
public String getFullName() {
return fullName;
}
public boolean isPrivate() {
return isPrivate;
}
public String getProfilePicUrl() {
return profilePicUrl;
}
public FriendshipRepoRestrictResponseFriendshipStatus getFriendshipStatus() {
return friendshipStatus;
}
public boolean isVerified() {
return isVerified;
}
@NonNull
@Override
public String toString() {
return "FriendshipRepoRestrictResponseUsersItem{" +
"pk=" + pk +
", username='" + username + '\'' +
", fullName='" + fullName + '\'' +
", isPrivate=" + isPrivate +
", profilePicUrl='" + profilePicUrl + '\'' +
", friendshipStatus=" + friendshipStatus +
", isVerified=" + isVerified +
'}';
}
}

View File

@ -0,0 +1,32 @@
package awais.instagrabber.repositories.responses;
import androidx.annotation.NonNull;
import java.util.List;
public class FriendshipRepoRestrictRootResponse {
private List<FriendshipRepoRestrictResponseUsersItem> users;
private String status;
public FriendshipRepoRestrictRootResponse(final List<FriendshipRepoRestrictResponseUsersItem> users, final String status) {
this.users = users;
this.status = status;
}
public List<FriendshipRepoRestrictResponseUsersItem> getUsers() {
return users;
}
public String getStatus() {
return status;
}
@NonNull
@Override
public String toString() {
return "FriendshipRepoRestrictRootResponse{" +
"users=" + users +
", status='" + status + '\'' +
'}';
}
}

View File

@ -0,0 +1,25 @@
package awais.instagrabber.repositories.responses;
public class LoginRequiredResponse {
private String message = "login_required";
private int logoutReason;
private String status = "fail";
public LoginRequiredResponse(final String message, final int logoutReason, final String status) {
this.message = message;
this.logoutReason = logoutReason;
this.status = status;
}
public String getMessage() {
return message;
}
public int getLogoutReason() {
return logoutReason;
}
public String getStatus() {
return status;
}
}

View File

@ -1,5 +1,10 @@
package awais.instagrabber.services;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import awais.instagrabber.BuildConfig;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
@ -11,15 +16,20 @@ public abstract class BaseService {
Retrofit.Builder getRetrofitBuilder() {
if (builder == null) {
final OkHttpClient client = new OkHttpClient.Builder()
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.addInterceptor(new AddCookiesInterceptor())
.followRedirects(false)
.followSslRedirects(false)
.build();
.followRedirects(true)
.followSslRedirects(true);
if (BuildConfig.DEBUG) {
// clientBuilder.addInterceptor(new LoggingInterceptor());
}
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
builder = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client);
.addConverterFactory(GsonConverterFactory.create(gson))
.client(clientBuilder.build());
}
return builder;
}

View File

@ -7,7 +7,9 @@ import java.util.Map;
import java.util.UUID;
import awais.instagrabber.repositories.FriendshipRepository;
import awais.instagrabber.repositories.responses.FriendshipRepositoryChangeResponseRootObject;
import awais.instagrabber.repositories.responses.FriendshipRepoChangeRootResponse;
import awais.instagrabber.repositories.responses.FriendshipRepoRestrictRootResponse;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
@ -38,40 +40,89 @@ public class FriendshipService extends BaseService {
public void follow(final String userId,
final String targetUserId,
final String crsfToken,
final ServiceCallback<FriendshipRepositoryChangeResponseRootObject> callback) {
final ServiceCallback<FriendshipRepoChangeRootResponse> callback) {
change("create", userId, targetUserId, crsfToken, callback);
}
public void unfollow(final String userId,
final String targetUserId,
final String crsfToken,
final ServiceCallback<FriendshipRepositoryChangeResponseRootObject> callback) {
final ServiceCallback<FriendshipRepoChangeRootResponse> callback) {
change("destroy", userId, targetUserId, crsfToken, callback);
}
private void change(final String action,
final String userId,
public void block(final String userId,
final String targetUserId,
final String crsfToken,
final ServiceCallback<FriendshipRepoChangeRootResponse> callback) {
change("block", userId, targetUserId, crsfToken, callback);
}
public void unblock(final String userId,
final String targetUserId,
final String crsfToken,
final ServiceCallback<FriendshipRepositoryChangeResponseRootObject> callback) {
final Map<String, Object> form = new HashMap<>(5);
form.put("_csrftoken", crsfToken);
form.put("_uid", userId);
final ServiceCallback<FriendshipRepoChangeRootResponse> callback) {
change("unblock", userId, targetUserId, crsfToken, callback);
}
public void restrict(final String targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipRepoRestrictRootResponse> callback) {
}
public void toggleRestrict(final String targetUserId,
final boolean restrict,
final String csrfToken,
final ServiceCallback<FriendshipRepoRestrictRootResponse> callback) {
final Map<String, String> form = new HashMap<>(3);
form.put("_csrftoken", csrfToken);
form.put("_uuid", UUID.randomUUID().toString());
form.put("user_id", targetUserId);
final Map<String, String> signedForm = Utils.sign(form);
final Call<FriendshipRepositoryChangeResponseRootObject> request = repository.change(action, targetUserId, signedForm);
request.enqueue(new Callback<FriendshipRepositoryChangeResponseRootObject>() {
form.put("target_user_id", targetUserId);
final String action = restrict ? "restrict" : "unrestrict";
final Call<FriendshipRepoRestrictRootResponse> request = repository.toggleRestrict(Constants.I_USER_AGENT, action, form);
request.enqueue(new Callback<FriendshipRepoRestrictRootResponse>() {
@Override
public void onResponse(@NonNull final Call<FriendshipRepositoryChangeResponseRootObject> call,
@NonNull final Response<FriendshipRepositoryChangeResponseRootObject> response) {
public void onResponse(@NonNull final Call<FriendshipRepoRestrictRootResponse> call,
@NonNull final Response<FriendshipRepoRestrictRootResponse> response) {
if (callback != null) {
callback.onSuccess(response.body());
}
}
@Override
public void onFailure(@NonNull final Call<FriendshipRepositoryChangeResponseRootObject> call,
public void onFailure(@NonNull final Call<FriendshipRepoRestrictRootResponse> call,
@NonNull final Throwable t) {
if (callback != null) {
callback.onFailure(t);
}
}
});
}
private void change(final String action,
final String userId,
final String targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipRepoChangeRootResponse> callback) {
final Map<String, Object> form = new HashMap<>(5);
form.put("_csrftoken", csrfToken);
form.put("_uid", userId);
form.put("_uuid", UUID.randomUUID().toString());
form.put("radio_type", "wifi-none");
form.put("user_id", targetUserId);
final Map<String, String> signedForm = Utils.sign(form);
final Call<FriendshipRepoChangeRootResponse> request = repository.change(Constants.I_USER_AGENT, action, targetUserId, signedForm);
request.enqueue(new Callback<FriendshipRepoChangeRootResponse>() {
@Override
public void onResponse(@NonNull final Call<FriendshipRepoChangeRootResponse> call,
@NonNull final Response<FriendshipRepoChangeRootResponse> response) {
if (callback != null) {
callback.onSuccess(response.body());
}
}
@Override
public void onFailure(@NonNull final Call<FriendshipRepoChangeRootResponse> call,
@NonNull final Throwable t) {
if (callback != null) {
callback.onFailure(t);

View File

@ -0,0 +1,29 @@
package awais.instagrabber.services;
import android.util.Log;
import androidx.annotation.NonNull;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
class LoggingInterceptor implements Interceptor {
private static final String TAG = "LoggingInterceptor";
@NonNull
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Log.i(TAG, String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Log.i(TAG, String.format("Received response for %s in %.1fms%n%s\nbody: %s",
response.request().url(), (t2 - t1) / 1e6d, response.headers(), response.body()));
return response;
}
}