Skip to content

Commit

Permalink
重构
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzia committed Nov 13, 2018
0 parents commit 53551f1
Show file tree
Hide file tree
Showing 77 changed files with 3,747 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
@@ -0,0 +1,11 @@
*.iml
.gradle
/local.properties
/.idea/caches/build_file_checksums.ser
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions app/.gitignore
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions app/build.gradle
@@ -0,0 +1,35 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.zia.easybook"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation project(':easybookmodule')
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
33 changes: 33 additions & 0 deletions app/src/main/AndroidManifest.xml
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zia.easybook">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".CatalogActivity">
</activity>
<activity android:name=".PreviewActivity">
</activity>
</application>

</manifest>
168 changes: 168 additions & 0 deletions app/src/main/java/com/zia/easybook/CatalogActivity.kt
@@ -0,0 +1,168 @@
package com.zia.easybook

import android.app.ProgressDialog
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.Toast
import com.zia.easybookmodule.bean.Book
import com.zia.easybookmodule.bean.Catalog
import com.zia.easybookmodule.bean.Type
import com.zia.easybookmodule.engine.EasyBook
import com.zia.easybookmodule.rx.Disposable
import com.zia.easybookmodule.rx.Subscriber
import kotlinx.android.synthetic.main.activity_catalog.*
import java.io.File
import java.util.*

class CatalogActivity : AppCompatActivity(), CatalogAdapter.CatalogSelectListener {

private lateinit var book: Book
private lateinit var adapter: CatalogAdapter
//控制内存泄漏
private var disposable: Disposable? = null

private val dialog by lazy {
val dialog = ProgressDialog(this@CatalogActivity)
dialog.setCancelable(false)
dialog.progress = 0
dialog.setMessage("")
dialog.setTitle("正在下载")
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
dialog
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_catalog)

book = intent.getSerializableExtra("book") as Book
initBookInfo()

adapter = CatalogAdapter(this)
catalogRv.layoutManager = LinearLayoutManager(this)
catalogRv.adapter = adapter

EasyBook.getCatalog(book)
.subscribe(object : Subscriber<List<Catalog>> {
override fun onFinish(t: List<Catalog>) {
val arrayList = ArrayList<Catalog>(t)
arrayList.reverse()
adapter.freshCatalogs(arrayList)
book_loading.visibility = View.GONE
}

override fun onError(e: Exception) {
Toast.makeText(this@CatalogActivity, e.message, Toast.LENGTH_SHORT).show()
book_loading.text = e.message
}

override fun onMessage(message: String) {

}

override fun onProgress(progress: Int) {
}

})

book_download.setOnClickListener {
chooseType()
}
}

private fun download(type: Type) {
disposable = EasyBook.download(book)
.setType(type)
.setThreadCount(150)
.setSavePath(Environment.getExternalStorageDirectory().path + File.separator + "book")
.subscribe(object : Subscriber<File> {
override fun onFinish(t: File) {
hideDialog()
Toast.makeText(this@CatalogActivity, "保存成功,位置在${t.path}", Toast.LENGTH_SHORT).show()
}

override fun onError(e: java.lang.Exception) {
e.printStackTrace()
Toast.makeText(this@CatalogActivity, e.message, Toast.LENGTH_SHORT).show()
hideDialog()
}

override fun onMessage(message: String) {
updateDialog(message)
}

override fun onProgress(progress: Int) {
updateDialog(progress)
}
})
}

override fun onDestroy() {
disposable?.dispose()
super.onDestroy()
}

override fun onCatalogSelect(itemView: View, position: Int, catalog: Catalog) {
val intent = Intent(this@CatalogActivity,PreviewActivity::class.java)
intent.putExtra("catalog",catalog)
intent.putExtra("book",book)
startActivity(intent)
}

private fun chooseType() {
val types = arrayOf("EPUB", "TXT")
val style =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) android.R.style.Theme_Material_Light_Dialog
else android.R.style.Theme_DeviceDefault_Light_Dialog
AlertDialog.Builder(this, style)
.setTitle("选择下载格式")
.setItems(types) { dialog, which ->
var type = Type.EPUB
when (which) {
0 -> {
type = Type.EPUB
}
1 -> {
type = Type.TXT
}
}
download(type)
}.show()
}

private fun initBookInfo() {
book_name.text = book.bookName
book_author.text = book.author
book_lastUpdateChapter.text = "最新:${book.lastChapterName}"
book_site.text = book.site.siteName
book_lastUpdateTime.text = "更新:${book.lastUpdateTime}"
}

private fun updateDialog(progress: Int?) {
if (progress != null) {
dialog.progress = progress
}
if (!dialog.isShowing) {
dialog.show()
}
}

private fun updateDialog(msg: String?) {
if (msg != null) {
dialog.setMessage(msg)
}
if (!dialog.isShowing) {
dialog.show()
}
}

private fun hideDialog() {
dialog.dismiss()
}
}
60 changes: 60 additions & 0 deletions app/src/main/java/com/zia/easybook/CatalogAdapter.kt
@@ -0,0 +1,60 @@
package com.zia.easybook

import android.annotation.SuppressLint
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.zia.easybookmodule.bean.Catalog
import kotlinx.android.synthetic.main.item_catalog.view.*
import java.util.*


/**
* Created by zia on 2018/11/1.
*/
class CatalogAdapter(private val catalogSelectListener: CatalogSelectListener) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {

var catalogs: ArrayList<Catalog>? = null

fun freshCatalogs(catalogs: ArrayList<Catalog>) {
this.catalogs = catalogs
notifyDataSetChanged()
}

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(p0.context).inflate(R.layout.item_catalog, p0, false)
return CatalogHolder(view)
}

override fun getItemCount(): Int {
return if (catalogs == null) 0
else {
catalogs!!.size
}
}

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is CatalogHolder -> {
val catalog = catalogs!![position]
holder.itemView.item_catalog_name.text = catalog.chapterName
holder.itemView.setOnClickListener {
catalogSelectListener.onCatalogSelect(
holder.itemView,
position,
catalog
)
}
}
}
}

class CatalogHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

interface CatalogSelectListener {
fun onCatalogSelect(itemView: View, position: Int, catalog: Catalog)
}
}

0 comments on commit 53551f1

Please sign in to comment.