Skip to content

Instantly share code, notes, and snippets.

View chao2zhang's full-sized avatar
:octocat:

Chao Zhang chao2zhang

:octocat:
View GitHub Profile
@chao2zhang
chao2zhang / transform.py
Created January 15, 2023 04:53
Android module's package name from `Manifest.xml` to `build.gradle`
import os
import re
for root, dirs, files in os.walk("."):
for file in files:
pkg = ''
if file == 'AndroidManifest.xml' and 'build' not in root:
print(os.path.join(root, file))
content = open(os.path.join(root, file)).read()
@chao2zhang
chao2zhang / DIModule.kt
Created December 12, 2021 23:31
Shared Dispatcher
@CoDispatcher(DispatcherType.SHARED)
@Singleton
@Provides
fun sharedDispatcher(threadPoolExecutor: ThreadPoolExecutor): CoroutineDispatcher {
return threadPoolExecutor.asCoroutineDispatcher()
}
@chao2zhang
chao2zhang / HomeViewModel.kt
Created December 12, 2021 23:29
Use custom coroutine dispatcher
@HiltViewModel
class HomeViewModel @Inject constructor(
private val redditRepository: RedditRepository,
@CoDispatcher(DispatcherType.SHARED)
private val coroutineDispatcher: CoroutineDispatcher
) : ViewModel() {
val reddits: LiveData<RedditListing> = redditRepository
.topReddits
.asLiveData(context = coroutineDispatcher)
@chao2zhang
chao2zhang / DIModule.kt
Created December 12, 2021 23:02
Configure to work manager
// DIModule.kt
@Provides
@Singleton
fun workManagerConfig(threadPoolExecutor: ThreadPoolExecutor): Configuration {
return Configuration.Builder()
.setExecutor(threadPoolExecutor)
.build()
}
// MainApplication.kt
@chao2zhang
chao2zhang / DIModule.kt
Created December 12, 2021 22:45
Configure for OkHttp
@Provides
@Singleton
fun okhttpClient(
threadPoolExecutor: ThreadPoolExecutor,
@ApplicationContext applicationContext: Context
): OkHttpClient {
val client = OkHttpClient.Builder()
.dispatcher(Dispatcher(threadPoolExecutor))
.build()
@chao2zhang
chao2zhang / DIModule.kt
Created December 12, 2021 22:41
Create a shared thread pool executor
@Provides
@Singleton
fun threadPoolExecutor(): ThreadPoolExecutor {
val index = AtomicInteger()
return ThreadPoolExecutor(
Runtime.getRuntime().availableProcessors().coerceAtLeast(2),
Int.MAX_VALUE,
60,
TimeUnit.SECONDS,
SynchronousQueue(),
@chao2zhang
chao2zhang / logcat.log
Last active December 12, 2021 22:35
Thread pools
ConnectivityThread
FinalizerDaemon
FinalizerWatchdogDaemon
HeapTaskDaemon
OkHttp TaskRunner
OkHttp TaskRunner
OkHttp TaskRunner
OkHttp www.reddit.com
Okio Watchdog
ReferenceQueueDaemon
diff --git a/Flagship/build.gradle b/Flagship/build.gradle
index 9dd476b9e86..1ff6765ffb7 100644
--- a/Flagship/build.gradle
+++ b/Flagship/build.gradle
@@ -131,7 +131,7 @@ android {
release {
minifyEnabled true
shrinkResources true
- proguardFiles getDefaultProguardFile('proguard-android.txt'), ('proguard-rules.pro')
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), ('proguard-rules.pro')
@chao2zhang
chao2zhang / MainActivity.kt
Last active September 2, 2021 22:56
Hot Flow
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
binding = ActivityMainBinding.inflate(layoutInflater)
@chao2zhang
chao2zhang / MyViewModel.kt
Created September 2, 2021 22:50
Hot Flow MyViewModel
class MyViewModel : ViewModel() {
private val _sourceFlow = MutableStateFlow(0)
val sourceFlow: StateFlow<Int>
get() = _sourceFlow
private val _transformedFlow = _sourceFlow.map {
it * it
}
val transformedFlow: Flow<Int>