This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RxPermissions.getInstance(this, new HandleOutOfActivityObject(viewIdOfResultView)) | |
.request(Manifest.permission.CAMERA) | |
.subscribe(granted -> { | |
if (granted) resultView.permissionGranted(); | |
} | |
resultView { | |
public void permissionGranted(); | |
} | |
// This object you'd want to save to state if the request fails and you need to leave the activity: | |
class HandleOutOfActivityObject { | |
int viewId; | |
public void resolve() { | |
findViewById(viewId).permissionGranted(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RxActivityDelegate { | |
private static final String KEY_OBJECT = "rxActivityController.object"; | |
private static final String KEY_OBJECTTYPE = "rxActivityController.object.type"; | |
private static final String KEY_REQUESTCODE = "rxActivityController.requestCode"; | |
public static void setResponse(Activity activity, int requestCode, RxResponseHandler rxResponseHandler) { | |
Intent intent = activity.getIntent(); | |
intent.putExtra(KEY_OBJECT, JsonUtils.toBytes(rxResponseHandler)); | |
intent.putExtra(KEY_OBJECTTYPE, rxResponseHandler.getClass().getName()); | |
intent.putExtra(KEY_REQUESTCODE, requestCode); | |
} | |
public static void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { | |
if (activity.getIntent().hasExtra(KEY_REQUESTCODE)) { | |
int ourRequestCode = activity.getIntent().getIntExtra(KEY_REQUESTCODE, Integer.MIN_VALUE); | |
if (ourRequestCode == requestCode) { | |
try { | |
Class<Object> objectType = (Class<Object>) Class.forName(activity.getIntent().getStringExtra(KEY_OBJECTTYPE)); | |
RxResponseHandler responseHandler = JsonUtils.getSmileReader(objectType).readValue(activity.getIntent().getByteArrayExtra(KEY_OBJECT)); | |
responseHandler.onActivityResult(activity, requestCode, resultCode, data); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
throw new IllegalStateException(e); | |
} catch (JsonProcessingException e) { | |
e.printStackTrace(); | |
throw new IllegalStateException(e); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
throw new IllegalStateException(e); | |
} | |
} | |
} | |
} | |
public interface RxResponseHandler { | |
void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment