Refactor create release/tag screen

This commit is contained in:
M M Arif 2023-10-07 12:03:51 +05:00
parent 448ffc88d4
commit 74ba524a10
3 changed files with 151 additions and 209 deletions

View File

@ -1,15 +1,10 @@
package org.mian.gitnex.activities;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import androidx.annotation.NonNull;
import com.vdurmont.emoji.EmojiParser;
@ -25,9 +20,8 @@ import org.mian.gitnex.R;
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.databinding.ActivityCreateReleaseBinding;
import org.mian.gitnex.helpers.AlertDialogs;
import org.mian.gitnex.helpers.AppUtil;
import org.mian.gitnex.helpers.Markdown;
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.SnackBar;
import org.mian.gitnex.helpers.contexts.RepositoryContext;
import retrofit2.Call;
import retrofit2.Callback;
@ -39,10 +33,8 @@ public class CreateReleaseActivity extends BaseActivity {
private ActivityCreateReleaseBinding binding;
List<String> branchesList = new ArrayList<>();
private View.OnClickListener onClickListener;
private String selectedBranch;
private RepositoryContext repository;
private final View.OnClickListener createReleaseListener = v -> processNewRelease();
private boolean renderMd = false;
@SuppressLint("ClickableViewAccessibility")
@ -53,19 +45,9 @@ public class CreateReleaseActivity extends BaseActivity {
binding = ActivityCreateReleaseBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
repository = RepositoryContext.fromIntent(getIntent());
binding.releaseTitle.requestFocus();
assert imm != null;
imm.showSoftInput(binding.releaseTitle, InputMethodManager.SHOW_IMPLICIT);
binding.releaseContent.setOnTouchListener(
(touchView, motionEvent) -> {
touchView.getParent().requestDisallowInterceptTouchEvent(true);
@ -78,90 +60,72 @@ public class CreateReleaseActivity extends BaseActivity {
return false;
});
initCloseListener();
binding.close.setOnClickListener(onClickListener);
binding.topAppBar.setNavigationOnClickListener(
v -> {
finish();
});
binding.topAppBar.setOnMenuItemClickListener(
menuItem -> {
int id = menuItem.getItemId();
if (id == R.id.markdown) {
if (!renderMd) {
Markdown.render(
ctx,
EmojiParser.parseToUnicode(
Objects.requireNonNull(
Objects.requireNonNull(
binding.releaseContent
.getText())
.toString())),
binding.markdownPreview);
binding.markdownPreview.setVisibility(View.VISIBLE);
binding.releaseContentLayout.setVisibility(View.GONE);
renderMd = true;
} else {
binding.markdownPreview.setVisibility(View.GONE);
binding.releaseContentLayout.setVisibility(View.VISIBLE);
renderMd = false;
}
return true;
} else if (id == R.id.create) {
processNewRelease();
return true;
} else if (id == R.id.create_tag) {
createNewTag();
return true;
} else {
return super.onOptionsItemSelected(menuItem);
}
});
getBranches(repository.getOwner(), repository.getName());
disableProcessButton();
if (!connToInternet) {
disableProcessButton();
} else {
binding.createNewRelease.setOnClickListener(createReleaseListener);
}
binding.createNewTag.setOnClickListener(v -> createNewTag());
}
@Override
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.markdown_switcher, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.markdown) {
if (!renderMd) {
Markdown.render(
ctx,
EmojiParser.parseToUnicode(
Objects.requireNonNull(
Objects.requireNonNull(binding.releaseContent.getText())
.toString())),
binding.markdownPreview);
binding.markdownPreview.setVisibility(View.VISIBLE);
binding.releaseContentLayout.setVisibility(View.GONE);
renderMd = true;
} else {
binding.markdownPreview.setVisibility(View.GONE);
binding.releaseContentLayout.setVisibility(View.VISIBLE);
renderMd = false;
}
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
private void createNewTag() {
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
String tagName = Objects.requireNonNull(binding.releaseTagName.getText()).toString();
String message =
Objects.requireNonNull(binding.releaseTitle.getText()).toString()
Objects.requireNonNull(binding.releaseTitle.getText())
+ "\n\n"
+ Objects.requireNonNull(binding.releaseContent.getText()).toString();
if (!connToInternet) {
Toasty.error(ctx, getResources().getString(R.string.checkNetConnection));
return;
}
+ Objects.requireNonNull(binding.releaseContent.getText());
if (tagName.equals("")) {
Toasty.error(ctx, getString(R.string.tagNameErrorEmpty));
SnackBar.error(
ctx, findViewById(android.R.id.content), getString(R.string.tagNameErrorEmpty));
return;
}
if (selectedBranch == null) {
Toasty.error(ctx, getString(R.string.selectBranchError));
SnackBar.error(
ctx, findViewById(android.R.id.content), getString(R.string.selectBranchError));
return;
}
disableProcessButton();
CreateTagOption createReleaseJson = new CreateTagOption();
createReleaseJson.setMessage(message);
createReleaseJson.setTagName(tagName);
@ -173,7 +137,7 @@ public class CreateReleaseActivity extends BaseActivity {
repository.getOwner(), repository.getName(), createReleaseJson);
call.enqueue(
new Callback<Tag>() {
new Callback<>() {
@Override
public void onResponse(
@ -182,35 +146,38 @@ public class CreateReleaseActivity extends BaseActivity {
if (response.code() == 201) {
RepoDetailActivity.updateFABActions = true;
Toasty.success(ctx, getString(R.string.tagCreated));
finish();
SnackBar.success(
ctx,
findViewById(android.R.id.content),
getString(R.string.tagCreated));
new Handler().postDelayed(() -> finish(), 3000);
} else if (response.code() == 401) {
enableProcessButton();
AlertDialogs.authorizationTokenRevokedDialog(ctx);
} else if (response.code() == 403) {
enableProcessButton();
Toasty.error(ctx, ctx.getString(R.string.authorizeError));
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.authorizeError));
} else if (response.code() == 404) {
enableProcessButton();
Toasty.warning(ctx, ctx.getString(R.string.apiNotFound));
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.apiNotFound));
} else {
enableProcessButton();
Toasty.error(ctx, ctx.getString(R.string.genericError));
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.genericError));
}
}
@Override
public void onFailure(@NonNull Call<Tag> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
enableProcessButton();
}
public void onFailure(@NonNull Call<Tag> call, @NonNull Throwable t) {}
});
}
private void processNewRelease() {
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
String newReleaseTagName =
Objects.requireNonNull(binding.releaseTagName.getText()).toString();
String newReleaseTitle = Objects.requireNonNull(binding.releaseTitle.getText()).toString();
@ -220,31 +187,24 @@ public class CreateReleaseActivity extends BaseActivity {
boolean newReleaseType = binding.releaseType.isChecked();
boolean newReleaseDraft = binding.releaseDraft.isChecked();
if (!connToInternet) {
Toasty.error(ctx, getResources().getString(R.string.checkNetConnection));
return;
}
if (newReleaseTitle.equals("")) {
Toasty.error(ctx, getString(R.string.titleErrorEmpty));
SnackBar.error(
ctx, findViewById(android.R.id.content), getString(R.string.titleErrorEmpty));
return;
}
if (newReleaseTagName.equals("")) {
Toasty.error(ctx, getString(R.string.tagNameErrorEmpty));
SnackBar.error(
ctx, findViewById(android.R.id.content), getString(R.string.tagNameErrorEmpty));
return;
}
if (checkBranch == null) {
Toasty.error(ctx, getString(R.string.selectBranchError));
SnackBar.error(
ctx, findViewById(android.R.id.content), getString(R.string.selectBranchError));
return;
}
disableProcessButton();
createNewReleaseFunc(
repository.getOwner(),
repository.getName(),
@ -289,31 +249,37 @@ public class CreateReleaseActivity extends BaseActivity {
if (response.code() == 201) {
RepoDetailActivity.updateFABActions = true;
Toasty.success(ctx, getString(R.string.releaseCreatedText));
finish();
SnackBar.success(
ctx,
findViewById(android.R.id.content),
getString(R.string.releaseCreatedText));
new Handler().postDelayed(() -> finish(), 3000);
} else if (response.code() == 401) {
enableProcessButton();
AlertDialogs.authorizationTokenRevokedDialog(ctx);
} else if (response.code() == 403) {
enableProcessButton();
Toasty.error(ctx, ctx.getString(R.string.authorizeError));
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.authorizeError));
} else if (response.code() == 404) {
enableProcessButton();
Toasty.warning(ctx, ctx.getString(R.string.apiNotFound));
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.apiNotFound));
} else {
enableProcessButton();
Toasty.error(ctx, ctx.getString(R.string.genericError));
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.genericError));
}
}
@Override
public void onFailure(@NonNull Call<Release> call, @NonNull Throwable t) {
enableProcessButton();
}
public void onFailure(@NonNull Call<Release> call, @NonNull Throwable t) {}
});
}
@ -349,7 +315,6 @@ public class CreateReleaseActivity extends BaseActivity {
branchesList);
binding.releaseBranch.setAdapter(adapter);
enableProcessButton();
binding.releaseBranch.setOnItemClickListener(
(parent, view, position, id) ->
@ -366,21 +331,6 @@ public class CreateReleaseActivity extends BaseActivity {
});
}
private void initCloseListener() {
onClickListener = view -> finish();
}
private void disableProcessButton() {
binding.createNewTag.setEnabled(false);
binding.createNewRelease.setEnabled(false);
}
private void enableProcessButton() {
binding.createNewTag.setEnabled(true);
binding.createNewRelease.setEnabled(true);
}
@Override
public void onResume() {
super.onResume();

View File

@ -1,61 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/primaryBackgroundColor"
android:orientation="vertical">
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Widget.AppCompat.SearchView"
app:elevation="@dimen/dimen0dp">
android:background="?attr/primaryBackgroundColor"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
<com.google.android.material.appbar.CollapsingToolbarLayout
style="?attr/collapsingToolbarLayoutLargeStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/primaryBackgroundColor">
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
android:background="?attr/primaryBackgroundColor"
app:contentScrim="?attr/primaryBackgroundColor"
android:layout_height="?attr/collapsingToolbarLayoutLargeSize">
<ImageView
android:id="@+id/close"
android:layout_width="@dimen/dimen26dp"
android:layout_height="@dimen/dimen26dp"
android:layout_marginStart="@dimen/dimen16dp"
android:layout_marginEnd="@dimen/dimen16dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/close"
android:focusable="true"
android:gravity="center_vertical"
android:src="@drawable/ic_close"/>
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:elevation="0dp"
android:layout_height="?attr/actionBarSize"
app:title="@string/createRelease"
app:layout_collapseMode="pin"
app:menu="@menu/create_release_tag_menu"
app:navigationIcon="@drawable/ic_close" />
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="1"
android:text="@string/createRelease"
android:textColor="?attr/primaryTextColor"
android:textSize="@dimen/dimen20sp"/>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/primaryBackgroundColor">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/dimen16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/dimen16dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/releaseTitleLayout"
@ -65,7 +54,6 @@
android:layout_marginBottom="@dimen/dimen8dp"
android:hint="@string/releaseTitleText"
android:textColorHint="?attr/hintColor"
app:boxBackgroundColor="?attr/inputBackgroundColor"
app:boxStrokeErrorColor="@color/darkRed"
app:endIconMode="clear_text"
app:endIconTint="?attr/iconsColor"
@ -93,7 +81,6 @@
android:layout_marginBottom="@dimen/dimen8dp"
android:hint="@string/releaseTagNameText"
android:textColorHint="?attr/hintColor"
app:boxBackgroundColor="?attr/inputBackgroundColor"
app:boxStrokeErrorColor="@color/darkRed"
app:endIconMode="clear_text"
app:endIconTint="?attr/iconsColor"
@ -121,7 +108,6 @@
android:layout_marginBottom="@dimen/dimen8dp"
android:hint="@string/releaseContentText"
android:textColorHint="?attr/hintColor"
app:boxBackgroundColor="?attr/inputBackgroundColor"
app:boxStrokeErrorColor="@color/darkRed"
app:endIconMode="clear_text"
app:endIconTint="?attr/iconsColor"
@ -162,7 +148,6 @@
android:layout_marginBottom="@dimen/dimen8dp"
android:hint="@string/releaseBranchText"
android:textColorHint="?attr/hintColor"
app:boxBackgroundColor="?attr/inputBackgroundColor"
app:endIconTint="?attr/iconsColor"
app:hintTextColor="?attr/hintColor">
@ -197,26 +182,8 @@
android:textColor="?attr/primaryTextColor"
android:textSize="@dimen/dimen16sp"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/createNewRelease"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen54dp"
android:layout_marginTop="@dimen/dimen8dp"
android:text="@string/newCreateButtonCopy"
android:textColor="?attr/materialCardBackgroundColor"
android:textStyle="bold"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/createNewTag"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen54dp"
android:layout_marginTop="@dimen/dimen8dp"
android:text="@string/create_tag"
android:textColor="?attr/materialCardBackgroundColor"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/markdown"
android:icon="@drawable/ic_markdown"
android:orderInCategory="0"
android:title="@string/strMarkdown"
app:showAsAction="ifRoom" />
<item
android:id="@+id/create"
android:orderInCategory="1"
android:title="@string/newCreateButtonCopy"
android:contentDescription="@string/newCreateButtonCopy" />
<item
android:id="@+id/create_tag"
android:orderInCategory="2"
android:title="@string/create_tag"
android:contentDescription="@string/create_tag" />
</menu>