Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For inline ActivityResult use Google's new ActivityResultContracts #78

Closed
raghavsatyadev opened this issue Apr 27, 2020 · 3 comments
Closed

Comments

@raghavsatyadev
Copy link

raghavsatyadev commented Apr 27, 2020

I am providing a link for it here : Inline Activity Result

I have also made a helper class for you to use

Usage :

ActivityResultUtils.startActivityForResult(this,
        new Intent(this, CampusSelectionActivity.class),
        (resultType, data) -> {
            switch (resultType) {
                case CANCELED:
                    //Cancelled
                    break;
                case OK:
                    //Success
                    break;
                case FIRST_USER:
                    //First User
                    break;
            }
        });

Helper :

import android.content.Intent;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.Fragment;

import java.util.Arrays;

public class ActivityResultUtils {
    public static void startActivityForResult(AppCompatActivity activity, Intent intent,
                                              ResultListener listener) {
        activity.prepareCall(new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (listener != null) {
                        listener.onResult(ResultType.getEnumByResultIndex(result.getResultCode()), result.getData());
                    }
                }).launch(intent);
    }

    public static void startActivityForResult(AppCompatActivity activity, Intent intent,
                                              ActivityOptionsCompat optionsCompat,
                                              ResultListener listener) {
        activity.prepareCall(new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (listener != null) {
                        listener.onResult(ResultType.getEnumByResultIndex(result.getResultCode()), result.getData());
                    }
                }).launch(intent, optionsCompat);
    }

    public static void startActivityForResult(Fragment fragment, Intent intent,
                                              ResultListener listener) {
        fragment.prepareCall(new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (listener != null) {
                        listener.onResult(ResultType.getEnumByResultIndex(result.getResultCode()), result.getData());
                    }
                }).launch(intent);
    }

    public static void startActivityForResult(Fragment fragment, Intent intent,
                                              ActivityOptionsCompat optionsCompat,
                                              ResultListener listener) {
        fragment.prepareCall(new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (listener != null) {
                        listener.onResult(ResultType.getEnumByResultIndex(result.getResultCode()), result.getData());
                    }
                }).launch(intent, optionsCompat);
    }

    public enum ResultType {
        CANCELED(0, 0),
        OK(1, -1),
        FIRST_USER(2, 1);

        private int index;

        private float resultIndex;

        ResultType(int index, float resultIndex) {
            this.index = index;
            this.resultIndex = resultIndex;
        }

        public static ResultType getEnumByIndex(int index) {
            try {
                for (ResultType resultType : ResultType.values())
                    if (resultType.index == index) return resultType;
            } catch (Exception ignored) {
            }
            return CANCELED;
        }

        public static ResultType getEnumByResultIndex(int resultIndex) {
            try {
                for (ResultType resultType : ResultType.values())
                    if (resultType.getResultIndex() == resultIndex) return resultType;
            } catch (Exception ignored) {
            }
            return CANCELED;
        }

        public float getResultIndex() {
            return resultIndex;
        }

        public static int size() {
            return Arrays.asList(ResultType.values()).size();
        }

        public int getIndex() {
            return index;
        }
    }

    public interface ResultListener {
        void onResult(ResultType resultType, Intent data);
    }
}
@Dhaval2404
Copy link
Owner

Dhaval2404 commented Apr 27, 2020

@raghavsatyadev Thank you so much! With the help of this, I will make something similar to current InlineActivityResult implementation. So that users can use it without any major modifications.

Dhaval2404 added a commit that referenced this issue May 4, 2020
Replace InlineActivityResult with ActivityResultContract

#32 #78
@Dhaval2404 Dhaval2404 added this to the v1.8 milestone May 4, 2020
@VincentJoshuaET
Copy link

VincentJoshuaET commented Jan 4, 2021

Is this possible already? Can it now be used with crop?

@raghavsatyadev
Copy link
Author

@Dhaval2404 have you actually merged this commit in the main branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants