-
-
Notifications
You must be signed in to change notification settings - Fork 24
ADFA-3718 | Require scrolling to end before project creation #1321
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/itsaky/androidide/utils/ProjectCreationManager.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.itsaky.androidide.utils | ||
|
|
||
| import android.content.Context | ||
| import com.itsaky.androidide.R | ||
| import com.itsaky.androidide.roomData.recentproject.RecentProject | ||
| import com.itsaky.androidide.tasks.executeAsyncProvideError | ||
| import com.itsaky.androidide.templates.ProjectTemplateRecipeResult | ||
| import com.itsaky.androidide.templates.StringParameter | ||
| import com.itsaky.androidide.templates.Template | ||
| import com.itsaky.androidide.templates.impl.ConstraintVerifier | ||
|
|
||
| class ProjectCreationManager(private val context: Context) { | ||
|
|
||
| fun execute( | ||
| template: Template<*>, | ||
| onStart: () -> Unit, | ||
| onSuccess: (ProjectTemplateRecipeResult, RecentProject) -> Unit, | ||
| onError: (String) -> Unit | ||
| ) { | ||
| val isValid = template.parameters.filterIsInstance<StringParameter>().all { param -> | ||
| ConstraintVerifier.isValid(param.value, param.constraints) | ||
| } | ||
|
|
||
| if (!isValid) { | ||
| onError(context.getString(R.string.msg_invalid_project_details)) | ||
| return | ||
| } | ||
|
|
||
| onStart() | ||
|
|
||
| executeAsyncProvideError({ | ||
| template.recipe.execute(TemplateRecipeExecutor(context.applicationContext)) | ||
| }) { result, err -> | ||
| if (result == null || err != null || result !is ProjectTemplateRecipeResult) { | ||
| err?.printStackTrace() | ||
| val errorMsg = err?.cause?.message ?: err?.message ?: context.getString(R.string.project_creation_failed) | ||
| onError(errorMsg) | ||
| return@executeAsyncProvideError | ||
| } | ||
|
|
||
| val now = System.currentTimeMillis().toString() | ||
| val project = RecentProject( | ||
| location = result.data.projectDir.path, | ||
| name = result.data.name, | ||
| createdAt = now, | ||
| lastModified = now, | ||
| templateName = template.templateNameStr, | ||
| language = result.data.language?.name ?: "unknown" | ||
| ) | ||
|
|
||
| onSuccess(result, project) | ||
| } | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
app/src/main/java/com/itsaky/androidide/utils/ui/TemplateScrollGateKeeper.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package com.itsaky.androidide.utils.ui | ||
|
|
||
| import android.view.View | ||
| import android.view.ViewTreeObserver | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
|
|
||
| /** | ||
| * Monitors a [RecyclerView] to detect when the user scrolls to the bottom. | ||
| * Once the bottom is reached, the state is locked to `true` until manually reset or the layout width changes. | ||
| * | ||
| * @param recyclerView The list to monitor. | ||
| * @param onScrollStateChanged Callback invoked when the [hasReachedEnd] state changes. | ||
| */ | ||
| class TemplateScrollGateKeeper( | ||
|
jatezzz marked this conversation as resolved.
|
||
| private val recyclerView: RecyclerView, | ||
| private var onScrollStateChanged: (() -> Unit)? | ||
| ) { | ||
| /** | ||
| * `true` if the user has scrolled to the bottom of the list at least once. | ||
| */ | ||
| var hasReachedEnd = false | ||
| private set | ||
|
|
||
| private var lastWidth = -1 | ||
|
|
||
| private val scrollListener = object : RecyclerView.OnScrollListener() { | ||
| override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { | ||
| checkIfReachedEnd() | ||
| } | ||
| } | ||
|
|
||
| private val layoutChangeListener = View.OnLayoutChangeListener { _, left, _, right, _, _, _, _, _ -> | ||
| val currentWidth = right - left | ||
|
|
||
| if (lastWidth != -1 && lastWidth != currentWidth) { | ||
| hasReachedEnd = false | ||
| onScrollStateChanged?.invoke() | ||
| } | ||
| lastWidth = currentWidth | ||
| } | ||
|
|
||
| private val globalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener { | ||
| checkIfReachedEnd() | ||
| } | ||
|
|
||
| /** | ||
| * Attaches scroll and layout listeners to the [RecyclerView]. | ||
| */ | ||
| fun attach() { | ||
| recyclerView.addOnScrollListener(scrollListener) | ||
| recyclerView.addOnLayoutChangeListener(layoutChangeListener) | ||
| recyclerView.viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener) | ||
| } | ||
|
|
||
| /** | ||
| * Detaches listeners from the [RecyclerView] to prevent memory leaks. | ||
| */ | ||
| fun detach() { | ||
| recyclerView.removeOnScrollListener(scrollListener) | ||
| recyclerView.removeOnLayoutChangeListener(layoutChangeListener) | ||
| if (recyclerView.viewTreeObserver.isAlive) { | ||
| recyclerView.viewTreeObserver.removeOnGlobalLayoutListener(globalLayoutListener) | ||
| } | ||
| onScrollStateChanged = null | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Resets the gatekeeper state and notifies the callback. | ||
| */ | ||
| fun reset() { | ||
| hasReachedEnd = false | ||
| lastWidth = -1 | ||
| onScrollStateChanged?.invoke() | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates the scroll position and updates [hasReachedEnd] if the bottom is reached. | ||
| */ | ||
| fun checkIfReachedEnd() { | ||
| if (hasReachedEnd) return | ||
|
|
||
| val layoutManager = recyclerView.layoutManager as? LinearLayoutManager ?: return | ||
| val itemCount = layoutManager.itemCount | ||
| if (itemCount == 0) return | ||
|
|
||
| val lastVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition() | ||
|
|
||
| if (lastVisibleItem == RecyclerView.NO_POSITION) return | ||
|
|
||
| val isAtBottom = !recyclerView.canScrollVertically(1) | ||
|
|
||
| if (lastVisibleItem >= itemCount - 1 || isAtBottom) { | ||
| hasReachedEnd = true | ||
| onScrollStateChanged?.invoke() | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.