Skip to content

Commit

Permalink
Add additional fields for uploading content (#39)
Browse files Browse the repository at this point in the history
* Add additional fields for uploading content

* Update document

* Update document

* Remove example

* Remove example

* Remove real data

* Fix a bug

* Replace it with fake data

* Replace it with fake data

* Replace it with fake data

Co-authored-by: sungjun <[email protected]>
  • Loading branch information
SungjunApp and sungjun authored Apr 8, 2021
1 parent ae1fbeb commit 0924cdd
Show file tree
Hide file tree
Showing 22 changed files with 553 additions and 176 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ dependencies {
*/

testImplementation 'org.json:json:20200518'
testImplementation "com.fasterxml.jackson.core:jackson-databind:2.10.3"
testImplementation "com.fasterxml.jackson.core:jackson-databind:$jackson"
//testImplementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8"
testImplementation "com.squareup.okhttp3:mockwebserver:$okhttp3"

androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
androidTestImplementation "androidx.test.ext:junit-ktx:" + rootProject.extJUnitVersion
androidTestImplementation "com.fasterxml.jackson.core:jackson-databind:2.10.3"
androidTestImplementation "com.fasterxml.jackson.core:jackson-databind:$jackson"
androidTestImplementation 'androidx.test.espresso:espresso-contrib:' + rootProject.espressoVersion
androidTestImplementation "com.jakewharton.espresso:okhttp3-idling-resource:$jakeWhartonOkhttpOdlingResource"

Expand Down
152 changes: 152 additions & 0 deletions app/src/main/java/com/pixlee/pixleeandroidsdk/ui/BaseFragment.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package com.pixlee.pixleeandroidsdk.ui;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.pixlee.pixleeandroidsdk.MainActivity;
import com.pixlee.pixleeandroidsdk.R;

/**
* This helps Fragments and Dialogs to be loaded
Expand Down Expand Up @@ -63,4 +77,142 @@ public void onClick(DialogInterface dialog, int which) {
b.show();

}


void callMediaPicker() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/* video/*");
startActivityForResult(intent, REQ_MEDIA_PICKER);
}

private final int REQ_MEDIA_PICKER = 1314;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
if (requestCode == REQ_MEDIA_PICKER) {
extractImage(data);
}
}
}

// override this method in your fragment
public void uploadFile(String filePath) {

}

void extractImage(Intent data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.MIME_TYPE};
if (selectedImage != null) {
Cursor cursor = null;
try {
cursor = getContext().getContentResolver().query(
selectedImage,
filePathColumn, null, null, null
);
if (cursor != null) {
cursor.moveToFirst();

String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
uploadFile(filePath);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
}
}
}

final String permission = Manifest.permission.READ_EXTERNAL_STORAGE;

public void setupExternalStoragePermission() {
if (ContextCompat.checkSelfPermission(getActivity(), permission) == PackageManager.PERMISSION_GRANTED) {
callMediaPicker();
} else {
ActivityCompat.requestPermissions(
getActivity(),
new String[]{permission},
reqStorage
);
}

}

/**
* Put any random number for reqStorage in ActivityCompat.requestPermissions
* Example:
* ActivityCompat.requestPermissions(
* getActivity(),
* new String[]{permission},
* reqStorage
* );
* <p>
* Try to filter requestCode with the same number inside onRequestPermissionsResult(int requestCode).
* This will aloow you to receive the result about permission
*/
private final int reqStorage = 1729;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == reqStorage) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callMediaPicker();
} else {
boolean showRationale = ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissions[0]);
if (!showRationale) {
createDialogForStoragePermission();
}

}
}
}

private void createDialogForStoragePermission() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(R.string.storage_permission_for_uploading);
builder.setPositiveButton(R.string.button_setting, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(
new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData(Uri.parse("package:" + getActivity().getPackageName()))
);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});
builder.show();
}


AlertDialog loadingDialog;

