Browse Source

Uploads fixes

pull/27/head
Apostolos Fanakis 7 years ago
parent
commit
62dd31635e
  1. 46
      app/src/main/java/gr/thmmy/mthmmy/activities/UploadActivity.java
  2. 109
      app/src/main/java/gr/thmmy/mthmmy/utils/AppCompatSpinnerWithoutDefault.java
  3. 5
      app/src/main/res/layout/activity_upload.xml
  4. 1
      app/src/main/res/values/strings.xml

46
app/src/main/java/gr/thmmy/mthmmy/activities/UploadActivity.java

@ -21,12 +21,14 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URLEncoder;
import java.util.ArrayList;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.model.UploadCategory;
import gr.thmmy.mthmmy.utils.AppCompatSpinnerWithoutDefault;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.utils.parsing.ParseTask;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -43,14 +45,14 @@ public class UploadActivity extends BaseActivity {
private static ArrayList<UploadCategory> uploadRootCategories = new ArrayList<>();
//private String currentUploadCategory = "";
private ParseUploadPageTask parseUploadPageTask;
private String categorySelected = "0";
private String categorySelected = "-1";
private String uploaderProfileIndex = "1";
private Uri fileUri;
//UI elements
private MaterialProgressBar progressBar;
private LinearLayout categoriesSpinners;
private AppCompatSpinner rootCategorySpinner;
private AppCompatSpinnerWithoutDefault rootCategorySpinner;
private EditText uploadTitle;
private EditText uploadDescription;
//private static AppCompatButton titleDescriptionBuilderButton;
@ -118,14 +120,33 @@ public class UploadActivity extends BaseActivity {
findViewById(R.id.upload_upload_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fileUri != null) {
String uploadTitleText = uploadTitle.getText().toString();
String uploadDescriptionText = uploadDescription.getText().toString();
if (uploadTitleText.equals("")) {
uploadTitle.setError("Required");
}
if (uploadDescriptionText.equals("")) {
uploadDescription.setError("Required");
}
/*if (categorySelected.equals("-1")){
//TODO set error
//rootCategorySpinner
}*/
if (categorySelected.equals("-1") || uploadTitleText.equals("") ||
fileUri == null || uploadDescriptionText.equals("")) {
return;
}
try {
String uploadId = new MultipartUploadRequest(v.getContext(), uploadIndexUrl)
.addParameter("tp-dluploadtitle", uploadTitle.getText().toString())
.setUtf8Charset()
.addParameter("tp-dluploadtitle", uploadTitleText)
.addParameter("tp-dluploadcat", categorySelected)
.addParameter("tp-dluploadtext", uploadDescription.getText().toString())
.addParameter("tp-dluploadtext", uploadDescriptionText)
.addFileToUpload(fileUri.toString(), "tp-dluploadfile")
.addParameter("tp_dluploadicon", "blank.gif")
.addParameter("tp_dluploadicon", "blank.gif") //TODO auto-select this
.addParameter("tp-uploaduser", uploaderProfileIndex)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2).startUpload();
@ -133,7 +154,6 @@ public class UploadActivity extends BaseActivity {
Timber.e(exception, "AndroidUploadService: %s", exception.getMessage());
}
}
}
});
@ -189,7 +209,6 @@ public class UploadActivity extends BaseActivity {
private class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
private ArrayList<UploadCategory> parentCategories, childCategories;
private boolean initialized = false;
private CustomOnItemSelectedListener() {
//Disable default constructor
@ -201,14 +220,8 @@ public class UploadActivity extends BaseActivity {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Avoids call on initialization
if (!initialized) {
initialized = true;
return;
}
//Removes old, unneeded sub categories spinner(s)
int viewIndex = categoriesSpinners.indexOfChild((AppCompatSpinner) view.getParent());
int viewIndex = categoriesSpinners.indexOfChild((AppCompatSpinnerWithoutDefault) view.getParent());
if (viewIndex + 1 != categoriesSpinners.getChildCount()) { //Makes sure this is not the last child
categoriesSpinners.removeViews(viewIndex + 1, categoriesSpinners.getChildCount() - viewIndex - 1);
@ -229,7 +242,8 @@ public class UploadActivity extends BaseActivity {
android.R.layout.simple_spinner_dropdown_item, tmpSpinnerArray);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
AppCompatSpinner subSpinner = new AppCompatSpinner(categoriesSpinners.getContext(), Spinner.MODE_DROPDOWN);
AppCompatSpinnerWithoutDefault subSpinner = new AppCompatSpinnerWithoutDefault(categoriesSpinners.getContext());
subSpinner.setPromptId(R.string.upload_spinners_prompt);
subSpinner.setPopupBackgroundResource(R.color.primary);
subSpinner.setAdapter(spinnerArrayAdapter);
subSpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(childCategories));

109
app/src/main/java/gr/thmmy/mthmmy/utils/AppCompatSpinnerWithoutDefault.java

@ -0,0 +1,109 @@
package gr.thmmy.mthmmy.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.AppCompatSpinner;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class AppCompatSpinnerWithoutDefault extends AppCompatSpinner {
public AppCompatSpinnerWithoutDefault(Context context) {
super(context);
}
public AppCompatSpinnerWithoutDefault(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AppCompatSpinnerWithoutDefault(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
@SuppressLint("PrivateApi")
public void setAdapter(SpinnerAdapter orig) {
final SpinnerAdapter adapter = newProxy(orig);
super.setAdapter(adapter);
try {
final Method m = AdapterView.class.getDeclaredMethod(
"setNextSelectedPositionInt", int.class);
m.setAccessible(true);
m.invoke(this, -1);
final Method n = AdapterView.class.getDeclaredMethod(
"setSelectedPositionInt", int.class);
n.setAccessible(true);
n.invoke(this, -1);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
new Class[]{SpinnerAdapter.class},
new SpinnerAdapterProxy(obj));
}
/**
* Intercepts getView() to display the prompt if position < 0
*/
protected class SpinnerAdapterProxy implements InvocationHandler {
SpinnerAdapter obj;
Method getView;
SpinnerAdapterProxy(SpinnerAdapter obj) {
this.obj = obj;
try {
this.getView = SpinnerAdapter.class.getMethod(
"getView", int.class, View.class, ViewGroup.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
try {
return m.equals(getView) &&
(Integer) (args[0]) < 0 ?
getView((Integer) args[0], (View) args[1], (ViewGroup) args[2]) :
m.invoke(obj, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
View getView(int position, View convertView, ViewGroup parent) {
if (position < 0) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (inflater != null) {
final TextView v =
(TextView) inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
v.setText(getPrompt());
return v;
}
return null;
}
return obj.getView(position, convertView, parent);
}
}
}

5
app/src/main/res/layout/activity_upload.xml

@ -58,13 +58,14 @@
android:background="@color/background"
android:orientation="vertical">
<android.support.v7.widget.AppCompatSpinner
<gr.thmmy.mthmmy.utils.AppCompatSpinnerWithoutDefault
android:id="@+id/upload_spinner_category_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:popupBackground="@color/primary" />
android:popupBackground="@color/primary"
android:prompt="@string/upload_spinners_prompt" />
</LinearLayout>
<TextView

1
app/src/main/res/values/strings.xml

@ -114,6 +114,7 @@
<string name="upload_description_hint">Description</string>
<string name="upload_select_file">Select file to upload</string>
<string name="upload_upload_button">Upload</string>
<string name="upload_spinners_prompt">Select one</string>
<!--Settings Activity-->
<string name="action_settings">Settings</string>

Loading…
Cancel
Save