Skip to content

Instantly share code, notes, and snippets.

Avatar
🦉

Hafiz Rahman hafizrahman

🦉
View GitHub Profile
@hafizrahman
hafizrahman / SubjectFragment.kt
Created Apr 20, 2020
Observe the MediatorLiveData given by ViewModel, and update RecyclerViewAdapter accordingly.
View SubjectFragment.kt
mainViewModel = ViewModelProvider(activity!!).get(MainViewModel::class.java)
// New observer
mainViewModel.getAllData()?.observe(viewLifecycleOwner,
Observer { subjectsAndRemindersPair ->
view.sro_subject_list.adapter = SubjectRecyclerViewAdapter(
subjectsAndRemindersPair.first,
subjectsAndRemindersPair.second,
listener)
})
@hafizrahman
hafizrahman / SubjectRecyclerViewAdapter.kt
Created Apr 20, 2020
Put in the two List<> as constructor parameters in RecyclerViewAdapter
View SubjectRecyclerViewAdapter.kt
class SubjectRecyclerViewAdapter(
val subjects: List<Subject>,
val reminders: List<Reminder>,
val listener: OnListFragmentInteractionListener?
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
@hafizrahman
hafizrahman / MainViewModel.kt
Created Apr 20, 2020
Methods inside ViewModel to grab two LiveData from repository and put them into one MediatorLiveData
View MainViewModel.kt
fun getSubjects(): LiveData<List<Subject>>? {
return sroRepository.getSubjects()
}
fun getReminders(): LiveData<List<Reminder>>? {
return sroRepository.getReminders()
}
fun getAllData(): CombinedSubjectReminders? {
var ldSubjects = getSubjects()
@hafizrahman
hafizrahman / CombinedSubjectReminders.kt
Created Apr 20, 2020
Example of a MediatorLiveData class to handle two LiveDatas at once.
View CombinedSubjectReminders.kt
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
// This class ia a MediatorLiveData created since I have two different sources of data:
// - subjects table
// - reminders table
// Since they then become two separate LiveData, and I need both to fill in the data
// on the main RecyclerView, then we're using a MediatorLiveData to grab both LiveData
// and add them as sources, so that we can notify observers when any of the data gets
@hafizrahman
hafizrahman / exampleActivity.kt
Created Dec 29, 2019
Example of View Binding using Kotlin Extension
View exampleActivity.kt
// Using R.layout.activity_main from the 'main' source set
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Instead of findViewById<TextView>(R.id.textView)
textView.setText("Hello, world!")
@hafizrahman
hafizrahman / constants.kt
Last active Dec 9, 2019
Constants file example.
View constants.kt
package studio.oldblack.wpflightcontrol
const val WPFC_SHARED_PREFS_FILENAME = "wpfc_prefs"
const val WPFC_WPCOM_AUTH_ENDPOINT = "https://public-api.wordpress.com/oauth2/authorize"
const val WPFC_WPCOM_TOKEN_ENDPOINT = "https://public-api.wordpress.com/oauth2/token"
@hafizrahman
hafizrahman / ConstantsUsageExample.kt
Created Dec 8, 2019
Example of how to use a constant variable
View ConstantsUsageExample.kt
val sharedPreferences = EncryptedSharedPreferences.create(
WPFC_SHARED_PREFS_FILENAME,
masterKeyAlias,
this,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
@hafizrahman
hafizrahman / gist:04265e530b2823792fa53c802a4894f2
Last active Dec 5, 2019
Add WP REST API support to certain taxonomies
View gist:04265e530b2823792fa53c802a4894f2
/**
* Add REST API support to an already registered taxonomy.
*/
add_filter( 'register_taxonomy_args', 'my_taxonomy_args', 10, 2 );
function my_taxonomy_args( $args, $taxonomy_name ) {
if ( 'freebbble-type' === $taxonomy_name || 'freebbble-license' === $taxonomy_name ) {
$args['show_in_rest'] = true;
@hafizrahman
hafizrahman / add-gutenberg-class.php
Last active Apr 10, 2019
WordPress: Add body class (for single post template) or post class (for blog/archive pages) "made-with-gutenberg" for posts/pages created with Gutenberg editor.
View add-gutenberg-class.php
// add class to body class on single post template
function themeprefix_body_class_blocks( $classes ) {
if ( is_singular() && has_blocks() ) {
$classes[] = 'made-with-gutenberg';
}
return $classes;
}
add_filter( 'body_class', 'themeprefix_body_class_blocks' );
View settag example
for (int i = 0; i < 5; i++) {
UICollectionView *currentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, maxWidth, collectionViewHeight) collectionViewLayout:layout];
// Trick to let the delegate and data source functions deal with the multiple UICollectionView's
[currentCollectionView setTag:i];
}