aboutsummaryrefslogblamecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/post/FetchRules.java
blob: 6a1efca3906ab6d49cf74ed1319a4b8c5059469e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
                                                  
 
                          

                                   
                                    





                              
                                     
 
                                                            
                                                           
                                                           
                                                           

                                                            










                                            
                                                                                                                      
                                                                                    
                                                                          
                                                         


                                                                                         
                                            


                                                                                                    
                                                                                             
                                 
                                                                    



                                                              
                                              

                                                        
                       











                                                                                     


                                                                                       










                                                                                                                             
                                                                      

                                       
                                                         
             
           

     
package ml.docilealligator.infinityforreddit.post;

import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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

import java.util.ArrayList;
import java.util.concurrent.Executor;

import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.subreddit.Rule;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class FetchRules {
    public interface FetchRulesListener {
        void success(ArrayList<Rule> rules);
        void failed();
    }

    public static void fetchRules(Executor executor, Handler handler, Retrofit retrofit, @Nullable String accessToken,
                                  @NonNull String accountName, String subredditName,
                                  FetchRulesListener fetchRulesListener) {
        RedditAPI api = retrofit.create(RedditAPI.class);
        Call<String> rulesCall = accountName.equals(Account.ANONYMOUS_ACCOUNT)
                ? api.getRules(subredditName)
                : api.getRulesOauth(APIUtils.getOAuthHeader(accessToken), subredditName);
        rulesCall.enqueue(new Callback<>() {
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                if (response.isSuccessful()) {
                    parseRules(executor, handler, response.body(), new FetchRulesListener() {
                        @Override
                        public void success(ArrayList<Rule> rules) {
                            fetchRulesListener.success(rules);
                        }

                        @Override
                        public void failed() {
                            fetchRulesListener.failed();
                        }
                    });
                } else {
                    fetchRulesListener.failed();
                }
            }

            @Override
            public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
                fetchRulesListener.failed();
            }
        });
    }

    private static void parseRules(Executor executor, Handler handler, String response,
                                   FetchRulesListener fetchRulesListener) {
        executor.execute(() -> {
            try {
                JSONArray rulesArray = new JSONObject(response).getJSONArray(JSONUtils.RULES_KEY);
                ArrayList<Rule> rules = new ArrayList<>();
                for (int i = 0; i < rulesArray.length(); i++) {
                    String shortName = rulesArray.getJSONObject(i).getString(JSONUtils.SHORT_NAME_KEY);
                    String description = null;
                    if (rulesArray.getJSONObject(i).has(JSONUtils.DESCRIPTION_KEY)) {
                        description = Utils.modifyMarkdown(rulesArray.getJSONObject(i).getString(JSONUtils.DESCRIPTION_KEY));
                    }
                    rules.add(new Rule(shortName, description));
                }
                handler.post(() -> fetchRulesListener.success(rules));
            } catch (JSONException e) {
                e.printStackTrace();
                handler.post(fetchRulesListener::failed);
            }
        });
    }
}