Skip to content

Commit

Permalink
+multiple tags support
Browse files Browse the repository at this point in the history
  • Loading branch information
lenchan139 committed Jun 28, 2017
1 parent 37db24c commit 6dd9ddb
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 54 deletions.
Binary file modified app/app-release.apk
Binary file not shown.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "org.lenchan139.ncbookmark"
minSdkVersion 21
targetSdkVersion 25
versionCode 4
versionName 'v4'
versionCode 5
versionName 'v5'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -28,9 +28,9 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:design:25.3.1'
compile 'org.jsoup:jsoup:1.10.2'
compile 'com.google.code.gson:gson:2.8.0'
testCompile 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.lenchan139.ncbookmark.Class

import org.json.JSONArray

/**
* Created by len on 28/6/2017.
*/

class BookmarkItemV2{

var title: String? = null
var url: String? = null
var tags: JSONArray? = null
var id: Int = 0

override fun toString(): String {
return "$url|$title|$tags"
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/org/lenchan139/ncbookmark/Class/TagsItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.lenchan139.ncbookmark.Class

import org.json.JSONArray

/**
* Created by len on 28/6/2017.
*/
class TagsItem{
fun jsonArrayToString(jsonArray: JSONArray): String?{

if(jsonArray.length() == 1){
return jsonArray.getString(0)
}else if (jsonArray.length() >= 2){
var array : ArrayList<String>
var str : String
str = jsonArray.getString(0)
for(i in 1..jsonArray.length()-1){
str = str + "," + jsonArray.getString(i)
}
return str
}
return null
}

fun strToArray(str: String): Array<String>? {
if(str.indexOf(",")<=0){
return arrayOf(str)
}else if(str.indexOf(",")>=1){
return str.split(",").toTypedArray()

}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.json.JSONException
import org.jsoup.Connection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.lenchan139.ncbookmark.Class.TagsItem
import org.lenchan139.ncbookmark.MainActivity
import org.lenchan139.ncbookmark.R

Expand Down Expand Up @@ -146,7 +147,7 @@ class AddBookmarkActivityV2 : AppCompatActivity() {
}

private inner class AddBookmarkTask : AsyncTask<URL, Int, Long>() {
internal var tag: String? = null
internal lateinit var tag: Array<String>
internal var url: String? = null
internal var title: String? = null
internal var des: String? = null
Expand All @@ -155,12 +156,16 @@ class AddBookmarkActivityV2 : AppCompatActivity() {
internal var error_msg: String? = null
internal var urlSe = "/index.php/apps/bookmarks/public/rest/v2/bookmark"
internal val base64login = String(Base64.encode(login.toByteArray(), 0))

override fun onPreExecute() {
tag = edtTag.text.toString()
val tampTag = TagsItem().strToArray(edtTag.text.toString())
if(tampTag!= null){
tag = tampTag
}
url = edtUrl.text.toString()
title = edtTitle.text.toString()
des = edtDescr.text.toString()
if (tag == null || url == null || title == null) {
if ( url == null || title == null) {
no_error = false
Toast.makeText(this@AddBookmarkActivityV2, "Please tag, url and title cannot be empty.", Toast.LENGTH_SHORT).show()
}
Expand All @@ -174,15 +179,18 @@ class AddBookmarkActivityV2 : AppCompatActivity() {
if (no_error) {

try {
result = Jsoup.connect(urlNt!! + urlSe)
var jsoup = Jsoup.connect(urlNt!! + urlSe)
.ignoreContentType(true)
.header("Authorization", "Basic " + base64login)
.method(Connection.Method.POST)
.data("url", url)
.data("item[tags][]", tag)
.data("title", title)
.data("description", des)
.execute().parse()
for(i in 0..tag.size-1){
jsoup.data("item[tags][]", tag.get(i).trim())
}
result = jsoup.execute().parse()

} catch (e: IOException) {
e.printStackTrace()
error_msg = e.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import org.jsoup.Connection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.lenchan139.ncbookmark.Class.BookmarkItem
import org.lenchan139.ncbookmark.Class.BookmarkItemV2
import org.lenchan139.ncbookmark.Class.TagsItem
import org.lenchan139.ncbookmark.R
import org.lenchan139.ncbookmark.v1.BookmarkViewActivity

Expand Down Expand Up @@ -121,7 +123,7 @@ class BookmarkViewActivityV2 : AppCompatActivity() {
}

override fun onPostExecute(result: Long?) {
val jsonList = ArrayList<BookmarkItem>()
val jsonList = ArrayList<BookmarkItemV2>()
try {
//JSONObject jsonResponse = new JSONObject(jsonText);

Expand All @@ -134,9 +136,9 @@ class BookmarkViewActivityV2 : AppCompatActivity() {
for (i in 0..cast.length() - 1) {
val tempJ = cast.getJSONObject(i)
if (tempJ.getString("tags") == "[\"\"]") {
val tempBi = BookmarkItem()
val tempBi = BookmarkItemV2()
tempBi.url = tempJ.getString("url")
tempBi.tags = tempJ.getString("tags")
tempBi.tags = tempJ.getJSONArray("tags")
tempBi.title = tempJ.getString("title")
tempBi.id = tempJ.getInt("id")
jsonList.add(tempBi)
Expand All @@ -146,9 +148,9 @@ class BookmarkViewActivityV2 : AppCompatActivity() {
} else {
for (i in 0..cast.length() - 1) {
val tempJ = cast.getJSONObject(i)
val tempBi = BookmarkItem()
val tempBi = BookmarkItemV2()
tempBi.url = tempJ.getString("url")
tempBi.tags = tempJ.getString("tags")
tempBi.tags = tempJ.getJSONArray("tags")
tempBi.title = tempJ.getString("title")
tempBi.id = tempJ.getInt("id")
jsonList.add(tempBi)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.json.JSONObject
import org.jsoup.Connection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.lenchan139.ncbookmark.Class.TagsItem
import org.lenchan139.ncbookmark.R

import java.io.IOException
Expand Down Expand Up @@ -87,7 +88,7 @@ class EditBookmarkActivityV2 : AppCompatActivity() {

private inner class EditBookmarkTask : AsyncTask<URL, Int, Long>() {
internal var rid = bookmarkId
internal var tag: String? = null
internal lateinit var tag: Array<String>
internal var url: String? = null
internal var title: String? = null
internal var des: String? = null
Expand All @@ -97,13 +98,16 @@ class EditBookmarkActivityV2 : AppCompatActivity() {
internal var urlSe = "/index.php/apps/bookmarks/public/rest/v2/bookmark/" + rid
internal val base64login = String(Base64.encode(login.toByteArray(), 0))
override fun onPreExecute() {
tag = edtTag.text.toString()
val tempTag = TagsItem().strToArray(edtTag.text.toString())
if(tempTag !=null){
tag = tempTag
}
url = edtUrl.text.toString()
title = edtTitle.text.toString()
des = edtDescr.text.toString()
if (tag == null || url == null || title == null) {
if (url == null || title == null) {
no_error = false
Toast.makeText(this@EditBookmarkActivityV2, "Please tag, url and title cannot be empty.", Toast.LENGTH_SHORT).show()
Toast.makeText(this@EditBookmarkActivityV2, "Please, url and title cannot be empty.", Toast.LENGTH_SHORT).show()
}
if (des == null) {
des = ""
Expand All @@ -115,16 +119,18 @@ class EditBookmarkActivityV2 : AppCompatActivity() {
override fun doInBackground(vararg params: URL): Long? {

try {
result = Jsoup.connect(urlNt!! + urlSe)
var jsoup = Jsoup.connect(urlNt!! + urlSe)
.ignoreContentType(true)
.header("Authorization", "Basic " + base64login)
.method(Connection.Method.PUT)
.data("url", url)
.data("record_id", rid.toString())
.data("item[tags][]", tag)
.data("title", title)
.data("description", des)
.execute().parse()
for(i in 0..tag.size-1){
jsoup.data("item[tags][]", tag.get(i).trim())
}
result = jsoup.execute().parse()
} catch (e: IOException) {
e.printStackTrace()
error_msg = e.message
Expand Down Expand Up @@ -232,7 +238,7 @@ class EditBookmarkActivityV2 : AppCompatActivity() {
if (cast.getJSONObject(i).getInt("id") == rid) {
finalObject = cast.getJSONObject(i)
edtDescr.setText(finalObject!!.getString("description"))
edtTag.setText(finalObject.getJSONArray("tags").getString(0))
edtTag.setText(TagsItem().jsonArrayToString(finalObject.getJSONArray("tags")))
edtTitle.setText(finalObject.getString("title"))
edtUrl.setText(finalObject.getString("url"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TagListActivityV2 : AppCompatActivity() {
internal var username: String? = null
internal var password: String? = null
internal lateinit var login: String
internal var urlSe = "/index.php/apps/bookmarks/public/rest/v2/bookmark?page=-1"
internal var urlSe = "/index.php/apps/bookmarks/public/rest/v2/tag"

override fun onResume() {
DlTask().execute()
Expand Down Expand Up @@ -92,35 +92,11 @@ class TagListActivityV2 : AppCompatActivity() {

val jsonList = ArrayList<BookmarkItem>()
try {
//JSONObject jsonResponse = new JSONObject(jsonText);

//raw to raw list
val a = JSONObject(result!!.body().text())
val cast = a.getJSONArray("data")
for (i in 0..cast.length() - 1) {
val tempJ = cast.getJSONObject(i)
val tempBi = BookmarkItem()
tempBi.url = tempJ.getString("url")
tempBi.tags = tempJ.getString("tags")
tempBi.title = tempJ.getString("title")
jsonList.add(tempBi)
Log.v("currJSONObj", tempBi.toString())
}
//get all tags
val listTags = ArrayList<String>()
for (i1 in jsonList.indices) {
val tempTags = jsonList[i1].tags
var isHad = false
//check tag if had.
for (i2 in listTags.indices) {
if (tempTags == listTags[i2]) {
isHad = true
}
}
//if not had, add it.
if (!isHad) {
listTags.add(tempTags!!)
}
var jsonArray = JSONArray(result!!.body().text())
listTags.add("!ungrouped")
for (i in 0..jsonArray.length()-1){
listTags.add(jsonArray.getString(i))
}
Log.v("listArray", listTags.toString())

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.2-5'
ext.kotlin_version = '1.1.3'
repositories {
jcenter()
}
Expand Down

0 comments on commit 6dd9ddb

Please sign in to comment.