Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aa #597

Merged
merged 5 commits into from
Apr 5, 2024
Merged

aa #597

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions annotations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
9 changes: 9 additions & 0 deletions annotations/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("java-library")
id("org.jetbrains.kotlin.jvm")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
8 changes: 8 additions & 0 deletions annotations/src/main/java/ceui/lisa/annotations/ItemHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ceui.lisa.annotations

import kotlin.reflect.KClass

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
@MustBeDocumented
annotation class ItemHolder(val itemHolderCls: KClass<*>)
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId "ceui.lisa.pixiv"
minSdkVersion 21
targetSdkVersion 33
versionCode 272
versionName "3.3.9"
versionCode 280
versionName "3.4.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

javaCompileOptions {
Expand Down Expand Up @@ -126,6 +126,9 @@ dependencies {
api 'io.reactivex.rxjava2:rxjava:2.2.21'
api 'io.reactivex.rxjava2:rxandroid:2.1.1'

implementation(project(":annotations")) // 1
implementation(project(":processor")) // 2
kapt(project(":processor")) // 3

implementation 'com.github.bumptech.glide:glide:4.14.2'
kapt 'com.github.bumptech.glide:compiler:4.14.2'
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@
</activity>
<activity android:name=".activities.VPActivity" />
<activity android:name=".activities.NovelActivity" />
<activity android:name=".jetpack.NavActivity" />

<receiver
android:name=".notification.RecommendAppWidgetProvider"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/ceui/lisa/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import ceui.lisa.fragments.FragmentViewPager;
import ceui.lisa.helper.DrawerLayoutHelper;
import ceui.lisa.helper.NavigationLocationHelper;
import ceui.lisa.jetpack.NavActivity;
import ceui.lisa.utils.Common;
import ceui.lisa.utils.Dev;
import ceui.lisa.utils.GlideUtil;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/ceui/lisa/http/AppApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ Observable<ListUser> getWhoFollowThisUser(@Header("Authorization") String token,
@Query("user_id") int user_id);


@GET("v1/illust/comments")
@GET("/v3/illust/comments")
Observable<ListComment> getIllustComment(@Header("Authorization") String token,
@Query("illust_id") int illust_id);

@GET("v1/novel/comments")
@GET("v3/novel/comments")
Observable<ListComment> getNovelComment(@Header("Authorization") String token,
@Query("novel_id") int novel_id);

Expand Down
14 changes: 0 additions & 14 deletions app/src/main/java/ceui/lisa/jetpack/CommonViewModel.kt

This file was deleted.

13 changes: 0 additions & 13 deletions app/src/main/java/ceui/lisa/jetpack/NavActivity.kt

This file was deleted.

9 changes: 0 additions & 9 deletions app/src/main/java/ceui/lisa/jetpack/NavFragment.kt

This file was deleted.

83 changes: 83 additions & 0 deletions app/src/main/java/ceui/loxia/KeyedViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ceui.loxia

import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.lifecycle.HasDefaultViewModelProviderFactory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.CreationExtras
import kotlin.reflect.KClass



@MainThread
public inline fun <reified VM : ViewModel> Fragment.keyedViewModels(
noinline keyPrefixProvider: ()->String,
noinline ownerProducer: () -> ViewModelStoreOwner = { this },
noinline extrasProducer: (() -> CreationExtras)? = null,
noinline factoryProducer: (() -> ViewModelProvider.Factory)? = null
): Lazy<VM> {
val owner by lazy(LazyThreadSafetyMode.NONE) { ownerProducer() }
return keyedCreateViewModelLazy(
keyPrefixProvider,
VM::class,
{ owner.viewModelStore },
{
extrasProducer?.invoke()
?: (owner as? HasDefaultViewModelProviderFactory)?.defaultViewModelCreationExtras
?: CreationExtras.Empty
},
factoryProducer ?: {
(owner as? HasDefaultViewModelProviderFactory)?.defaultViewModelProviderFactory
?: defaultViewModelProviderFactory
})
}

@MainThread
public fun <VM : ViewModel> Fragment.keyedCreateViewModelLazy(
keyPrefixProvider: ()->String,
viewModelClass: KClass<VM>,
storeProducer: () -> ViewModelStore,
extrasProducer: () -> CreationExtras = { defaultViewModelCreationExtras },
factoryProducer: (() -> ViewModelProvider.Factory)? = null

): Lazy<VM> {
val factoryPromise = factoryProducer ?: {
defaultViewModelProviderFactory
}
return KeyedViewModelLazy(keyPrefixProvider, viewModelClass, storeProducer, factoryPromise, extrasProducer)
}

public class KeyedViewModelLazy<VM : ViewModel> @JvmOverloads constructor(
private val keyPrefixProvider: ()->String,
private val viewModelClass: KClass<VM>,
private val storeProducer: () -> ViewModelStore,
private val factoryProducer: () -> ViewModelProvider.Factory,
private val extrasProducer: () -> CreationExtras = { CreationExtras.Empty }
) : Lazy<VM> {
private var cached: VM? = null

override val value: VM
get() {
val viewModel = cached
return if (viewModel == null) {
val factory = factoryProducer()
val store = storeProducer()
val canonicalName: String = viewModelClass.java.canonicalName
?: throw IllegalArgumentException("Local and anonymous classes can not be ViewModels")
ViewModelProvider(
store,
factory,
extrasProducer()
).get("${keyPrefixProvider()} : $canonicalName", viewModelClass.java).also {
cached = it
}
} else {
viewModel
}
}

override fun isInitialized(): Boolean = cached != null
}
3 changes: 3 additions & 0 deletions app/src/main/java/ceui/loxia/NavFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import ceui.lisa.R

open class NavFragment(layoutId: Int) : Fragment(layoutId)

interface ActionReceiver {
}

abstract class SlinkyListFragment(layoutId: Int = R.layout.fragment_slinky_list) : NavFragment(layoutId) {

open fun isDefaultLayoutManager(): Boolean {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/ceui/loxia/SpaceHolder.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package ceui.loxia

import ceui.lisa.annotations.ItemHolder
import ceui.lisa.databinding.CellSpaceBinding
import ceui.refactor.ListItemHolder
import ceui.refactor.ListItemViewHolder

class SpaceHolder : ListItemHolder() {
}

@ItemHolder(SpaceHolder::class)
class SpaceViewHolder(private val bd: CellSpaceBinding) : ListItemViewHolder<CellSpaceBinding, SpaceHolder>(bd) {

override fun onBindViewHolder(holder: SpaceHolder, position: Int) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/ceui/loxia/TextDescHolder.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ceui.loxia

import ceui.lisa.annotations.ItemHolder
import ceui.lisa.databinding.CellSpaceBinding
import ceui.lisa.databinding.CellTextDescBinding
import ceui.refactor.ListItemHolder
Expand All @@ -8,6 +9,7 @@ import ceui.refactor.ListItemViewHolder
class TextDescHolder(val content: String) : ListItemHolder() {
}

@ItemHolder(TextDescHolder::class)
class TextDescViewHolder(private val bd: CellTextDescBinding) : ListItemViewHolder<CellTextDescBinding, TextDescHolder>(bd) {

override fun onBindViewHolder(holder: TextDescHolder, position: Int) {
Expand Down
68 changes: 68 additions & 0 deletions app/src/main/java/ceui/loxia/UIAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ceui.loxia

import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.findFragment
import kotlin.reflect.KClass

inline fun<reified InterfaceT> Fragment.sendAction(action: (receiver: InterfaceT)->Boolean) {
var received = false
var itr: Fragment? = this
while (itr != null) {
val receiver = itr as? InterfaceT
if (receiver != null && action(receiver)) {
received = true
break
} else {
itr = itr.parentFragment
}
}

if (!received) {
val receiver = this.activity as? InterfaceT
if (receiver != null) {
action(receiver)
}
}
}

inline fun<reified InterfaceT> View.sendAction(action: (receiver: InterfaceT)->Boolean) {
val fragment = this.findFragment<Fragment>()
fragment.sendAction<InterfaceT>(action)
}

inline fun <reified ActionReceiverT> Fragment.findActionReceiverOrNull(): ActionReceiverT? {
var itr: Fragment? = this
while (itr != null) {
val receiver = itr as? ActionReceiverT
if (receiver != null) {
return receiver
} else {
itr = itr.parentFragment
}
}

return activity as? ActionReceiverT
}


inline fun<reified ActionReceiverT> View.findActionReceiverOrNull(): ActionReceiverT? {
val fragment = this.findFragmentOrNull<Fragment>()
return fragment?.findActionReceiverOrNull<ActionReceiverT>()
}

inline fun<reified ActionReceiverT> View.findActionReceiver(): ActionReceiverT {
val fragment = this.findFragment<Fragment>()
return fragment.findActionReceiverOrNull<ActionReceiverT>()!!
}

inline fun <reified F : Fragment> View.findFragmentOrNull(): F? {
return try {
val targetFragment = findFragment<Fragment>()
if (targetFragment is F) {
targetFragment as F
} else null
} catch (e: Exception) {
null
}
}
20 changes: 10 additions & 10 deletions app/src/main/java/ceui/loxia/flag/FlagReasonFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ceui.lisa.databinding.FragmentSlinkyListBinding
import ceui.loxia.*
import ceui.refactor.viewBinding

class FlagReasonFragment : SlinkyListFragment() {
class FlagReasonFragment : SlinkyListFragment(), FlagActionReceiver {

private val binding by viewBinding(FragmentSlinkyListBinding::bind)
private val safeArgs by threadSafeArgs<FlagReasonFragmentArgs>()
Expand All @@ -27,15 +27,6 @@ class FlagReasonFragment : SlinkyListFragment() {
setUpSlinkyList(binding.listView, binding.refreshLayout, binding.itemLoading, viewModel)
}

fun onHolderClick(holder: FlagReasonHolder) {
startActivity(Intent(requireContext(), TemplateActivity::class.java).apply {
putExtra(TemplateActivity.EXTRA_FRAGMENT, "填写举报详细信息")
putExtra(FlagDescFragment.FlagReasonIdKey, holder.id)
putExtra(FlagDescFragment.FlagObjectIdKey, safeArgs.flagObjectId)
putExtra(FlagDescFragment.FlagObjectTypeKey, safeArgs.flagObjectType)
})
}

companion object {
fun newInstance(flagObjectId: Int, flagObjectType: Int): FlagReasonFragment {
val fragment = FlagReasonFragment()
Expand All @@ -56,4 +47,13 @@ class FlagReasonFragment : SlinkyListFragment() {
requireActivity().finish()
}
}

override fun onClickFlag(holder: FlagReasonHolder) {
startActivity(Intent(requireContext(), TemplateActivity::class.java).apply {
putExtra(TemplateActivity.EXTRA_FRAGMENT, "填写举报详细信息")
putExtra(FlagDescFragment.FlagReasonIdKey, holder.id)
putExtra(FlagDescFragment.FlagObjectIdKey, safeArgs.flagObjectId)
putExtra(FlagDescFragment.FlagObjectTypeKey, safeArgs.flagObjectType)
})
}
}
12 changes: 10 additions & 2 deletions app/src/main/java/ceui/loxia/flag/FlagReasonRepository.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package ceui.loxia.flag

import ceui.lisa.R
import ceui.lisa.annotations.ItemHolder
import ceui.lisa.databinding.CellFlagReasonBinding
import ceui.loxia.RefreshState
import ceui.loxia.Repository
import ceui.loxia.findActionReceiverOrNull
import ceui.loxia.findFragmentOrNull
import ceui.loxia.novel.NovelTextHolder
import ceui.refactor.ListItemHolder
import ceui.refactor.ListItemViewHolder
import ceui.refactor.findFragmentOrNull
import ceui.refactor.setOnClick

class FlagReasonRepository : Repository<FlagReasonFragment>() {
Expand Down Expand Up @@ -51,13 +54,18 @@ class FlagReasonHolder(val id: Int, val content: String, val key: String) : List
}
}

@ItemHolder(FlagReasonHolder::class)
class FlagReasonViewHolder(binding: CellFlagReasonBinding) :
ListItemViewHolder<CellFlagReasonBinding, FlagReasonHolder>(binding) {

override fun onBindViewHolder(holder: FlagReasonHolder, position: Int) {
binding.flagReasonTv.text = holder.content
binding.root.setOnClick {
it.findFragmentOrNull<FlagReasonFragment>()?.onHolderClick(holder)
it.findActionReceiverOrNull<FlagActionReceiver>()?.onClickFlag(holder)
}
}
}

interface FlagActionReceiver {
fun onClickFlag(holder: FlagReasonHolder)
}
Loading
Loading