void makeLoading(Boolean show) {
if (show) {
if (loadingDialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_progress, null);
builder.setView(dialogLayout);
builder.setCancelable(false);
loadingDialog = builder.show();
} else {
loadingDialog.show();
}

} else {
if (loadingDialog != null && loadingDialog.isShowing())
loadingDialog.dismiss();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pixlee.pixleesdk.util.Event
import com.pixlee.pixleesdk.client.PXLKtxAlbum
import com.pixlee.pixleesdk.client.PXLKtxBaseAlbum
import com.pixlee.pixleesdk.data.PXLPhoto
import com.pixlee.pixleesdk.enums.PXLPhotoSize
import com.pixlee.pixleesdk.ui.viewholder.PhotoWithImageScaleType
import com.pixlee.pixleesdk.ui.widgets.PXLPhotoView
import com.pixlee.pixleesdk.ui.widgets.TextPadding
import com.pixlee.pixleesdk.ui.widgets.TextViewStyle
import com.pixlee.pixleesdk.util.Event
import com.pixlee.pixleesdk.util.px
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -75,8 +74,7 @@ open class BaseViewModel(val pxlKtxAlbum: PXLKtxAlbum) : ViewModel() {
val newList = ArrayList<PhotoWithImageScaleType>()
if (it.photos.isNotEmpty()) {
it.photos.forEach {
Log.e("pxlvideo", "pxlvideo.url: ${it.videoUrl.toString()}")
Log.e("pxlvideo", "pxlvideo.big: ${it.getUrlForSize(PXLPhotoSize.BIG)}")
Log.e("pxlvideo", "pxlvideo.uploaderAdditionalFields: ${it.uploaderAdditionalFields}")
newList.add(PhotoWithImageScaleType(pxlPhoto = it,
configuration = customizedConfiguration.copy().apply {
mainTextViewStyle = TextViewStyle().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ import com.pixlee.pixleesdk.util.px
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.*
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.drawerLayout
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.fabFilter
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.fabUpload
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.lottieView
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.tvDebugText
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.v_body
import kotlinx.android.synthetic.main.module_search.*
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
* This shows how you can load photos of Pixlee using PXLAlbum.java
Expand Down Expand Up @@ -109,6 +113,14 @@ class KtxGalleryGridFragment : BaseFragment(), LifecycleObserver {
}

fun addViewModelListeners() {
viewModel.toastMessage.observe(viewLifecycleOwner, EventObserver{
showToast(it)
})

viewModel.uploadStatus.observe(viewLifecycleOwner, Observer{
fabUpload.isEnabled = !it
})

viewModel.loading.observe(viewLifecycleOwner, Observer {
lottieView.visibility = if (it) View.VISIBLE else View.GONE
})
Expand Down Expand Up @@ -225,6 +237,7 @@ class KtxGalleryGridFragment : BaseFragment(), LifecycleObserver {

fun initFilterClickListeners() {
// set filter buttons
fabUpload.setOnClickListener { setupExternalStoragePermission() }
fabFilter.setOnClickListener { drawerLayout.openDrawer(GravityCompat.END) }
btnCloseFilter.setOnClickListener { drawerLayout.closeDrawer(GravityCompat.END) }
btnApply.setOnClickListener {
Expand All @@ -233,6 +246,30 @@ class KtxGalleryGridFragment : BaseFragment(), LifecycleObserver {
}
}

override fun uploadFile(filePath: String) {
val json = JSONObject()
try {
json.put("name", "Sample Name")
json.put("age", 24)
val arr = JSONArray()
arr.put(10)
arr.put(20)
arr.put(35)
json.put("points", arr)
} catch (e: JSONException) {
e.printStackTrace()
}
viewModel.uploadPhoto(
filePath,
"uploaded from SDK-" + System.currentTimeMillis() + " using a file",
"[email protected]",
"replace this with your user name",
true,
null,
null,
json)
}

/***
* Initializes the PXLClient and creates the PXLAlbum
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import com.pixlee.pixleeandroidsdk.ui.BaseFragment
import com.pixlee.pixleeandroidsdk.ui.BaseViewModel
import com.pixlee.pixleeandroidsdk.ui.widgets.PXLPhotoViewFragment
import com.pixlee.pixleeandroidsdk.ui.widgets.ViewerActivity
import com.pixlee.pixleesdk.client.PXLBaseAlbum.RequestHandlers
import com.pixlee.pixleesdk.client.PXLClient
import com.pixlee.pixleesdk.client.PXLKtxAlbum
import com.pixlee.pixleesdk.client.PXLKtxBaseAlbum
import com.pixlee.pixleesdk.data.MediaResult
import com.pixlee.pixleesdk.data.PXLAlbumFilterOptions
import com.pixlee.pixleesdk.data.PXLAlbumSortOptions
import com.pixlee.pixleesdk.enums.*
Expand All @@ -38,6 +40,9 @@ import com.pixlee.pixleesdk.util.px
import kotlinx.android.synthetic.main.fragment_ktx_gallery_list.*
import kotlinx.android.synthetic.main.module_search.*
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject


/**
Expand Down Expand Up @@ -107,6 +112,13 @@ class KtxGalleryListFragment : BaseFragment(), LifecycleObserver {
}

fun addViewModelListeners() {
viewModel.toastMessage.observe(viewLifecycleOwner, EventObserver {
showToast(it)
})

viewModel.uploadStatus.observe(viewLifecycleOwner, Observer {
fabUpload.isEnabled = !it
})

viewModel.loading.observe(viewLifecycleOwner, Observer {
lottieView.visibility = if (it) View.VISIBLE else View.GONE
Expand Down Expand Up @@ -206,6 +218,7 @@ class KtxGalleryListFragment : BaseFragment(), LifecycleObserver {

fun initFilterClickListeners() {
// set filter buttons
fabUpload.setOnClickListener { setupExternalStoragePermission() }
fabFilter.setOnClickListener { drawerLayout.openDrawer(GravityCompat.END) }
btnCloseFilter.setOnClickListener { drawerLayout.closeDrawer(GravityCompat.END) }
btnApply.setOnClickListener {
Expand All @@ -214,6 +227,26 @@ class KtxGalleryListFragment : BaseFragment(), LifecycleObserver {
}
}

override fun uploadFile(filePath: String) {
viewModel.uploadPhoto(
localMediaPath = filePath,
title = "uploaded from SDK-" + System.currentTimeMillis() + " using a file",
email = "[email protected]",
username = "replace this with your user name",
approved = true,
productSKUs = listOf("productA", "productB"), // Optional
categoryNames = listOf("Clothing", "Shoes"), // Optional
connectedUser = JSONObject().apply { // Optional
put("name", "sample name")
put("age", 24)
put("points", JSONArray().apply {
put(10)
put(20)
put(35)
})
})
}

/***
* Initializes the PXLClient and creates the PXLAlbum
*/
Expand Down
Loading

0 comments on commit 0924cdd

Please sign in to comment.