Skip to content

Commit

Permalink
Merge pull request #269 from wealthfront/non-activity-context
Browse files Browse the repository at this point in the history
Support non-activity context in DialogComponent
  • Loading branch information
Kritarie committed Jan 6, 2023
2 parents b965842 + 2312200 commit 0654719
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.wealthfront.magellan;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Parcelable;
import android.util.SparseArray;
import android.view.ViewGroup;
Expand Down Expand Up @@ -44,17 +42,6 @@ public void hide(@NotNull Context context) {
}

private void setActivity(@NotNull Context context) {
screen.setActivity(findActivity(context));
}

@NotNull
private Activity findActivity(@NotNull Context context) {
if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof ContextWrapper) {
return findActivity(((ContextWrapper) context).getBaseContext());
} else {
throw new IllegalStateException("Context must be Activity or wrap Activity");
}
screen.setActivity(ContextUtil.INSTANCE.findActivity(context));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.wealthfront.magellan

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper

public object ContextUtil {
public fun findActivity(context: Context): Activity {
return when (context) {
is Activity -> {
context
}
is ContextWrapper -> {
findActivity(context.baseContext)
}
else -> {
throw IllegalStateException("Context must be Activity or wrap Activity")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.app.Dialog
import android.content.Context
import androidx.annotation.VisibleForTesting
import com.wealthfront.magellan.ContextUtil
import com.wealthfront.magellan.DialogCreator
import com.wealthfront.magellan.lifecycle.LifecycleAware
import javax.inject.Inject
Expand Down Expand Up @@ -42,9 +43,12 @@ public class DialogComponent @Inject constructor() : LifecycleAware {
}

private fun createDialog() {
val dialogCreator = dialogCreator
val context = context
if (dialogCreator != null && context != null) {
dialog = dialogCreator!!.createDialog(context as Activity)
val dialog = dialogCreator.createDialog(ContextUtil.findActivity(context))
dialog!!.show()
this.dialog = dialog
}
}

Expand Down

0 comments on commit 0654719

Please sign in to comment.