-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Page moved and API call changed; Add Navigation component for backdro…
…p menu; Placeholder for News fragment; Upgrades and Material theme. move version for play store, readme
- Loading branch information
Showing
17 changed files
with
351 additions
and
191 deletions.
There are no files selected for viewing
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
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
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
80 changes: 80 additions & 0 deletions
80
app/src/main/java/com/droidteahouse/coronaTracker/ui/BaseFragment.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.droidteahouse.coronaTracker.ui | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.NetworkCapabilities | ||
import android.os.Build | ||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.* | ||
import com.droidteahouse.GlideApp | ||
import com.droidteahouse.GlideRequests | ||
import com.droidteahouse.coronaTracker.R | ||
import com.droidteahouse.coronaTracker.ServiceLocator | ||
import com.droidteahouse.coronaTracker.repository.NetworkState | ||
import kotlinx.android.synthetic.main.fragment_coronatracker.* | ||
|
||
open class BaseFragment : Fragment() { | ||
var network: MutableLiveData<Boolean> = MutableLiveData<Boolean>() | ||
lateinit var glide: GlideRequests | ||
|
||
val model: CoronaTrackerViewModel by viewModels { | ||
object : AbstractSavedStateViewModelFactory(this, null) { | ||
override fun <T : ViewModel?> create( | ||
key: String, | ||
modelClass: Class<T>, | ||
handle: SavedStateHandle | ||
): T { | ||
val repo = ServiceLocator.instance(context!!) | ||
.getRepository() | ||
@Suppress("UNCHECKED_CAST") | ||
return CoronaTrackerViewModel(repo) as T | ||
} | ||
} | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
glide = GlideApp.with(context!!) | ||
} | ||
|
||
|
||
//@todo this will not work with a VPN connection like reverse tethering to detect if the relay server is cut | ||
fun checkNetwork(): Boolean { | ||
var result = false | ||
val cm = activity!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) { | ||
val activeNetworkInfo = cm.getActiveNetworkInfo() | ||
result = activeNetworkInfo != null && activeNetworkInfo.isConnected | ||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
val network = cm.activeNetwork | ||
val capabilities = cm | ||
.getNetworkCapabilities(network) | ||
result = capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) | ||
} | ||
network.value = result | ||
if (result == false) swipe_refresh.isRefreshing = false | ||
return result | ||
} | ||
|
||
fun initSwipeToRefresh() { | ||
swipe_refresh.setColorSchemeResources( | ||
R.color.colorPrimary, | ||
android.R.color.holo_green_dark, | ||
android.R.color.holo_orange_dark, | ||
android.R.color.holo_blue_dark) | ||
model.refreshState.observe(this, Observer { | ||
swipe_refresh.isRefreshing = (it == NetworkState.LOADING) | ||
checkNetwork() | ||
}) | ||
|
||
swipe_refresh.setOnRefreshListener { | ||
if (checkNetwork()) { | ||
model.refresh() | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.