Skip to content

Commit

Permalink
Refactor button bars and alert dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
DSteve595 committed Dec 28, 2018
1 parent f8875e0 commit 17d8094
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 191 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ android {
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

androidExtensions {
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/com/stevenschoen/putionew/ButtonBarLayout.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.stevenschoen.putionew

import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.widget.LinearLayout
import androidx.core.content.res.getDimensionPixelSizeOrThrow
import androidx.core.content.res.use

class ButtonBarLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.buttonBarLayoutStyle,
defStyleRes: Int = R.style.Widget_Putio_ButtonBarLayout
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {

private var buttonPadding: Int = 0

init {
orientation = HORIZONTAL
gravity = Gravity.END

context.obtainStyledAttributes(attrs, R.styleable.ButtonBarLayout, defStyleAttr, defStyleRes)
.use { attrsArray ->
buttonPadding = attrsArray.getDimensionPixelSizeOrThrow(
R.styleable.ButtonBarLayout_buttonPadding
)
}

dividerDrawable = GradientDrawable().apply {
setSize(buttonPadding, 0)
}
showDividers = SHOW_DIVIDER_MIDDLE
}

}
88 changes: 6 additions & 82 deletions app/src/main/java/com/stevenschoen/putionew/PutioUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
Expand All @@ -24,9 +20,6 @@
import android.renderscript.ScriptIntrinsicBlur;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
Expand All @@ -35,7 +28,6 @@
import com.google.gson.GsonBuilder;
import com.squareup.picasso.Transformation;
import com.stevenschoen.putionew.model.PutioRestInterface;
import com.stevenschoen.putionew.model.ResponseOrError;
import com.stevenschoen.putionew.model.files.PutioFile;
import com.stevenschoen.putionew.model.files.PutioSubtitle;

Expand All @@ -47,9 +39,6 @@
import java.util.List;
import java.util.Locale;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -82,13 +71,6 @@ public PutioUtils(Context context) throws NoTokenException {
this.putioRestInterface = makePutioRestInterface(baseUrl).create(PutioRestInterface.class);
}

public static Dialog showPutioDialog(Context context, String title, int contentViewId) {
return new MaterialAlertDialogBuilder(context)
.setTitle(title)
.setView(contentViewId)
.show();
}

public static String getNameFromUri(Context context, Uri uri) {
if (uri.getScheme().equals("file")) {
return new File(uri.getPath()).getName();
Expand Down Expand Up @@ -126,18 +108,6 @@ public static String resolveRedirect(String url) throws IOException {
return url;
}

private static void open(Uri uri, Context context) {
String typename = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(typename);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, type);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, context.getString(R.string.cantopenbecausetype), Toast.LENGTH_LONG).show();
}
}

public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit)
Expand Down Expand Up @@ -278,17 +248,11 @@ class GetStreamUrlAndPlay extends AsyncTask<String, Void, GetStreamUrlAndPlayRes

@Override
public void onPreExecute() {
gettingStreamDialog = PutioUtils.showPutioDialog(context,
context.getString(R.string.gettingstreamurltitle),
R.layout.dialog_loading);
gettingStreamDialog.setOnCancelListener(new OnCancelListener() {

@Override
public void onCancel(DialogInterface dialog) {
GetStreamUrlAndPlay.this.cancel(true);
}
});
gettingStreamDialog.show();
gettingStreamDialog = new MaterialAlertDialogBuilder(context)
.setTitle(R.string.gettingstreamurltitle)
.setView(R.layout.dialog_loading)
.setOnCancelListener(d -> GetStreamUrlAndPlay.this.cancel(true))
.show();
}

@Override
Expand All @@ -311,7 +275,7 @@ protected GetStreamUrlAndPlayResult doInBackground(String... params) {
// No subtitles, not a problem
} else {
e.printStackTrace();
if (context != null && context instanceof Activity) {
if (context instanceof Activity) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -350,46 +314,6 @@ public void onPostExecute(GetStreamUrlAndPlayResult result) {
new GetStreamUrlAndPlay().execute(url);
}

public Dialog removeTransferDialog(
final Context context, final SingleObserver<ResponseOrError.BasePutioResponse> observer,
final long... idsToDelete) {
final Dialog removeDialog = showPutioDialog(context, context.getString(R.string.removetransfertitle), R.layout.dialog_removetransfer);

Button removeRemove = removeDialog.findViewById(R.id.button_removetransfer_remove);
removeRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Single<ResponseOrError.BasePutioResponse> cancelObservable = getRestInterface()
.cancelTransfer(PutioUtils.longsToString(idsToDelete))
.observeOn(AndroidSchedulers.mainThread());
if (observer != null) {
cancelObservable.subscribe(observer);
} else {
cancelObservable.subscribe();
}
Toast.makeText(context, context.getString(R.string.transferremoved), Toast.LENGTH_SHORT).show();
removeDialog.dismiss();
}
});

