package com.jsvoid.solarsystem.activities;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsvoid.solarsystem.R;
import com.jsvoid.solarsystem.SolarSystemApplication;
import com.jsvoid.solarsystem.models.CelestialObject;
import com.squareup.picasso.Picasso;

import retrofit2.Retrofit;

public class DetailsActivity extends AppCompatActivity {

    public static final String OBJECT = "object";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_details);

        Retrofit retrofit = ((SolarSystemApplication) getApplication()).getRetrofit();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        ImageView mImage = (ImageView) findViewById(R.id.object_image);
        TextView mDetail = (TextView) findViewById(R.id.details);

        CelestialObject mObject = null;
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mObject = bundle.getParcelable(OBJECT);
        }

        if (mObject != null) {
            if (toolbar != null) {
                toolbar.setTitle(mObject.name);
                setSupportActionBar(toolbar);
            }
            if (mDetail != null) {
                mDetail.setText(mObject.getDetailsHtml());
            }
            if (mImage != null) {
                Picasso.with(this)
                        .load(retrofit.baseUrl().toString() + mObject.imageUrl)
                        .placeholder(R.drawable.image_preview)
                        .into(mImage);
            }
        }
    }
}

@@ -37,16 +37,71 @@
 ities;%0A%0A
+import android.content.Intent;%0Aimport android.net.Uri;%0A
 import a
@@ -118,16 +118,16 @@
 Bundle;%0A
-
 import a
@@ -209,16 +209,238 @@
 oolbar;%0A
+import android.text.SpannableStringBuilder;%0Aimport android.text.Spanned;%0Aimport android.text.method.LinkMovementMethod;%0Aimport android.text.style.ClickableSpan;%0Aimport android.text.style.URLSpan;%0Aimport android.view.View;%0A
 import a
@@ -811,16 +811,91 @@
 object%22;
+%0A    public static final String BASE_WIKI_URL = %22https://en.wikipedia.org%22;
 %0A%0A    @O
@@ -1771,16 +1771,35 @@
 setText(
+convertToClickable(
 mObject.
@@ -1815,16 +1815,94 @@
 sHtml())
+);%0A                mDetail.setMovementMethod(LinkMovementMethod.getInstance())
 ;%0A      
@@ -2166,32 +2166,32 @@
 ;%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -2189,11 +2189,985 @@
  %7D%0A    %7D
+%0A%0A    private SpannableStringBuilder convertToClickable(Spanned html) %7B%0A        SpannableStringBuilder builder = new SpannableStringBuilder(html);%0A        URLSpan%5B%5D urls = builder.getSpans(0, html.length(), URLSpan.class);%0A        for (URLSpan span : urls) %7B%0A            makeLinkClickable(builder, span);%0A        %7D%0A        return builder;%0A    %7D%0A%0A    private void makeLinkClickable(SpannableStringBuilder builder, final URLSpan span) %7B%0A        int start = builder.getSpanStart(span);%0A        int end = builder.getSpanEnd(span);%0A        int flags = builder.getSpanFlags(span);%0A        ClickableSpan clickable = new ClickableSpan() %7B%0A            public void onClick(View view) %7B%0A                String url = BASE_WIKI_URL + span.getURL();%0A                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));%0A                startActivity(intent);%0A            %7D%0A        %7D;%0A        builder.setSpan(clickable, start, end, flags);%0A        builder.removeSpan(span);%0A    %7D
 %0A%7D%0A

make links clickable