This file contains hidden or 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 MainActivity : AppCompatActivity() { | |
| ... | |
| /** | |
| * Function to fetch posts | |
| */ | |
| private fun fetchPosts() { | |
| apiClient.getApiService(this).fetchPosts() | |
| .enqueue(object : Callback<PostsResponse> { | |
| override fun onFailure(call: Call<PostsResponse>, t: Throwable) { |
This file contains hidden or 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
| /** | |
| * Interface for defining REST request functions | |
| */ | |
| interface ApiService { | |
| ... | |
| @GET(Constants.POSTS_URL) | |
| fun fetchPosts(): Call<PostsResponse> |
This file contains hidden or 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
| /** | |
| * Retrofit instance class | |
| */ | |
| class ApiClient { | |
| private lateinit var apiService: ApiService | |
| fun getApiService(context: Context): ApiService { | |
| // Initialize ApiService if not initialized yet | |
| if (!::apiService.isInitialized) { |
This file contains hidden or 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
| /** | |
| * Interceptor to add auth token to requests | |
| */ | |
| class AuthInterceptor(context: Context) : Interceptor { | |
| private val sessionManager = SessionManager(context) | |
| override fun intercept(chain: Interceptor.Chain): Response { | |
| val requestBuilder = chain.request().newBuilder() | |
| // If token has been saved, add it to the request |
This file contains hidden or 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 MainActivity : AppCompatActivity() { | |
| ... | |
| /** | |
| * Function to fetch posts | |
| */ | |
| private fun fetchPosts() { | |
| // Pass the token as parameter |
This file contains hidden or 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
| interface ApiService { | |
| ... | |
| @GET(Constants.POSTS_URL) | |
| fun fetchPosts(@Header("Authorization") token: String): Call<PostsResponse> | |
| } |
This file contains hidden or 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
| data class PostsResponse ( | |
| @SerializedName("status_code") | |
| var status: Int, | |
| @SerializedName("message") | |
| var message: String, | |
| @SerializedName("posts") | |
| var posts: List<Post> | |
| ) |
This file contains hidden or 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
| data class Post ( | |
| @SerializedName("id") | |
| var id: Int, | |
| @SerializedName("title") | |
| var title: String, | |
| @SerializedName("description") | |
| var description: String, | |
This file contains hidden or 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 LoginActivity : AppCompatActivity() { | |
| private lateinit var sessionManager: SessionManager | |
| private lateinit var apiClient: ApiClient | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_login) | |
| apiClient = ApiClient() | |
| sessionManager = SessionManager(this) |
This file contains hidden or 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
| /** | |
| * Session manager to save and fetch data from SharedPreferences | |
| */ | |
| class SessionManager (context: Context) { | |
| private var prefs: SharedPreferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE) | |
| companion object { | |
| const val USER_TOKEN = "user_token" | |
| } |
NewerOlder