Button cancelRemove = removeDialog.findViewById(R.id.button_removetransfer_cancel);
cancelRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
removeDialog.cancel();
}
});

return removeDialog;
}

public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnected();
}

private static class GetStreamUrlAndPlayResult {
String url;
List<PutioSubtitle> subtitles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.stevenschoen.putionew.PutioApplication
import com.stevenschoen.putionew.PutioTransfersService
import com.stevenschoen.putionew.PutioTransfersService.TransfersServiceBinder
Expand Down Expand Up @@ -72,7 +73,7 @@ class TransfersFragment : RxFragment() {

transfersListView = view.findViewById(R.id.transferslist)
transfersListView!!.layoutManager = LinearLayoutManager(
context, LinearLayoutManager.VERTICAL, false
context, RecyclerView.VERTICAL, false
)
val padding = resources.getDimensionPixelSize(R.dimen.transfers_card_padding)
transfersListView!!.addItemDecoration(object : RecyclerView.ItemDecoration() {
Expand Down Expand Up @@ -209,7 +210,18 @@ class TransfersFragment : RxFragment() {
.observeOn(AndroidSchedulers.mainThread())
.subscribe(makeUpdateNowObserver())
} else {
utils!!.removeTransferDialog(activity, makeUpdateNowObserver(), transfer.id).show()
MaterialAlertDialogBuilder(context, R.style.ThemeOverlay_Putio_Dialog_Alert_Destructive)
.setTitle(R.string.removetransfertitle)
.setMessage(R.string.removetransferbody)
.setPositiveButton(R.string.remove) { _, _ ->
utils!!.restInterface.cancelTransfer(transfer.id.toString())
.bindToLifecycle(this)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(makeUpdateNowObserver())
Toast.makeText(context, R.string.transferremoved, Toast.LENGTH_SHORT).show()
}
.setNegativeButton(R.string.cancel, null)
.show()
}
hideOptionsIfShowing()
}
Expand Down
11 changes: 2 additions & 9 deletions app/src/main/res/layout/addtransfer_file.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,17 @@
android:layout_marginEnd="16dp"
/>

<LinearLayout
<com.stevenschoen.putionew.ButtonBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/addtransfer_file_destination_holder"
android:layout_marginTop="8dp"
android:gravity="end"
android:orientation="horizontal"
android:paddingEnd="12dp"
android:paddingBottom="8dp"
>

<Button
android:id="@+id/addtransfer_file_cancel"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/cancel"
/>

Expand All @@ -90,8 +84,7 @@
style="@style/Widget.Putio.Button.Unelevated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="48dp"
android:text="@string/add"
/>
</LinearLayout>
</com.stevenschoen.putionew.ButtonBarLayout>
</RelativeLayout>
11 changes: 2 additions & 9 deletions app/src/main/res/layout/addtransfer_link.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,17 @@
android:layout_marginEnd="16dp"
/>

<LinearLayout
<com.stevenschoen.putionew.ButtonBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/addtransfer_link_destination_holder"
android:layout_marginTop="8dp"
android:gravity="end"
android:orientation="horizontal"
android:paddingEnd="12dp"
android:paddingBottom="8dp"
>

<Button
android:id="@+id/addtransfer_link_cancel"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/cancel"
/>

Expand All @@ -82,8 +76,7 @@
style="@style/Widget.Putio.Button.Unelevated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="48dp"
android:text="@string/add"
/>
</LinearLayout>
</com.stevenschoen.putionew.ButtonBarLayout>
</RelativeLayout>
9 changes: 2 additions & 7 deletions app/src/main/res/layout/destination_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,18 @@
android:layout_weight="1"
/>

<LinearLayout
<com.stevenschoen.putionew.ButtonBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorSurface"
android:elevation="@dimen/appBarElevation"
android:gravity="end"
android:orientation="horizontal"
android:paddingVertical="8dp"
android:paddingEnd="16dp"
>

<Button
android:id="@+id/destination_cancel"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/cancel"
/>

Expand All @@ -51,5 +46,5 @@
android:layout_height="wrap_content"
android:text="@string/choose_folder"
/>
</LinearLayout>
</com.stevenschoen.putionew.ButtonBarLayout>
</LinearLayout>
47 changes: 0 additions & 47 deletions app/src/main/res/layout/dialog_removetransfer.xml

This file was deleted.

Loading

0 comments on commit 17d8094

Please sign in to comment.