Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Network source can now run in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
m7mdra committed Sep 2, 2018
1 parent f4a0800 commit c9b953f
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/src/main/java/m7mdra/com/htmlrecyclerview/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class Data {
"<section>\n" +
"<h2>Solidity</h2>\n" +
"<p>Solidity is a programming language for writing smart contracts which run on Ethereum Virtual Machine on Blockchain. It is a contract-oriented, high-level language.</p>\n" +
"<img src=\"../img/ethereum-logo.svg\" width=\"380\">\n" +
"<img src=\"https://res.cloudinary.com/teepublic/image/private/s--68-qLqlp--/t_Preview/b_rgb:191919,c_limit,f_jpg,h_630,q_90,w_630/v1509381144/production/designs/2011603_1.jpg\" width=\"380\">\n" +
"</section>\n" +
"\n" +
"<section>\n" +
Expand Down Expand Up @@ -116,4 +116,5 @@ public class Data {
"</div>\n" +
"</div>\n";


}
62 changes: 30 additions & 32 deletions app/src/main/java/m7mdra/com/htmlrecyclerview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,50 @@ package m7mdra.com.htmlrecyclerview

import android.annotation.SuppressLint
import android.app.FragmentTransaction
import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.support.v4.view.accessibility.AccessibilityRecordCompat.setSource
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import m7mdra.com.htmlrecycler.*
import m7mdra.com.htmlrecycler.adapter.DefaultElementsAdapter
import m7mdra.com.htmlrecycler.elements.ImageElement
import m7mdra.com.htmlrecycler.source.NetworkSource
import m7mdra.com.htmlrecycler.source.Source
import m7mdra.com.htmlrecycler.source.StringSource
import org.jsoup.nodes.Document


class MainActivity : AppCompatActivity() {

@SuppressLint("StaticFieldLeak")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
object : AsyncTask<Unit, Unit, Document>() {
override fun doInBackground(vararg params: Unit?): Document {
return StringSource(Data.data2).get()

}

override fun onPostExecute(result: Document) {
super.onPostExecute(result)
HtmlRecycler.Builder(this@MainActivity)
.setSource(object:Source{
override fun get(): Document {
return result
}
})
.setAdapter(DefaultElementsAdapter(this@MainActivity) { element, i, view ->
if (element is ImageElement)
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragment_layout, ImageFragment.newInstance(element.ImageUrl))
setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
commit()
addToBackStack("")
}
}).setRecyclerView(recyclerView)
.build()
}

}.execute().get()
val networkSource = NetworkSource("https://gist.githubusercontent.com/m7mdra/f22c62bc6941e08064b4fbceb4832a90/raw/ea8574d986635cf214541f1f5702ef37cc731aaf/article.html")

HtmlRecycler.Builder(this@MainActivity)
.setSource(networkSource)
.setAdapter(DefaultElementsAdapter(this@MainActivity) { element, i, view ->

Toast.makeText(this, "you clicked ${element::class.java.simpleName} element", Toast.LENGTH_SHORT).show()
if (element is ImageElement)
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragment_layout, ImageFragment.newInstance(element.ImageUrl))
setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
commit()
addToBackStack("")
}
}).setRecyclerView(recyclerView)
.setLoadingCallback(object : HtmlRecycler.LoadCallback {
override fun onLoadingStart() {
progressBar.visibility = View.VISIBLE
}

override fun onLoaded(document: Document?) {
progressBar.visibility = View.GONE

}
})
.build()

}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package m7mdra.com.htmlrecycler;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.jsoup.nodes.Document;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -17,16 +20,17 @@ public class HtmlRecycler {
private RecyclerView recyclerView;
private ElementsAdapter adapter;
private Source source;


private LoadCallback mLoadCallback;

private HtmlRecycler(Builder builder) {
recyclerView = builder.recyclerView;
adapter = builder.adapter;
source = builder.source;
mLoadCallback = builder.loadingCallback;
}

public static final class Builder {
public LoadCallback loadingCallback;
private RecyclerView recyclerView;
private ElementsAdapter adapter;
private Source source;
Expand All @@ -42,6 +46,11 @@ public Builder setRecyclerView(RecyclerView val) {
return this;
}

public Builder setLoadingCallback(LoadCallback loadingCallback) {
this.loadingCallback = loadingCallback;
return this;
}

public Builder setAdapter(ElementsAdapter val) {
adapter = val;
return this;
Expand All @@ -52,15 +61,69 @@ public Builder setSource(Source val) {
return this;
}

private static final String TAG = "Builder";

public HtmlRecycler build() {
List<Element> elements = new ArrayList<>();
ElementIdentifier.extractData(elements, source.get().body().children());
recyclerView.setAdapter(adapter);
adapter.addElements(elements);
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
final DocumentParseTask documentParseTask = new DocumentParseTask(source, new LoadCallback() {
@Override
public void onLoaded(Document document) {
if (loadingCallback != null)
loadingCallback.onLoaded(document);
List<Element> elements = new ArrayList<>();
ElementIdentifier.extractData(elements, document.body().children());
adapter.addElements(elements);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
}

@Override
public void onLoadingStart() {
if (loadingCallback != null)
loadingCallback.onLoadingStart();
}

});
documentParseTask.execute();
return new HtmlRecycler(this);
}
}

static class DocumentParseTask extends AsyncTask<Void, Void, Document> {
private LoadCallback mLoadCallback;
private Source source;

public DocumentParseTask(Source source, LoadCallback loadCallback) {
mLoadCallback = loadCallback;
this.source = source;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
mLoadCallback.onLoadingStart();
}

@Override
protected Document doInBackground(Void... voids) {
return source.get();
}

@Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
mLoadCallback.onLoaded(document);
}


}

public interface LoadCallback {
void onLoaded(Document document);

void onLoadingStart();


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package m7mdra.com.htmlrecycler;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ResizableImageView extends android.support.v7.widget.AppCompatImageView {

public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();

if (d != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class DefaultElementsAdapter(private val context: Context, private val onClick:
Picasso.get().load((element as ImageElement).ImageUrl)
.placeholder(ColorDrawable(Color.parseColor("#EFF0EE")))
.error(ColorDrawable(Color.parseColor("#EFF0EE")))
.fit()
.into(holder.image)
}
is BlockQuoteViewHolder -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ElementIdentifier(private val element: Element) {
"dl" -> ElementType.DescriptionList
"div" -> ElementType.Div
"section" -> ElementType.Section
// "table" -> return ElementType.Table
else -> ElementType.Unknown
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import java.util.logging.Handler
@WorkerThread
class NetworkSource(private val url: String) : Source {
override fun get(): Document {
if (Looper.getMainLooper().thread== Thread.currentThread())
throw Exception("'NetworkSource' should be called of Main Thread (UI Thread)")
return Jsoup.connect(url).get()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import m7mdra.com.htmlrecycler.R
import m7mdra.com.htmlrecycler.ResizableImageView

class ImageViewHolder(val view: View) :RecyclerView.ViewHolder(view) {
val image=view.findViewById<ImageView>(R.id.image)
val image=view.findViewById<ResizableImageView>(R.id.image)
}
3 changes: 2 additions & 1 deletion htmlrecycler/src/main/res/layout/row_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
android:background="@android:color/darker_gray"
android:orientation="vertical">

<ImageView
<m7mdra.com.htmlrecycler.ResizableImageView

android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"/>
Expand Down

0 comments on commit c9b953f

Please sign in to comment.