Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Stavro Xhardha stavro96

🏠
Working from home
Block or report user

Report or block stavro96

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View GalleryFragment.kt
//gallery viewmodel instantiated before
galleryViewModel.galleryData.observe(this, Observer {
if(it.equals("SomeValueThatLiveDataHolds")){
Toast.makeText(mContext, it, Toast.LENGTH_LONG).show()
}
})
@stavro96
stavro96 / GalleryViewModel.kt
Last active Jun 25, 2019
Showing a gallery viewmodel
View GalleryViewModel.kt
class GalleryViewModel : ViewModel() {
private val _galleryData: MutableLiveData<String> = MutableLiveData()
var galleryData: LiveData<String> = _galleryData
init {
//some actions here
_galleryData.value = someDependency.getSomeValueFromRepositoryLayer()
}
}
@stavro96
stavro96 / SettingsRepositoryTest.kt
Created Jun 20, 2019
Checking the methods behavior on error response.
View SettingsRepositoryTest.kt
@Test
fun `on api error response method should return response code 400`() = runBlocking {
`when`(treasureApi.getCountriesListAsync(COUNTRIES_API_URL)).thenReturn(
Response.error(
400, ResponseBody.create(
MediaType.parse("application/json"),
"{\"error_message\":[\"Do you even lift?\"]}"
)
)
)
@stavro96
stavro96 / SetupRepository.kt
Created Jun 20, 2019
Mocking a fake response.
View SetupRepository.kt
`when`(treasureApi.getCountriesListAsync(COUNTRIES_API_URL)).thenReturn(
Response.error(
400, ResponseBody.create(
MediaType.parse("application/json"),
"{\"error_message\":[\"Do you even lift?\"]}"
)
)
)
View SettingsRepositoryTest.kt
@Test
fun `when writing country execution should go fine`() {
val country = Country("Albania", "Tirana", "no need")
setupRepository.saveCountryToSharedPreferences(country)
verify(rocket).writeString(COUNTRY_SHARED_PREFERENCE_KEY, country.name)
verify(rocket).writeString(CAPITAL_SHARED_PREFERENCES_KEY, country.capitalCity)
}
View SetupRepositoryTest.kt
@RunWith(JUnit4::class)
class SetupRepositoryTest {
private lateinit var setupRepository: SetupRepository
private lateinit var rocket: Rocket
private lateinit var treasureApi: TreasureApi
@Before
fun setUp() {
rocket = mock()
treasureApi = mock()
@stavro96
stavro96 / build.groovy
Created Jun 20, 2019
Importing mockito and mockito-kotlin
View build.groovy
testImplementation 'org.mockito:mockito-core:2.27.0'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"
@stavro96
stavro96 / SetupRepository.kt
Created Jun 20, 2019
Setting up SetupRepository
View SetupRepository.kt
@RunWith(JUnit4::class)
class SetupRepositoryTest {
@Before
fun setUp() {
print("Testing started")
}
@After
fun finish() {
@stavro96
stavro96 / AlarmManagerExample.kt
Last active Jun 17, 2019
Alarm Manager Example
View AlarmManagerExample.kt
private fun startSchedulingNotifications(midnightTime: Calendar) {
//kte e ben tek Aktiviteti/Fragmenti ku do e besh start alarmin
val intent = Intent(activity, SomeAlarmReceiverName::class.java)
val pendingIntent =
PendingIntent.getBroadcast(activity, 1001, intent, PendingIntent.FLAG_ONE_SHOT) //do e besh fire vetem 1 here
val alarmManager = activity!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager // ben cast ne AlarmManager ne java e ke kshtu:
// AlarmMangaer alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE)
val kohaQeKerkonTi = Calendar.getInstance()
kohaQeKerkonTi.set(HOUR_OF_DAY, 1) //ora 1
View SetupRepository.kt
class SetupRepository(
private val treasureApi: TreasureApi,
private val mSharedPreferences: Rocket
) {
suspend fun makeCountryApiCallAsync(): Response<ArrayList<Country>> =
treasureApi.getCountriesListAsync(COUNTRIES_API_URL)
fun saveCountryToSharedPreferences(country: Country) {
mSharedPreferences.writeString(COUNTRY_SHARED_PREFERENCE_KEY, country.name)
You can’t perform that action at this time.