Skip to content

Commit

Permalink
feat: Redesign Library Downloader Dialog UI
Browse files Browse the repository at this point in the history
  • Loading branch information
aikrq committed Dec 29, 2024
1 parent 56e74e1 commit 61ad772
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.gson.Gson;

Expand All @@ -32,85 +32,81 @@
import pro.sketchware.utility.FileUtil;
import pro.sketchware.utility.SketchwareUtil;

public class LibraryDownloaderDialogFragment extends DialogFragment {
public class LibraryDownloaderDialogFragment extends BottomSheetDialogFragment {
private LibraryDownloaderDialogBinding binding;
private final Gson gson = new Gson();
private boolean notAssociatedWithProject = false;
private String dependencyName;
private BuildSettings buildSettings;
private String local_lib_file = "";
private LibraryDownloaderListener listener;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
binding = LibraryDownloaderDialogBinding.inflate(getLayoutInflater());
initVariables();
private Gson gson = new Gson();
private BuildSettings buildSettings;

MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
builder.setView(binding.getRoot());
private boolean notAssociatedWithProject;
private String dependencyName;
private String localLibFile;
private OnLibraryDownloadedTask onLibraryDownloadedTask;

return builder.create();
public interface OnLibraryDownloadedTask {
void invoke();
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = LibraryDownloaderDialogBinding.inflate(inflater, container, false);
return binding.getRoot();
}

public void setListener(LibraryDownloaderListener listener) {
this.listener = listener;
}

public interface LibraryDownloaderListener {
void onTaskCompleted();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

private void initVariables() {
if (getArguments() == null) return;

notAssociatedWithProject = getArguments().getBoolean("notAssociatedWithProject", false);
buildSettings = (BuildSettings) getArguments().getSerializable("buildSettings");
local_lib_file = getArguments().getString("local_lib_file");
localLibFile = getArguments().getString("localLibFile");

binding.btnCancel.setOnClickListener(v -> dismiss());
binding.btnDownload.setOnClickListener(v -> initDownloadFlow());
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
binding.downloadButton.setOnClickListener(v1 -> initDownloadFlow());
public void setOnLibraryDownloadedTask(OnLibraryDownloadedTask onLibraryDownloadedTask) {
this.onLibraryDownloadedTask = onLibraryDownloadedTask;
}

private void initDownloadFlow() {
dependencyName = Objects.requireNonNull(binding.dependencyInput.getText()).toString();
if (dependencyName.isEmpty()) {
SketchwareUtil.toastError("Please enter a dependency");
dependencyName = binding.dependencyInput.getText().toString();
if (dependencyName == null || dependencyName.isEmpty()) {
binding.dependencyInput.setError("Please enter a dependency");
binding.dependencyInput.setErrorEnabled(true);
return;
}

var parts = dependencyName.split(":");
if (parts.length != 3) {
SketchwareUtil.toastError("Invalid dependency format");
binding.dependencyInput.setError("Invalid dependency format");
binding.dependencyInput.setErrorEnabled(true);
return;
}

binding.downloadStatusTxt.setText("Looking for dependency...");
binding.dependencyInfo.setText("Looking for dependency...");
binding.dependencyInput.setErrorEnabled(false);
setDownloadState(true);

var group = parts[0];
var artifact = parts[1];
var version = parts[2];
var resolver = new DependencyResolver(group, artifact, version, binding.skipSubDependenciesCheckBox.isChecked(), buildSettings);
var resolver = new DependencyResolver(group, artifact, version, binding.cbSkipSubdependencies.isChecked(), buildSettings);
var handler = new Handler(Looper.getMainLooper());

class SetTextRunnable implements Runnable {
private final String message;
private final String text;

SetTextRunnable(String message) {
this.message = message;
SetTextRunnable(String text) {
this.text = text;
}

@Override
public void run() {
binding.downloadStatusTxt.setText(message);
binding.dependencyInfo.setText(text);
}
}

Expand Down Expand Up @@ -209,29 +205,27 @@ public void onTaskCompleted(@NonNull List<String> dependencies) {
SketchwareUtil.toast("Library downloaded successfully");
if (!notAssociatedWithProject) {
new SetTextRunnable("Adding dependencies to project...").run();
var fileContent = FileUtil.readFile(local_lib_file);
var fileContent = FileUtil.readFile(localLibFile);
var enabledLibs = gson.fromJson(fileContent, Helper.TYPE_MAP_LIST);
enabledLibs.addAll(dependencies.stream()
.map(name -> ManageLocalLibraryActivity.createLibraryMap(name, dependencyName))
.collect(Collectors.toList()));
FileUtil.writeFile(local_lib_file, gson.toJson(enabledLibs));
FileUtil.writeFile(localLibFile, gson.toJson(enabledLibs));
}
if (getActivity() == null) return;
dismiss();
if (listener != null) listener.onTaskCompleted();
if (onLibraryDownloadedTask != null) onLibraryDownloadedTask.invoke();
});
}
});
});
}

