Much progress, working on new release using the API

This commit is contained in:
Jan-Lk Else 2016-12-20 21:28:45 +01:00
parent 51842bddd0
commit 843ccc020c
7 changed files with 92 additions and 44 deletions

View File

@ -3,10 +3,10 @@ apply plugin: 'kotlin-android'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "telegra.ph"
minSdkVersion 13
minSdkVersion 15
targetSdkVersion 25
versionCode 4
versionName "0.4 beta"
@ -27,8 +27,11 @@ android {
}
dependencies {
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.android.support:support-v13:25.1.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.github.delight-im:Android-AdvancedWebView:v3.0.0'
compile 'com.afollestad.material-dialogs:core:0.9.1.0'
compile 'com.afollestad:bridge:3.3.0'
}

View File

@ -0,0 +1,69 @@
package telegra.ph
import com.afollestad.bridge.Bridge
import org.json.JSONArray
import org.json.JSONObject
class Api {
private val ApiBase = "https://api.telegra.ph/"
fun getPage(id: String?, callback: (page: Page?) -> Unit) {
Bridge.get("${ApiBase}getPage/$id?return_content=true").asJsonObject { response, jsonObject, bridgeException ->
callback(jsonObject?.parsePage())
}
}
private fun JSONObject.parsePage(): Page? {
val result: Page = Page()
if (optBoolean("ok", false)) {
optJSONObject("result").let {
result.path = it.optString("path", "")
result.url = it.optString("url", "")
result.title = it.optString("title", "")
result.description = it.optString("description", "")
result.author_name = it.optString("author_name", "")
result.author_url = it.optString("author_url", "")
result.image_url = it.optString("image_url", "")
it.optJSONArray("content")?.parseContent(result)
result.views = it.optInt("views", 0)
result.can_edit = it.optBoolean("can_edit", false)
}
}
return result
}
private fun JSONArray.parseContent(result: Page) {
for (child in this) {
if (child is String) result.content += child
if (child is JSONObject) {
result.content += "<${child.optString("tag", "")}"
child.optJSONObject("attrs")?.let {
for (key in it.keys()) {
result.content += " $key=\"${it.optString(key, "")}\""
}
for (i in 0 until length()) {
result.content += "${it.names()}"
}
}
result.content += ">"
child.optJSONArray("children").parseContent(result)
result.content += "</${child.optString("tag", "")}>"
}
}
}
}
class Page(
var path: String = "",
var url: String = "",
var title: String = "",
var description: String = "",
var author_name: String = "",
var author_url: String = "",
var image_url: String = "",
var content: String = "",
var views: Int = 0,
var can_edit: Boolean = false
)

View File

@ -0,0 +1,5 @@
package telegra.ph
import org.json.JSONArray
operator fun JSONArray.iterator(): Iterator<Any> = (0 until length()).asSequence().map { opt(it) }.iterator()

View File

@ -2,10 +2,8 @@ package telegra.ph
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import com.afollestad.materialdialogs.MaterialDialog
@ -22,7 +20,6 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
setContentView(R.layout.activity_main)
webView?.setListener(this, this)
webView?.apply {
setMixedContentAllowed(true)
setCookiesEnabled(true)
@ -30,13 +27,16 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
addPermittedHostname("telegra.ph")
}
// Check if app is opened to show special page
var urlToLoad = TELEGRAPH
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph"))
urlToLoad = intent.dataString
// Load URL
webView?.loadUrl(urlToLoad)
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) {
Api().getPage(intent.dataString.split("/").last()) { page ->
page?.let {
val html = "<h1>${it.title}</h1>${it.content}"
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
}
}
} else {
webView?.loadUrl(TELEGRAPH)
}
}
override fun onPageFinished(url: String?) {
@ -80,18 +80,6 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
else super.onBackPressed()
}
override fun onActionModeStarted(mode: ActionMode?) {
val menu = mode?.menu
mode?.menuInflater?.inflate(R.menu.formatting, menu)
menu?.findItem(R.id.format)?.apply {
setOnMenuItemClickListener {
executeJavaScript("javascript:showFormatTooltip();")
false
}
}
super.onActionModeStarted(mode)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.activity_main, menu)
@ -120,12 +108,4 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
}
}
private fun executeJavaScript(code: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView?.evaluateJavascript(code, null)
} else {
webView?.loadUrl(code)
}
}
}

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/format"
android:title="@string/format"
app:showAsAction="always"/>
</menu>

View File

@ -5,5 +5,4 @@
<string name="help_text">
<![CDATA[<h3>How to use text formatting?</h3>You can use formatting like <b>bold</b>, <i>italics</i> and so on at least on Lollipop and above by double tapping the text.<h3>The app only shows strange things!</h3>It\'s not verified, that the app will work on devices with KitKat or lower. In Lollipop Google introduced a Chrome based and updatable WebView with more features and fixes. I\'m sorry! But in December \'16 Telegram will publish docs for the Telegraph API, so I\'ll be able to create a native app instead of a wrapper. Be patient!]]>
</string>
<string name="format">Format</string>
</resources>

View File

@ -1,11 +1,11 @@
buildscript {
ext.kotlin_version = '1.0.5-2'
ext.kotlin_version = '1.0.5-3'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-alpha1'
classpath 'com.android.tools.build:gradle:2.3.0-beta1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}