Skip to content

Instantly share code, notes, and snippets.

View Wajahat-Jawaid's full-sized avatar
🎯
Focusing

Wajahat Jawaid Wajahat-Jawaid

🎯
Focusing
View GitHub Profile
// ...
fun getSubscribersCount(blogUrl: String) = liveData(Dispatchers.IO) {
try {
emit(Resource.success(data = repository.getSubscribersCount(blogUrl)))
} catch (exception: Exception) {
emit(Resource.error(data = null, message = exception.message ?: "Error!!!"))
}
}
class PostsAdapter(
private val posts: List<PostAdapterModel>,
private val viewLifecycleOwner: LifecycleOwner,
private val viewModel: PostsViewModel,
private val clickListener: OnPostClickListener
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// Deciding whether to inflate the header view or the item view based on the #viewType
// ...
private fun fetchPosts() {
// Verify if Internet is present
if (!isNetworkAvailable(requireContext())) {
hideLoadingAnimation()
showErrorView(R.string.no_internet)
return
}
mViewModel.getPosts()
class JetFilesApplication : Application() {
// AppContainer instance used by the rest of classes to obtain dependencies.
lateinit var container: AppContainer
override fun onCreate() {
super.onCreate()
container = AppContainerImpl()
}
}
data class ErrorMessage(val id: Long, @StringRes val messageId: Int)
fun Long.formattedDate(): String {
val sdf = SimpleDateFormat("dd MMM, yyyy HH:mm", Locale.getDefault())
return sdf.format(this)
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (hasPermissions()) {
renderComposeView()
} else {
requestPermission()
}
}
@Composable
fun JetFilesApp(
appContainer: AppContainer
) {
JetFilesTheme {
ProvideWindowInsets {
// Drawing content behind the system windows. Just a UX thing!
val systemUiController = rememberSystemUiController()
SideEffect {
systemUiController.setSystemBarsColor(Color.Transparent, darkIcons = true)
@Composable
fun StarButton(
isFavorite: Boolean,
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
val clickLabel = stringResource(if (isFavorite) R.string.unstar else R.string.star)
IconToggleButton(
checked = isFavorite,
object DialogUtils {
fun showAlertDialog(
context: Context,
@StringRes title: Int,
@StringRes message: Int,
@StringRes buttonText: Int,
action: () -> Unit
) {
val resources = context.resources