Search in sources :

Example 31 with Pair

use of androidx.core.util.Pair in project materialistic by hidroh.

the class UserServicesClient method submit.

@Override
public void submit(Context context, String title, String content, boolean isUrl, Callback callback) {
    Pair<String, String> credentials = AppUtils.getCredentials(context);
    if (credentials == null) {
        callback.onDone(false);
        return;
    }
    /*
          The flow:
          POST /submit with acc, pw
           if 302 to /login, considered failed
          POST /r with fnid, fnop, title, url or text
           if 302 to /newest, considered successful
           if 302 to /x, considered error, maybe duplicate or invalid input
           if 200 or anything else, considered error
         */
    // fetch submit page with given credentials
    execute(postSubmitForm(credentials.first, credentials.second)).flatMap(response -> response.code() != HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(response) : Observable.error(new IOException())).flatMap(response -> {
        try {
            return Observable.just(new String[] { response.header(HEADER_SET_COOKIE), response.body().string() });
        } catch (IOException e) {
            return Observable.error(e);
        } finally {
            response.close();
        }
    }).map(array -> {
        array[1] = getInputValue(array[1], SUBMIT_PARAM_FNID);
        return array;
    }).flatMap(array -> !TextUtils.isEmpty(array[1]) ? Observable.just(array) : Observable.error(new IOException())).flatMap(array -> execute(postSubmit(title, content, isUrl, array[0], array[1]))).flatMap(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(Uri.parse(response.header(HEADER_LOCATION))) : Observable.error(new IOException())).flatMap(uri -> TextUtils.equals(uri.getPath(), DEFAULT_SUBMIT_REDIRECT) ? Observable.just(true) : Observable.error(buildException(uri))).observeOn(AndroidSchedulers.mainThread()).subscribe(callback::onDone, callback::onError);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Context(android.content.Context) Request(okhttp3.Request) Uri(android.net.Uri) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Scheduler(rx.Scheduler) Observable(rx.Observable) Inject(javax.inject.Inject) AppUtils(io.github.hidroh.materialistic.AppUtils) Matcher(java.util.regex.Matcher) FormBody(okhttp3.FormBody) R(io.github.hidroh.materialistic.R) Toast(android.widget.Toast) Pair(androidx.core.util.Pair) Response(okhttp3.Response) Call(okhttp3.Call) Pattern(java.util.regex.Pattern) HttpUrl(okhttp3.HttpUrl) IOException(java.io.IOException)

Aggregations

Pair (androidx.core.util.Pair)31 ArrayList (java.util.ArrayList)9 EpisodeAction (de.danoeh.antennapod.net.sync.model.EpisodeAction)7 ReferenceManager (org.javarosa.core.reference.ReferenceManager)6 Test (org.junit.Test)6 AppDependencyModule (org.odk.collect.android.injection.config.AppDependencyModule)6 CollectHelpers.setupFakeReferenceManager (org.odk.collect.android.support.CollectHelpers.setupFakeReferenceManager)6 MockFormEntryPromptBuilder (org.odk.collect.android.support.MockFormEntryPromptBuilder)6 Date (java.util.Date)5 ImageView (android.widget.ImageView)4 SimpleDateFormat (java.text.SimpleDateFormat)4 RandomString (net.bytebuddy.utility.RandomString)4 BitmapDrawable (android.graphics.drawable.BitmapDrawable)3 Drawable (android.graphics.drawable.Drawable)3 Uri (android.net.Uri)3 View (android.view.View)3 Nullable (androidx.annotation.Nullable)3 FileWidgetTest (org.odk.collect.android.widgets.base.FileWidgetTest)3 SynchronousImageLoader (org.odk.collect.android.widgets.support.SynchronousImageLoader)3 ImageLoader (org.odk.collect.imageloader.ImageLoader)3