Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
yueeng committed May 18, 2016
1 parent 0cbd563 commit c4837ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ apply plugin: "jp.leafytree.android-scala"

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "io.github.yueeng.hacg"
minSdkVersion 14
targetSdkVersion 23
versionCode 17
versionName "1.1.1"
versionCode 18
versionName "1.1.2"
multiDexEnabled true
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
}
Expand Down
36 changes: 24 additions & 12 deletions app/src/main/scala/io/github/yueeng/hacg/Common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.support.multidex.MultiDexApplication
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.support.v7.app.AlertDialog.Builder
import android.text.InputType
import android.view.View
import android.view.View.OnLongClickListener
import android.widget.{EditText, Toast}
Expand All @@ -37,8 +38,8 @@ object HAcg {
private val SYSTEM_PHILOSOPHY: String = "system.philosophy"
private val SYSTEM_PHILOSOPHY_HOSTS: String = "system.philosophy_hosts"

val DEFAULT_HOSTS = List("www.hacg.me/wp", "www.hacg.be/wp", "www.hacg.club/wp", "www.hacg.lol/wp")
val DEFAULT_PHILOSOPHY_HOSTS = List("bbs.hacg.me/m", "www.zhexue.in/m")
val DEFAULT_HOSTS = List("www.hacg.me/wp", "www.hacg.li/wp", "www.hacg.be/wp", "www.hacg.club/wp", "www.hacg.lol/wp")
val DEFAULT_PHILOSOPHY_HOSTS = List("liqu.pro")

val RELEASE = "https://github.com/yueeng/hacg/releases"

Expand All @@ -54,6 +55,11 @@ object HAcg {

def web = s"http://$host"

def domain = host.indexOf('/') match {
case i if i >= 0 => host.substring(0, i)
case _ => host
}

// def wordpress = s"$web/wordpress"

def philosophy_host = config.getString(SYSTEM_PHILOSOPHY, DEFAULT_PHILOSOPHY_HOSTS.head)
Expand All @@ -66,12 +72,12 @@ object HAcg {

def philosophy = s"http://$philosophy_host"


def setHosts(context: Context, title: Int, hint: Int, hostlist: () => Set[String], cur: () => String, set: String => Unit, ok: String => Unit, reset: () => Unit): Unit = {
val edit = new EditText(context)
if (hint != 0) {
edit.setHint(hint)
}
edit.setInputType(InputType.TYPE_TEXT_VARIATION_URI)
new Builder(context)
.setTitle(title)
.setView(edit)
Expand All @@ -82,7 +88,7 @@ object HAcg {
.setPositiveButton(R.string.app_ok,
dialogClick { (d, w) =>
val host = edit.getText.toString
if (host.matches( """^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}(?:/.+)?$""")) {
if (host.isNonEmpty) {
ok(host)
} else {
Toast.makeText(context, hint, Toast.LENGTH_SHORT).show()
Expand All @@ -102,25 +108,31 @@ object HAcg {
.create().show()
}

def setHost(context: Context): Unit = {
def setHost(context: Context, ok: String => Unit = null): Unit = {
setHostx(context,
R.string.settings_host,
R.string.settings_host_sample,
() => HAcg.hosts,
() => HAcg.host,
host => HAcg.host = host,
host => {
HAcg.host = host
if (ok != null) ok(host)
},
host => HAcg.hosts = HAcg.hosts + host,
() => HAcg.hosts = HAcg.DEFAULT_HOSTS.toSet
)
}

def setPhilosophy(context: Context): Unit = {
def setPhilosophy(context: Context, ok: String => Unit = null): Unit = {
setHostx(context,
R.string.settings_philosophy_host,
R.string.settings_philosophy_sample,
() => HAcg.philosophy_hosts,
() => HAcg.philosophy_host,
host => HAcg.philosophy_host = host,
host => {
HAcg.philosophy_host = host
if (ok != null) ok(host)
},
host => HAcg.philosophy_hosts = HAcg.philosophy_hosts + host,
() => HAcg.philosophy_hosts = HAcg.DEFAULT_PHILOSOPHY_HOSTS.toSet
)
Expand Down Expand Up @@ -258,7 +270,7 @@ object Common {
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()
val data = (new MultipartBody.Builder /: post)((b, o) => b.addFormDataPart(o._1, o._2)).build()
val data = (new MultipartBody.Builder /: post) ((b, o) => b.addFormDataPart(o._1, o._2)).build()
val request = new Request.Builder().url(url).post(data).build()
val response = http.newCall(request).execute()
Option(response.body().string(), response.request().url().toString)
Expand Down Expand Up @@ -390,9 +402,9 @@ object CookieManagerProxy {
}

/**
* PersistentCookieStore
* Created by Rain on 2015/7/1.
*/
* PersistentCookieStore
* Created by Rain on 2015/7/1.
*/
class PersistCookieStore(context: Context) extends CookieStore {
private final val map = new mutable.HashMap[URI, mutable.HashSet[HttpCookie]]
private final val pref: SharedPreferences = context.getSharedPreferences("cookies.pref", Context.MODE_PRIVATE)
Expand Down
22 changes: 16 additions & 6 deletions app/src/main/scala/io/github/yueeng/hacg/MainActivity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer

class MainActivity extends AppCompatActivity {
lazy val adapter = new ArticleFragmentAdapter(getSupportFragmentManager)
lazy val pager: ViewPager = findViewById(R.id.container)
lazy val ad: ViewPager = findViewById(R.id.pager)

Expand All @@ -42,7 +43,6 @@ class MainActivity extends AppCompatActivity {
getSupportActionBar.setLogo(R.mipmap.ic_launcher)

val tabs: TabLayout = findViewById(R.id.tab)
val adapter = new ArticleFragmentAdapter(getSupportFragmentManager)
pager.setAdapter(adapter)
tabs.setupWithViewPager(pager)

Expand Down Expand Up @@ -119,8 +119,8 @@ class MainActivity extends AppCompatActivity {
val image = new ImageView(container.getContext)
image.setAdjustViewBounds(true)
if (crop) image.setScaleType(ScaleType.CENTER_CROP)
image.setOnClickListener(viewClick { v => Common.openWeb(MainActivity.this, s"http://hacg.club/gg/${position + 1}") })
Picasso.`with`(container.getContext).load(s"http://hacg.club/gg/${position + 1}.jpg").into(image)
image.setOnClickListener(viewClick { v => Common.openWeb(MainActivity.this, s"http://${HAcg.domain}/gg/${position + 1}") })
Picasso.`with`(container.getContext).load(s"http://${HAcg.domain}/gg/${position + 1}.jpg").into(image)
container.addView(image)
image
}
Expand Down Expand Up @@ -156,7 +156,7 @@ class MainActivity extends AppCompatActivity {

override def post(result: Option[(String, String, String)]): Unit = {
result match {
case Some((v: String, t:String, u: String)) =>
case Some((v: String, t: String, u: String)) =>
new Builder(MainActivity.this)
.setTitle(getString(R.string.app_update_new, Common.version(MainActivity.this), v))
.setMessage(t)
Expand All @@ -175,7 +175,7 @@ class MainActivity extends AppCompatActivity {
}

class ArticleFragmentAdapter(fm: FragmentManager) extends FragmentStatePagerAdapter(fm) {
lazy val data = List("/", "/anime.html", "/comic.html", "/erogame.html", "/age.html", "/op.html", "/category/rou")
lazy val data = List("/", "/anime.html", "/comic.html", "/game.html", "/age.html", "/op.html", "/rou.html")
lazy val title = getResources.getStringArray(R.array.article_categories)

override def getItem(position: Int): Fragment =
Expand Down Expand Up @@ -208,7 +208,11 @@ class MainActivity extends AppCompatActivity {
val suggestions = new SearchRecentSuggestions(this, SearchHistoryProvider.AUTHORITY, SearchHistoryProvider.MODE)
suggestions.clearHistory()
true
case R.id.settings => HAcg.setHost(this); true
case R.id.settings => HAcg.setHost(this, _ => adapter.current match {
case f: ArticleFragment => f.reload()
case _ =>
})
true
case R.id.philosophy => startActivity(new Intent(this, classOf[WebActivity])); true
case R.id.about =>
new Builder(this)
Expand Down Expand Up @@ -312,6 +316,12 @@ class ArticleFragment extends Fragment with ViewEx.ViewEx[Boolean, SwipeRefreshL
out.putBoolean("error", error.error)
}

def reload(): Unit = {
adapter.data.clear()
adapter.notifyDataSetChanged()
query(defurl)
}

override def refresh(): Unit = view.post(runnable { () => view.setRefreshing(value) })

override def onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle): View = {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/scala/io/github/yueeng/hacg/WebActivity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class WebFragment extends Fragment with ViewEx.ViewEx[Boolean, SwipeRefreshLayou
override def onOptionsItemSelected(item: MenuItem): Boolean = {
item.getItemId match {
case R.id.open => Common.openWeb(getActivity, uri); true
case R.id.settings => HAcg.setPhilosophy(getActivity); true
case R.id.settings => HAcg.setPhilosophy(getActivity, _ => web.loadUrl(defuri)); true
case _ => super.onOptionsItemSelected(item)
}
}
Expand Down

0 comments on commit c4837ee

Please sign in to comment.