This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
viewModel.setUserName("Meet") | |
viewModel.getUsername.observe(this) { | |
binding.userName.text = it | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.asLiveData | |
import androidx.lifecycle.viewModelScope | |
import dagger.hilt.android.lifecycle.HiltViewModel | |
import kotlinx.coroutines.launch | |
import javax.inject.Inject | |
@HiltViewModel | |
class MainViewModel @Inject constructor(private val dataStoreManager: DataStoreManager) : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getUserNameWithTryCatch(): Flow<String?> = | |
dataStore.data.catch { exception -> | |
if (exception is IOException) { | |
emit(emptyPreferences()) | |
} else { | |
throw exception | |
} | |
}.map { myDataStore -> | |
myDataStore[userNameKey] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val userNameKey = stringPreferencesKey("userNameKey") | |
suspend fun storeUserName(username: String): Preferences { | |
return dataStore.edit { myDataStore -> | |
myDataStore[userNameKey] = username | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.datastore.preferences.core.stringPreferencesKey | |
private val userNameKey = stringPreferencesKey("userNameKey") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "MyDataStore") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Init flightMode Manager to handle warning dialog | |
private val flightModeManager by lazy { FlightModeManager(this) } | |
override fun onResume() { | |
super.onResume() | |
// Check flight Mode on OnResume | |
flightModeManager.checkFlightMode() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FlightModeManager(private val activity: AppCompatActivity) { | |
private var broadcastReceiver: BroadcastReceiver? = null | |
init { | |
initBroadcastReceiver() | |
// If Flight mode is off then we save false so we can show the dialog next time | |
// when user turn on the flight mode | |
if (!isFlightModeOn()) { | |
setFlightWarningShown(false) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let yearToStartTheSearchFrom = 2015; | |
let yearToEndTheSearch = 2022; | |
let startingIndex = 0; | |
let totalSpend = 0; | |
let globalSpend = 0; | |
for (let currentYear = yearToStartTheSearchFrom; currentYear <= yearToEndTheSearch; currentYear++) { | |
let isFetchCompleted = false; | |
while (!isFetchCompleted) { | |
const req = await fetch('https://www.amazon.in/gp/your-account/order-history/ref=ppx_yo_dt_b_pagination_3_1?ie=UTF8&orderFilter=year-' + currentYear + '&search=&startIndex=' + startingIndex, { |