private void setDownloadState(boolean downloading) {
binding.downloadButton.setEnabled(!downloading);
binding.btnCancel.setVisibility(downloading ? View.GONE : View.VISIBLE);
binding.btnDownload.setEnabled(!downloading);
binding.dependencyInput.setEnabled(!downloading);
binding.downloadButton.setText(downloading ? "Downloading..." : "Download");
binding.downloadProgressCardView.setVisibility(downloading ? View.VISIBLE : View.GONE);
binding.hintText.setVisibility(downloading ? View.GONE : View.VISIBLE);
binding.skipSubDependenciesCheckBox.setVisibility(downloading ? View.GONE : View.VISIBLE);
binding.cbSkipSubdependencies.setEnabled(!downloading);
setCancelable(!downloading);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import mod.hey.studios.util.Helper;
import mod.jbk.util.AddMarginOnApplyWindowInsetsListener;

import pro.sketchware.R;
import pro.sketchware.databinding.ManageLocallibrariesBinding;
import pro.sketchware.databinding.ViewItemLocalLibBinding;
import pro.sketchware.databinding.ViewItemLocalLibSearchBinding;
Expand Down Expand Up @@ -96,7 +95,7 @@ protected void onCreate(Bundle savedInstanceState) {

LibraryDownloaderDialogFragment fragment = new LibraryDownloaderDialogFragment();
fragment.setArguments(bundle);
fragment.setListener(this::loadLibraries);
fragment.setOnLibraryDownloadedTask(this::loadLibraries);
fragment.show(getSupportFragmentManager(), "library_downloader_dialog");
});

Expand Down
110 changes: 49 additions & 61 deletions app/src/main/res/layout/library_downloader_dialog.xml
Original file line number Diff line number Diff line change
@@ -1,95 +1,83 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="4dp">
android:orientation="vertical">

<com.google.android.material.bottomsheet.BottomSheetDragHandleView
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="14dp"
android:paddingHorizontal="4dp"
android:text="@string/local_library_manager_enter_dependency"
android:textAppearance="?attr/textAppearanceTitleLargeEmphasized" />
android:layout_marginStart="16dp"
android:layout_marginBottom="10dp"
android:text="Download a dependency"
android:textAppearance="?attr/textAppearanceHeadlineSmallEmphasized" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/dependencyInputLayout"
android:id="@+id/dependency_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="16dp"
android:hint="@string/local_library_manager_dependency_example"
android:importantForAutofill="noExcludeDescendants">
android:layout_marginTop="12dp"
android:hint="Enter a dependency"
android:importantForAutofill="noExcludeDescendants"
app:placeholderText="@string/local_library_manager_dependency_example">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/dependencyInput"
android:id="@+id/dependency_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

<TextView
android:id="@+id/hintText"
android:id="@+id/dependency_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:layout_marginTop="8dp"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:maxLines="2"
android:text="@string/local_library_manager_dependency_info"
android:textColor="?attr/colorOnSurface"
android:textAppearance="?attr/textAppearanceBodyMedium" />

<com.google.android.material.card.MaterialCardView
android:id="@+id/downloadProgressCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="14dp"
android:visibility="gone"
tools:visibility="visible">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingVertical="12dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:text="@string/local_library_manager_download_status"
android:textColor="?attr/colorPrimary"
android:textSize="14sp" />

<TextView
android:id="@+id/downloadStatusTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:layout_marginTop="4dp"
android:textSize="14sp"
android:singleLine="true"
tools:text="Downloading dependencies..." />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>

<CheckBox
android:id="@+id/skipSubDependenciesCheckBox"
android:id="@+id/cb_skip_subdependencies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="6dp"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:paddingStart="8dp"
android:text="@string/local_library_manager_skip_downloading_dependencies" />

<com.google.android.material.button.MaterialButton
android:id="@+id/downloadButton"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="@string/common_word_download" />
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="16dp"
android:orientation="horizontal">

<Button
android:id="@+id/btn_cancel"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:text="@string/common_word_cancel" />

<Button
android:id="@+id/btn_download"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/common_word_download" />

</LinearLayout>

</LinearLayout>
2 changes: 0 additions & 2 deletions app/src/main/res/layout/view_item_local_lib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
android:singleLine="true"
android:textAppearance="?attr/textAppearanceBodyLarge"
android:textColor="?attr/colorOnSurface"
android:textSize="17sp"
app:layout_constraintEnd_toStartOf="@id/checkbox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/library_size"
Expand All @@ -43,7 +42,6 @@
android:layout_marginTop="2dp"
android:textAppearance="?attr/textAppearanceBodyMedium"
android:textColor="?attr/colorOnSurfaceVariant"
android:textSize="15sp"
app:layout_constraintEnd_toStartOf="@id/checkbox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/library_name"
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/layout/view_item_local_lib_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
android:singleLine="true"
android:textAppearance="?attr/textAppearanceBodyLarge"
android:textColor="?attr/colorOnSurface"
android:textSize="17sp"
app:layout_constraintEnd_toStartOf="@id/checkbox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/library_size"
Expand All @@ -31,7 +30,6 @@
android:layout_marginTop="2dp"
android:textAppearance="?attr/textAppearanceBodyMedium"
android:textColor="?attr/colorOnSurfaceVariant"
android:textSize="15sp"
app:layout_constraintEnd_toStartOf="@id/checkbox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/library_name"
Expand Down

0 comments on commit 61ad772

Please sign in to comment.