Skip to content

Commit

Permalink
1.1.6
Browse files Browse the repository at this point in the history
RecyclerView grouped;
RecyclerView header/footer;
WebActivity;
  • Loading branch information
nyssance committed Dec 24, 2018
1 parent 121be6d commit 6d1908c
Show file tree
Hide file tree
Showing 50 changed files with 690 additions and 463 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ genos
* [Glide](https://github.com/bumptech/glide)
* [EventBus](https://github.com/greenrobot/EventBus)
* [Logger](https://github.com/orhanobut/logger)
* [AgentWeb](https://github.com/Justson/AgentWeb)

Special thanks [bintray-release](https://github.com/novoda/bintray-release), you save my life.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.0-alpha07")
classpath("com.android.tools.build:gradle:3.4.0-alpha08")
classpath(kotlin("gradle-plugin", "1.3.11"))
classpath("com.novoda:bintray-release:0.9")

Expand Down
17 changes: 10 additions & 7 deletions genos/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,27 @@ android {
}

dependencies {
val ktxCoreVersion = "1.0.1"
val ktxVersion = "1.0.1"
val ktxFragmentVersion = "1.0.0"

val materialVersion = "1.0.0"
// https://developer.android.com/topic/libraries/support-library/features.html
val constraintVersion = "1.1.2"
val lifecycleVersion = "2.0.0"
// https://developer.android.com/topic/libraries/architecture/adding-components.html
val pagingVersion = "2.0.0"
val workVersion = "1.0.0-alpha11"
val workVersion = "1.0.0-alpha13"
// vendor
val loggerVersion = "2.2.0" // https://github.com/orhanobut/logger
val retrofitVersion = "2.5.0" // https://square.github.io/retrofit/
val glideVersion = "4.8.0" // https://github.com/bumptech/glide
val eventbusVersion = "3.1.1" // https://github.com/greenrobot/EventBus
val agentWebVersion = "4.0.2" // https://github.com/Justson/AgentWeb

// Kotlin
implementation(kotlin("stdlib-jdk8", KotlinCompilerVersion.VERSION))
implementation(kotlin("reflect", KotlinCompilerVersion.VERSION))
// KTX
api("androidx.core:core-ktx:$ktxCoreVersion")
api("androidx.core:core-ktx:$ktxVersion")
api("androidx.fragment:fragment-ktx:$ktxFragmentVersion")
//
api("com.google.android.material:material:$materialVersion")
Expand All @@ -99,9 +99,12 @@ dependencies {
isTransitive = false
}
api("org.greenrobot:eventbus:$eventbusVersion")
api("com.just.agentweb:agentweb:$agentWebVersion")
api("com.just.agentweb:download:$agentWebVersion")
api("com.just.agentweb:filechooser:$agentWebVersion")
// Test
testImplementation("junit:junit:4.12")
androidTestImplementation("androidx.test.ext:junit:1.0.0")
androidTestImplementation("androidx.test:runner:1.1.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.1.0")
androidTestImplementation("androidx.test.ext:junit:1.1.0")
androidTestImplementation("androidx.test:runner:1.1.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.1.1")
}
9 changes: 9 additions & 0 deletions genos/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}

# [AgentWeb](https://github.com/Justson/AgentWeb)
-keep class com.just.agentweb.** {
*;
}
-dontwarn com.just.agentweb.**

# Java 注入类不要混淆 , 例如 sample 里面的 AndroidInterface 类 , 需要 Keep 。
# -keepclassmembers class com.just.agentweb.sample.common.AndroidInterface{ *; }
43 changes: 43 additions & 0 deletions genos/src/main/java/genos/Extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2018 NY <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package genos

import android.content.Intent
import android.widget.Toast
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment

// NY: ContextCompat, ViewCompat里有很多版本兼容方法, 不用自己写

@ColorInt
fun Fragment.getColor(@ColorRes id: Int): Int {
return ContextCompat.getColor(requireContext(), id)
}

@JvmOverloads
fun Fragment.startActivitySafely(intent: Intent, isNewTask: Boolean = false) { // 默认为不开启外部Activity
if (isNewTask) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // 该句决定是否在同一进程中
}
try {
startActivity(intent)
} catch (e: Exception) {
Toast.makeText(requireContext(), e.localizedMessage, Toast.LENGTH_LONG).show()
}
}
62 changes: 21 additions & 41 deletions genos/src/main/java/genos/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

package genos

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.os.Environment
import android.telephony.TelephonyManager
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.orhanobut.logger.Logger

object Helper {
Expand Down Expand Up @@ -65,60 +62,43 @@ object Helper {
return false
}

// @IdRes
@JvmStatic
fun getResId(context: Context, name: String, defType: String): Int {
val id = context.resources.getIdentifier(name, defType, context.packageName)
if (id == 0) {
Logger.t("helper").wtf("R.$defType.$name 不存在")
}
return id
fun getApplicationName(context: Context): String {
val info = context.applicationInfo
val resId = info.labelRes
return if (resId != 0) context.getString(resId) else info.nonLocalizedLabel.toString()
}

// Color
@JvmStatic
@ColorInt
fun getColor(context: Context, @ColorRes id: Int): Int {
return ContextCompat.getColor(context, id)
fun getActivityName(activity: Activity): String? {
try {
val pm = activity.packageManager
return pm.getActivityInfo(activity.componentName, 0).loadLabel(pm).toString()
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
return null
}

// Drawable
// SO: https://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22
@JvmStatic
fun getDrawable(context: Context, @DrawableRes id: Int): Drawable? {
return ContextCompat.getDrawable(context, id)
// return ResourcesCompat.getDrawable(context.getResources(), id,
// theme)
fun getResId(context: Context, name: String, defType: String, log: Boolean = true): Int {
val id = context.resources.getIdentifier(name, defType, context.packageName)
if (log && id == 0) {
Logger.t("helper").wtf("R.$defType.$name 不存在")
}
return id
}

// 其他
@JvmStatic
fun getColorFromIdentifier(context: Context, id: Int): Int {
val resId = getResId(context, context.resources.getResourceEntryName(id), "color")
return if (resId != 0) getColor(context, resId) else 0
return if (resId != 0) ContextCompat.getColor(context, resId) else 0
}

@JvmStatic
fun getDrawableFromIdentifier(context: Context, id: Int): Drawable? {
val resId = getResId(context, context.resources.getResourceEntryName(id), "drawable")
return if (resId != 0) getDrawable(context, resId) else null
}

@JvmStatic
fun getApplicationName(context: Context): String {
val applicationInfo = context.applicationInfo
val stringId = applicationInfo.labelRes
return if (stringId == 0) applicationInfo.nonLocalizedLabel.toString() else context.getString(stringId)
}

@JvmStatic
fun getActivityName(activity: FragmentActivity): CharSequence? {
try {
val pm = activity.packageManager
return pm.getActivityInfo(activity.componentName, 0).loadLabel(pm)
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
return null
return if (resId != 0) context.getDrawable(resId) else null
}
}
17 changes: 17 additions & 0 deletions genos/src/main/java/genos/Shortcuts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package genos

import android.content.Context
import android.content.Intent
import android.widget.Toast
import genos.ui.activity.WebActivity

object Shortcuts {
@JvmStatic
Expand All @@ -32,4 +34,19 @@ object Shortcuts {
@JvmStatic
fun showActionSheet() {
}

@JvmStatic
fun openLink(context: Context, url: String, title: String? = null) {
if (url.isBlank()) {
return
}
val intent = Intent(context, WebActivity::class.java)
intent.putExtra("url", url)
intent.putExtra("title", title)
try {
context.startActivity(intent)
} catch (e: Exception) {
Toast.makeText(context, e.localizedMessage, Toast.LENGTH_LONG).show()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
* limitations under the License.
*/

package genos.models
package genos.model

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.annotation.StringRes

abstract class BaseItem {
var id = 0
var name: String
var icon: Drawable? = null
var icon: Any? = null
var title: String
var subtitle: String? = null
var enabled = false

constructor(context: Context, @StringRes id: Int, name: String? = null, icon: Drawable? = null, title: String? = null, subtitle: String? = null, enabled: Boolean = false) {
@JvmOverloads
constructor(context: Context, @StringRes id: Int, name: String? = null, icon: Any? = null, title: String? = null, subtitle: String? = null, enabled: Boolean = false) {
this.id = id
this.name = name ?: context.resources.getResourceEntryName(id)
this.icon = icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,35 @@
* limitations under the License.
*/

package genos.models
package genos.model

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import androidx.annotation.StringRes
import androidx.fragment.app.FragmentActivity
import androidx.collection.SimpleArrayMap
import genos.BaseAppManager.Companion.APP_SCHEME

class Item : BaseItem {
var choices = HashMap<String, String>()
var dest: FragmentActivity? = null
open class Item : BaseItem {
var choices = SimpleArrayMap<String, String>()
var dest: Class<out Activity>? = null
var link = ""
set(value) {
field = value
enabled = (dest != null || link.isNotBlank())
}
var isSection = false

@JvmOverloads
constructor(context: Context, @StringRes id: Int, name: String? = null,
icon: Drawable? = null, title: String? = null, subtitle: String? = null,
dest: FragmentActivity? = null, link: String = "", enabled: Boolean = false,
choices: HashMap<String, String> = HashMap()) : super(context, id, name, icon, title, subtitle, enabled) {
icon: Any? = null, title: String? = null, subtitle: String? = null,
dest: Class<out Activity>? = null, link: String = "", enabled: Boolean = false,
choices: SimpleArrayMap<String, String> = SimpleArrayMap(),
isSection: Boolean = false) : super(context, id, name, icon, title, subtitle, enabled) {
this.choices = choices
this.dest = dest
if (link.isBlank()) {
dest?.let {
var str = dest.javaClass.simpleName.toLowerCase()
var str = it.javaClass.simpleName.toLowerCase()
str = when {
str.endsWith("list") -> str.replace("list", "s")
str.endsWith("detail") -> str.removeSuffix("detail")
Expand All @@ -48,5 +55,6 @@ class Item : BaseItem {
this.link = link.trim()
}
this.enabled = enabled || this.link.isNotBlank()
this.isSection = isSection
}
}
12 changes: 8 additions & 4 deletions genos/src/main/java/genos/ui/BaseAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package genos.ui

import android.util.SparseArray
import android.view.MotionEvent
import androidx.recyclerview.selection.ItemDetailsLookup
import androidx.recyclerview.selection.ItemKeyProvider
Expand All @@ -27,6 +28,11 @@ abstract class BaseAdapter<T : Any, VH : RecyclerView.ViewHolder> : RecyclerView
private set
lateinit var detailsLookup: ItemDetailsLookup<Long>
private set
val sections = SparseArray<String?>()

init {
sections.put(0, null)
}

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
// KeyProvider & DetailsLookup
Expand All @@ -42,10 +48,8 @@ abstract class BaseAdapter<T : Any, VH : RecyclerView.ViewHolder> : RecyclerView
}
detailsLookup = object : ItemDetailsLookup<Long>() {
override fun getItemDetails(e: MotionEvent): ItemDetails<Long>? {
val view = recyclerView.findChildViewUnder(e.x, e.y)
if (view != null) {
val holder = recyclerView.getChildViewHolder(view)
val position = holder.adapterPosition
recyclerView.findChildViewUnder(e.x, e.y)?.let {
val position = recyclerView.getChildViewHolder(it).adapterPosition
return object : ItemDetails<Long>() {
override fun getPosition(): Int {
return position
Expand Down
Loading

0 comments on commit 6d1908c

Please sign in to comment.