From 8322d7cf510b22ee20d310013ec71dd4f39442bb Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sat, 20 Apr 2019 19:56:15 +0200 Subject: Added Image Pager --- .../blatech/blaupload3/util/ImagePagerAdapter.kt | 55 ++++++++++++++++++++++ .../uk/co/blatech/blaupload3/util/JSONLoader.kt | 24 ++++++++++ .../uk/co/blatech/blaupload3/view/PagerFragment.kt | 28 +++++++++++ app/src/main/res/layout/pager_fragment.xml | 20 ++++++++ app/src/main/res/layout/pager_item.xml | 18 +++++++ app/src/main/res/menu/activity_main_drawer.xml | 4 +- app/src/main/res/navigation/nav_graph.xml | 12 ++++- 7 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/uk/co/blatech/blaupload3/util/ImagePagerAdapter.kt create mode 100644 app/src/main/java/uk/co/blatech/blaupload3/view/PagerFragment.kt create mode 100644 app/src/main/res/layout/pager_fragment.xml create mode 100644 app/src/main/res/layout/pager_item.xml (limited to 'app/src') diff --git a/app/src/main/java/uk/co/blatech/blaupload3/util/ImagePagerAdapter.kt b/app/src/main/java/uk/co/blatech/blaupload3/util/ImagePagerAdapter.kt new file mode 100644 index 0000000..628f878 --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload3/util/ImagePagerAdapter.kt @@ -0,0 +1,55 @@ +package uk.co.blatech.blaupload3.util + +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.pager_item.view.* +import uk.co.blatech.blaupload3.R +import uk.co.blatech.blaupload3.model.File + +class ImagePagerAdapter(private val fileList : ArrayList) : RecyclerView.Adapter() { + + class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + var imageLoader = AppController.instance?.imageLoader + + fun bind(file: File) { + itemView.pager_image.setImageUrl(file.url.toASCIIString(), imageLoader) + itemView.pager_title.text = file.filename + } + } + + fun updateFileList(newFileList : ArrayList) { + Log.i("aaaaa", "bbbbb") + for (file in newFileList) { + Log.i("file", file.toString()) + if (file.type == "image2") { + fileList.add(file) + } + } + notifyDataSetChanged() + + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImagePagerAdapter.ViewHolder { + val view = LayoutInflater.from(parent.context).inflate(R.layout.pager_item, parent,false) + return ViewHolder(view) } + + override fun getItemCount(): Int { + return fileList.size + } + + override fun getItemId(position: Int): Long { + return position.toLong() + } + + override fun onBindViewHolder(holder: ImagePagerAdapter.ViewHolder, position: Int) { + fileList?.get(position)?.let { file -> + if (file.type == "image2") { + holder.bind(file) + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/uk/co/blatech/blaupload3/util/JSONLoader.kt b/app/src/main/java/uk/co/blatech/blaupload3/util/JSONLoader.kt index 2560849..51ec786 100644 --- a/app/src/main/java/uk/co/blatech/blaupload3/util/JSONLoader.kt +++ b/app/src/main/java/uk/co/blatech/blaupload3/util/JSONLoader.kt @@ -63,6 +63,30 @@ class JSONLoader { queue.add(jsonRequest) } + + fun loadJsonToAdapter2(context: Context?, adapter: ImagePagerAdapter) { + val queue = Volley.newRequestQueue(context) + val url = "http://wupload.of.je/?format=json" + var fileList = ArrayList() + val jsonRequest = JsonArrayRequest( + Request.Method.GET, + url, + null, + Response.Listener { response -> + + for (i in 0 until response.length()) { + var jsonObj = response.getJSONObject(i) + var upload: File = File(filename =jsonObj.getString("filename"), type = jsonObj.getString("type"), modified = jsonObj.getLong("modified"), size = jsonObj.getLong("size")) + fileList.add(upload) + + + } + adapter.updateFileList(fileList) + }, + Response.ErrorListener { Log.e("json error", "narp")}) + queue.add(jsonRequest) + + } } diff --git a/app/src/main/java/uk/co/blatech/blaupload3/view/PagerFragment.kt b/app/src/main/java/uk/co/blatech/blaupload3/view/PagerFragment.kt new file mode 100644 index 0000000..bfed17c --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload3/view/PagerFragment.kt @@ -0,0 +1,28 @@ +package uk.co.blatech.blaupload3.view + +import android.os.Bundle +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import kotlinx.android.synthetic.main.pager_fragment.* +import uk.co.blatech.blaupload3.R +import uk.co.blatech.blaupload3.model.File +import uk.co.blatech.blaupload3.util.ImagePagerAdapter +import uk.co.blatech.blaupload3.util.JSONLoader + +class PagerFragment : Fragment() { + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + super.onCreate(savedInstanceState) + return inflater.inflate(R.layout.pager_fragment, container, false) + + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val adapter = ImagePagerAdapter(ArrayList()) + image_pager.adapter = adapter + JSONLoader.loadJsonToAdapter2(activity?.applicationContext, adapter) + Log.i("onCreateView", "bar") + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/pager_fragment.xml b/app/src/main/res/layout/pager_fragment.xml new file mode 100644 index 0000000..756b29a --- /dev/null +++ b/app/src/main/res/layout/pager_fragment.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/pager_item.xml b/app/src/main/res/layout/pager_item.xml new file mode 100644 index 0000000..d948c64 --- /dev/null +++ b/app/src/main/res/layout/pager_item.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml index 5c09517..c2ebab8 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -13,9 +13,9 @@ android:icon="@drawable/ic_menu_gallery" android:title="Gallery"/> + android:title="Pager"/> diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index 77eca0e..e449c97 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -8,7 +8,7 @@ + + + + \ No newline at end of file -- cgit v1.2.3